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


getch();

end:

file.close();

}

getch();
}

Your closing the file after you've written one set of data to it. Normally what happens is that the file will be over written unless you either,
1 - keep it open in which case put file.close after the ending of the while loop 2- close it but in an appending state.

I'd prefere number 1

plz explain it i didn't understand. plz tell smething more abt my problm.

>plz tell smething more abt my problm.
How about we skip the part where I call your code crap with examples and I just give you a better version:

#include <iostream>
#include <fstream>

using namespace std;

struct computerspec {
  int ram;
  int hdisk;
}compuspec;

int main()
{
  char ch;
  ofstream out("computerspec.txt");

  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;

    out<<"ComputerSpec attributes/Data members\n";
    out<<"RAM"<<"\t"<<"Hard disk"<<endl;
    out<<compuspec.ram<<"\t"<<compuspec.hdisk<<endl;

    cout<<"Press 'y' if u wanna continue"<<endl;
    while (cin.get(ch) && ch != '\n')
      ;
    cin.get(ch);
    cout<<endl;
  } while(ch=='y'||ch=='Y');

  out.close();

  ifstream in("computerspec.txt"); 

  while(in.get(ch)) 
    cout<<ch; 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.