Hello!
I'm fairly new to c++ but I have been following tutorials and have created functions before, but not one using a string. I can't work out what the problem is here.
The function is supposed to accept a file location, open the file, search for a phrase, and return a result if the certain phrase is found, otherwise return nothing. Then the result is written to a text file later in the program.
Im using visual c++ 2008 and it is currently a console application, which i plan to change once i can get the function working.
Here is my code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string passtocheckpriorityfunction;
string priorityresult;
string checkpriority (string filenameandpath){ // my function
string line;
ifstream myinputfile (filenameandpath);
if (myinputfile.is_open())
{
while (! myinputfile.eof())
{
getline (myinputfile,line);
if (line == "Importance: High"){
return << "Importance: 1.\n";
}
}
myinputfile.close();
}
}
cout << "Enter the file path and name";
cin >> passtocheckpriorityfunction;
priorityresult = checkpriority(passtocheckpriorityfunction)
ofstream myoutputfile ("result.txt");
myoutputfile << priorityresult;
myoutputfile.close();
return 0;
}
And here is my long list of errors:
------ Build started: Project: FunctionTest, Configuration: Debug Win32 ------
Compiling...
FunctionTest.cpp
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(17) : error C2601: 'checkpriority' : local function definitions are illegal
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(12): this line contains a '{' which has not yet been matched
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(43) : error C2146: syntax error : missing ';' before identifier 'ofstream'
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(43) : error C2146: syntax error : missing ';' before identifier 'myoutputfile'
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(43) : error C2275: 'std::ofstream' : illegal use of this type as an expression
c:\program files\microsoft visual studio 9.0\vc\include\iosfwd(720) : see declaration of 'std::ofstream'
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(43) : error C3861: 'myoutputfile': identifier not found
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(44) : error C2065: 'myoutputfile' : undeclared identifier
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(45) : error C2065: 'myoutputfile' : undeclared identifier
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(45) : error C2228: left of '.close' must have class/struct/union
type is ''unknown-type''
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(21) : error C2664: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstrea m(const char *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const char *'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
c:\documents and settings\me\my documents\visual studio 2008\projects\functiontest\functiontest.cpp(30) : error C2059: syntax error : '<<'
Build log was saved at "file://c:\Documents and Settings\me\My Documents\Visual Studio 2008\Projects\FunctionTest\Debug\BuildLog.htm"
FunctionTest - 10 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========