C++ tutorial 67 Error

0 Vikram Jayanthi · July 16, 2014
I have a problem with a program made in tutorial 67.
This program is meant to print out all the information in a text file onto the screen in a custom order. 
Code Below:

#include<iostream>
#include<fstream>
using namespace std;


int main()
{
ifstream file("characters.txt");


string fname,lname;
int age;
float money;
while(file>>fname>>lname>>age>>money)
{
cout<<fname<<"   "<<lname<<age<<money<<endl;




}






}


When I run it the data doesn't get displayed.There are no errors or warnings but the information from the file doesnt get displayed. The content of the file to be accessed is below:
characters.txt:
Vikram    Jayanthi   15   300

Please help if you know why this is happening.



 

Post a Reply

Replies

Oldest  Newest  Rating
+1 Franz Schmidt · July 18, 2014
I tried your program and I had to include "string" library then it worked without problems.

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
ifstream file("characters.txt");
if (!file){
cout << "Error: Can't open characters.txt!" << endl;
cin.sync();
cin.get();
}
else{
string fname, lname;
int age;
float money;
while (file >> fname >> lname >> age >> money)
{
cout << fname << "   " << lname << "   " << age << "   " << money << "   " << endl;
}
cin.sync();
cin.get();
}
}
  • 1

C++

107,075 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator