I want to read from a file called test.txt that has the following terms
1945
egg
vegetable
I have both a file with above called test.txt and the code below in the same file. I want the console output it should be:
The age is 1945
The name is egg
The snack is vegetable
When I run the program it gives me http://pastebin.com/5PnWrLHY
What do I need to change in my code to get this message to work?
> ifstream giveit(file);
> no known conversion for argument 1 from ‘std::string’ to ‘const char*’
As you can see here http://www.cplusplus.com/reference/fstream/ifstream/open/ you need c++11 to use a string to open a file. (-std=c++11 flag for gcc)
As an alternative, you may use the other constructor (the one that expects an constchar*ifstream giveit(file.c_str());
> error: expected primary-expression before ‘<<’ token file << <<endl; you've got too many <<, it should be file << endl;
By the way, don't change the meaning of your variables. `file' was supposed to hold the file name, but later you use it to capture the age, name and snack
lab08.cpp:11:22: error: expected initializer before ‘giveit’
const char* ifstream giveit(file.c_str());
^
lab08.cpp:14:19: error: ‘giveit’ was not declared in this scope
if (getline(giveit,line))
^
lab08.cpp:14:26: error: ‘line’ was not declared in this scope
if (getline(giveit,line))
^
lab08.cpp:30:3: error: ‘giveit’ was not declared in this scope
giveit.close();
^
How do I add an initializer before giveit and declare it and the new variable?