944,150 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2052
  • C++ RSS
May 2nd, 2005
0

Check the code!!

Expand Post »
Hye.
i write this code to get inpur from user such as ram and hard disk capacity as integer type using structures. After that i want to create a file in which i want to save that data. In the code below it is getting input from user but not creating file. Plz tell where is my mistake.
Code is:

#include <conio.h>
#include <iostream.h>
#include <fstream.h>

void main()
{
struct computerspec
{
int ram;
int hdisk;
}compuspec;

cout<<"Enter the RAM capacity"<<endl;
cin>>compuspec.ram;


cout<<endl;
cout<<"Enter the Hard Disk capacity"<<endl;
cin>>compuspec.hdisk;


cout<<endl;

ofstream file;

file.open("computerspec.txt");

file<<"ComputerSpec attributes/Data members\n";

file<<"RAM: "<<compuspec.ram<<endl;

file<<"Hdisk: "<<compuspec.hdisk<<endl;

file.close();

getch();

}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rose Aashii is offline Offline
9 posts
since Apr 2005
May 2nd, 2005
0

Re: Check the code!!

file.open("computerspec.txt");

You need to check if the file is open so
C++ Syntax (Toggle Plain Text)
  1. if (file.fail())
  2. {
  3. cerr<<"error opening file"<<endl;
  4. exit(1);
  5. }
  6. else
  7. cout << "File opened for processing" <<endl;

the chances are you havent created that file on the hdd or even if you have it may be in the wrong directory
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
May 2nd, 2005
0

Re: Check the code!!

Code tags please. http://daniweb.com/techtalkforums/announcement8-3.html.
Also it is int main not void main.
Reputation Points: 14
Solved Threads: 4
Junior Poster
prog-bman is offline Offline
108 posts
since Nov 2004
May 5th, 2005
0

Re: Check the code!!

Thread closed. Please don't double-post.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: problem with fstream
Next Thread in C++ Forum Timeline: Unix - The make tool





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC