Hey i'm in desperate need of some help. I'm doing my Advanced Higher Computing project for school, and my programming has come to a complete halt.
What im trying to do is create a quiz ( it's the Tv show Pointless), where each answer has a certain amount of points linked to it. Also each question has ten possible answers. So basically i need to read these from a file, into the struct:
struct record
{
char question[50];
char answer[10][10];
int points_worth[10];
};
I put a new line between each question/answer/points in the file. I need some help getting the information from the file into my struct.
The code i have is :
while(!myfile.eof())
{
getline(myfile, pointless.question,'\n');
for (int i =0;i<10:i++)
{
getline(myfile,pointless.answer[i],'\n');
getline(myfile,pointless.points_worth[i],'/n');
}
}
The structs name is obviously pointless, and the file is called myfile. However im getting errors saying no instance of overload function for getline?
I have to run to work right now, but try referring to this:
http://www.cplusplus.com/reference/iostream/ifstream/
1 2 3 4 5 6 7 8 9 10 11 12
///Note not checked for syntax errors
/**
* @param s - The buffer to read into
* @param n - The size of the stream to read
* @param delim - A delimiter to signify the end of a read
*/
istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );
char* s = newchar[50];
ifstream stream("somefile.txt");
stream.getline(s, 50, '\n');
I can take over for Luc Lieber, what method are you using to open myfile? Try declaring record.question as an std::string, this will read them in better.
According to this page here, getline is defined in the strings library as excepting a string as input. Right now you are trying to "Overload" getline by passing it a char array and it doesn't like that.
Thanks for the replies. My code is now compiling, but it hangs when reading the file and doesnt display the results. I'll show you the complete program that i have.
#include <iostream>
#include<fstream>
#include<string>
usingnamespace std;
struct record
{
string question;
string answer [10];
int points_worth[10];
};
record pointless;
record tester;
int main()
{
ofstream myfile;
myfile.open("trial.txt");
cout<<"Please Input Question"<<endl;
cin.getline(pointless.question,50);
myfile<<pointless.question;
myfile<<"\n";
for (int i=0;i<10;i++)
{
cout<<"Please input an Answer"<<endl;
cin >> pointless.answer[i];
myfile<<pointless.answer[i];
myfile<<"\n";
cout<<"please enter points for that answer"<<endl;
cin>>pointless.points_worth[i];
myfile<<pointless.points_worth[i];
}
myfile.close();
// this section of code works fine for writing to the file
ifstream file_op("trial.txt");
while(!=myfile.eof())
{
istream$ getline)char*s, char delim);
char*s =newchar[50]
ifstream stream ("myfile.txt");
stream.getline(tester,question,'\n');
for (int i=0;i<10;i++)
{
stream.getline(tester,answer[i],'\n');
}
cout<<tester.question;
for (int i=0;i<10;i++)
{
cout<<tester.answer[i]
}
}
system("PAUSE");
return 0;
}
I have left out the reading of the points_worth as i need to convert this from char to an int, and well taht will be the next bit to tackle. This is just hanging once i have inputed the question, ten answers and ten points. I have tried to manipulate the code for string, but my compiler started throwing all sort of errors at me. If there is anyone who knows how to fix this it would be greatly appreciated!