why it shows twice
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
void getfilename(string,string&);
int main()
{
ifstream input;
ofstream output;
int number;
int numberquestion;
string inputfile,outputfile;//
getfilename("input",inputfile);
getfilename("output",outputfile);
input.open(inputfile.c_str());
output.open(outputfile.c_str());
input>>number;
while(!input.eof())
{
input>>numberquestion;
output<<"full score: "<<numberquestion*2<<endl;
}
input.close();
output.close();
return 0;
}
void getfilename(string filetype,string& filename)
{
cout<<"enter name of"<<filetype<<"file\n";
cin>>filename;
}
|
input is
4
20
output is
full score: 40
full score: 40
why does it show twice??
Because you are looping on EOF. Don't do that.
You have to check whether the input operation failed immediately after attempting to input; you cannot wait. Combine lines 14..17 thus:
14 15 16 17
|
while (input >> numberquestion)
{
output << "full score: " << (numberquestion * 2) << endl;
}
|
Hope this helps.
Topic archived. No new replies allowed.