help

This is a discussion on help within the C++ Programming forums, part of the General Programming Boards category; can you some help me my file is not compile i got this example from the proffesor in class i ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    help

    can you some help me my file is not compile i got this example from the proffesor in class i dont really understand what it does can someone explain it to me. all i know it is supposed to open a file and write into it.
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<iostream>
    #include<fstream>
    using namespace std;
    class player
    {
          char name[21];
          int score;
          
          public:
                 friend istream &operator >>(istream cin, player &p)
                 {
                        cout<<"player name =>";
                        cin.getline(p.name,21);
                        cout<<"score=";
                        cin>>p.score;
                        return cin;
                 };
                 friend ostream & operator<<(ostream &cout, player &p)
                 {
                 cout<<p.name<<"score="<<p.score<<endl;
                 return cout;
                 };
                        
               
    };
    main()
    {
          int count =0; 
          player p;
          ofstream file1("c:score.doc",ios::out);
          if(file1==0)
          {
          cout<<"file opening eor";
          getch();
          exit(1);
          }
          cout<<"enter player data or ctrl-z to stop";
         cin>>p;
         file1.write((char*)p,sizeof(player));
        
          getch();
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    First, you really need to start coming up with better thread titles. Better than "help".
    How To Ask Questions The Smart Way

    > cin>>p;
    > file1.write((char*)p,sizeof(player));
    You have two overloaded operators in your class. You used >>, maybe you should use << as well?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21