Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 28th, 2006, 8:14 PM   #1
c_newbie
Newbie
 
Join Date: Dec 2005
Posts: 14
Rep Power: 0 c_newbie is on a distinguished road
using classes, & fstream

hi . i am trying to output to a file using ofstream .. i'm down to 3 errors now : 'out' uses undefined class 'std::basic_ofstream<_Elem,_Traits>' also 'initializing' : cannot convert from 'const char [7]' to 'int' and error C2228: left of '.write' must have class/struct/union.
any help would be appreciated.
the code is below:


#include<fstream.h>
#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;

class Person
  {
   private:
    int idNum;
    char name[10];
   public:
    Person(const int id, const char nm[]);
    friend ostream& operator<<(ostream& out,const Person &p);
    friend istream& operator>>(istream& in, Person &p);
	ostream& personDisplay(ostream &s);
  };
Person::Person(const int id = 000, const char nm[]="")
{
 idNum = id;
 strcpy(name,nm);
}
ostream& operator<<(ostream& out,const Person &p)
  {
    out<<p.name<<p.idNum<<endl;
    return(out);
  }
ostream& Person::personDisplay(ostream &s)
{
  cout<<"Here is a person: ";
  cout.setf(ios::left);
  cout.width(12);
  return(s);
}
istream& operator>>(istream& in, Person &p)
{
  cout<<"Enter ID Number ";
  cin>>p.idNum;
  cout<<"Enter name ";
  cin>>p.name;
  return(in);
 }

void main()
{
  Person aPerson;
  ofstream out("person.dat");   // change path if necessary
  cin>>aPerson;
  out.write((char*)(&aPerson), sizeof(aPerson));
}
c_newbie is offline   Reply With Quote
Old Mar 28th, 2006, 8:44 PM   #2
Bench
Newbie
 
Join Date: Feb 2006
Posts: 20
Rep Power: 0 Bench is on a distinguished road
Change your #include files to C++ standard ones, Also change void main() to int main(). Finally, You can remove the stdafx.h if you like. You don't need that header since you can turn precompiled headers off in MS Visual C++

The following compiled with 0 errors in MSVC++ 2003 .NET
#include<fstream>
#include<iostream>
#include<conio.h>
#include<cstring>
using namespace std;

class Person
  {
   private:
    int idNum;
    char name[10];
   public:
    Person(const int id, const char nm[]);
    friend ostream& operator<<(ostream& out,const Person &p);
    friend istream& operator>>(istream& in, Person &p);
	ostream& personDisplay(ostream &s);
  };
Person::Person(const int id = 000, const char nm[]="")
{
 idNum = id;
 strcpy(name,nm);
}
ostream& operator<<(ostream& out,const Person &p)
  {
    out<<p.name<<p.idNum<<endl;
    return(out);
  }
ostream& Person::personDisplay(ostream &s)
{
  cout<<"Here is a person: ";
  cout.setf(ios::left);
  cout.width(12);
  return(s);
}
istream& operator>>(istream& in, Person &p)
{
  cout<<"Enter ID Number ";
  cin>>p.idNum;
  cout<<"Enter name ";
  cin>>p.name;
  return(in);
 }

int main()
{
  Person aPerson;
  ofstream out("person.dat");   // change path if necessary
  cin>>aPerson;
  out.write((char*)(&aPerson), sizeof(aPerson));
}

After-thought: you could have made that code a little simpler if you'd used the C++ <string> library

Last edited by Bench; Mar 28th, 2006 at 8:53 PM. Reason: Wrong include file
Bench is offline   Reply With Quote
Old Mar 28th, 2006, 11:41 PM   #3
c_newbie
Newbie
 
Join Date: Dec 2005
Posts: 14
Rep Power: 0 c_newbie is on a distinguished road
Hey Bench
thank u so much .. really appreciate it .. u have no idea
thanx!
c_newbie is offline   Reply With Quote
Old Mar 29th, 2006, 9:47 AM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You'll want the cconio header instead of conio.h too.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 29th, 2006, 11:22 AM   #5
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
I'm not sure, but I don't think there is a cconio header as conio.h is not a standard C header file.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Mar 29th, 2006, 12:22 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Nice guess, though .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 29th, 2006, 2:42 PM   #7
c_newbie
Newbie
 
Join Date: Dec 2005
Posts: 14
Rep Power: 0 c_newbie is on a distinguished road
cool ..
ok i want to print the results on the screen and i am getting an error and/or "16"
do u guys know what code i should use ... i tried :
int main()
{
  Person aPerson;
  ofstream out("person.dat");   
  cin>>aPerson;
  cout<<((char*)(&aPerson), sizeof(aPerson)); //or
  aPerson.personDisplay;
  out.write((char*)(&aPerson), sizeof(aPerson));
  getch();
}
c_newbie is offline   Reply With Quote
Old Mar 29th, 2006, 3:25 PM   #8
c_newbie
Newbie
 
Join Date: Dec 2005
Posts: 14
Rep Power: 0 c_newbie is on a distinguished road
its cool .. it was a no-brainer.. figured it out
c_newbie is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:07 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC