I write the folowing code which is taking RAM and Hard disk capacity from user by using structures and than saving it to a new file after the user complete entring data it is displaying data entered by the user by opening that file. Problm is that i want to ask the user to choose an option (y/n): If user enter y then your program again get the next record of computer specification i.e RAM capacity and hard disk capacity and store the information in file. And when the user enter n it will stop getting information and display the data of the file. For this i use do while construct but it is not working properly it is getting data but displays only last entry. plz tell me wat should i do.
the code is:
#include <conio.h>
#include <iostream.h>
#include <fstream.h>
struct computerspec
{
int ram;
int hdisk;
}compuspec;
void main()
{
char ch;
do
{
//prompt the user to enter RAM capacity
cout<<"Enter the RAM capacity"<<endl;
cin>>compuspec.ram;
cout<<endl;
//prompt the user to enter Hard Disk capacity
cout<<"Enter the Hard Disk capacity"<<endl;
cin>>compuspec.hdisk;
cout<<endl;
cout<<"Press 'y' if u wanna continue"<<endl;
ch=getch();
cout<<endl;
}
while(ch=='y'||ch=='Y');
ofstream file;
file.open("computerspec.txt");
file<<"ComputerSpec attributes/Data members\n";
file<<"RAM"<<"\t"<<"Hard disk"<<endl;
file<<compuspec.ram<<"\t"<<compuspec.hdisk<<endl;
file.close();
{
char ch;
ifstream file;
file.open("computerspec.txt",ios::nocreate);
while(file)
{
file.get(ch);
cout<<ch;
}
getch();
end:
file.close();
}
getch();
}
Code reformatted and tags added. -Narue