reading input ?

#include <iostream>
#include <conio>
#include <fstream>
#include <iomanip>


int main()
{
int choice;
const int SIZE =41;
char name[SIZE],address[SIZE],hpnum[SIZE],opnum[SIZE],mpnum[SIZE];
double icnum ;



cout <<" \n\n\n\n ---------------1. Save the information--------------"<<endl;
cout <<" ---------------2. Retrieve the information----------"<<endl;
cout <<" ---------------3. Exit------------------------------"<<endl;



cout <<"\n\nPlease enter your choice : <1>,<2>,<3> ";
cin >> choice;
cin.ignore();


if (choice ==1)
{
cout <<"\nEnter your friend's name :";
cin.getline(name,SIZE);
cout <<name<<endl;
cout <<"Please enter your friend's IC number :";
cin >>icnum;
cout <<setprecision(12)<<icnum;
cin.ignore();
cout<<"\nPlease enter your friends's Address :";
cin.getline(address,SIZE);
cout<<address;
cout <<"\nPlease enter your friend's home phone number :";
cin >>hpnum;
cout <<"\nPlease enter your friend's office phone number :";
cin >>opnum;
cout <<"\nPlease enter your friend's mobile phone number :";
cin >>mpnum;


ofstream output;
output.open("Demo.txt");
output <<name<<endl;
output <<setprecision(12)<<icnum<<endl;
output <<address<<endl;
output <<hpnum<<endl;
output <<opnum<<endl;
output <<mpnum<<endl;

output.close();

}

if (choice == 2)
{
ifstream input ;
input.open("Demo.txt");

input >> name;
cout <<name<<endl;

input >> name;
cout <<name<<endl;

input >> name;
cout <<name<<endl;

input >> name;
cout <<name<<endl;

input >> name;
cout <<name<<endl;

input >> name;
cout <<name<<endl;

input.close();
}







getch();
return 0;

}

Firstly,i type the "1" button to answered the question ,which is later print out in the Demo text file like :

low nan zhi
8899776655
2334,Taman Berapi ,tangganu
8988
554
656

After that i execute the program again and type "2" to test if the info could be retrieve.This is the result i got :
low
nan
zhi
8899776655
2334,Taman
Berapi

How could i retrieve the information with the right sequences just exactly looks like the info in the text file ?
low nan zhi
8899776655
2334,Taman Berapi ,tangganu
8988
554
656


Try to use the getline() function, as you did for the cin.

input.getline( name, SIZE );
It reads a full line from the stream file.
Simply using
input >> name

seems to use a space as a delimiter. Try using getline() when reading from the file, since reading the full line is what you are trying to do.

Edit: lol, I was too slow :(
Last edited on
Topic archived. No new replies allowed.