Need Some Help!

This is a discussion on Need Some Help! within the C++ Programming forums, part of the General Programming Boards category; Accually i need to create a student database but i have a problem when i try to add a new ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    13

    Need Some Help!

    Accually i need to create a student database but i have a problem when i try to add a new student, it overwrites on the old student data so, when i choose display, only the last student that i have entered will appear !! ..

    this is what i have done so far ...

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <fstream>
    
    using namespace std;
    
    
    void add();
    void display();
    
    struct student{
           char fname[100];
           char lname[100];
           int id;
           float cgpa;
               };
               
    int main(int argc, char *argv[])
    {
      int x;
      
     
     do{
      cout <<"============================\n";
      cout <<"            Menu            \n";
      cout <<"============================\n";
      cout <<endl;
      cout <<"={1}= Add a new student.\n";
      cout <<"={2}= Display all\n";
      cout <<"={3}= Exit\n";
      
      cout <<"============================\n";
      
      
      cout <<"Your Choice: ";
      cin >>x;
      
      switch (x){
      case 1 : add();break; 
      case 2 : display();break;
      case 3 : ;break;
      
      default : cout <<"Invalid Choice\n";
      }
      }while (x != 3);
      
      system("PAUSE");	
      return 0;
    }
    
    
    
    // Add a new student to the database
    void add()
    {
      
      student info[200];
      int i;
      
     
      
      
      cout << "Enter student's first name: ";
      cin >>info[i].fname;
      
      cout << "Enter student's second name: ";
      cin >>info[i].lname;
      
      cout <<"Enter the student id: ";
      cin >> info[i].id;
      
      cout << "Enter the student CGPA: ";
      cin >> info[i].cgpa; 
      
     
            ofstream fout;
            fout.open("datab.bin",ios::out|ios::binary);
            fout.write(reinterpret_cast<char*>(&info[i]),sizeof(info));
            fout.close();
      
      
    }
    
    void display()
    {
    student info[200];
    int i;
     
            ifstream fin; 
            fin.open("datab.bin",ios::in|ios::binary);
            fin.read(reinterpret_cast<char*>(&info[i]),sizeof(info));
            
            fin.close();
           
            
            cout << "Name: "<<info[i].fname<<" "<<info[i].lname<<endl;
            cout << "ID: "<<info[i].id<<endl;
            cout << "CGPA: "<<info[i].cgpa<<endl;
    }

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    just after a quick look, I suggest you try this:
    Code:
        ofstream fout;
        fout.open("datab.bin",ios::out|ios::binary|ios::app);
        fout.write(reinterpret_cast<char*>(&info&#091;i&#093;),sizeof(info));
        fout.close();
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    You need to open your ofstream with the ios::app option so that you'll append to your existing file rather than rewrite its contents.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    New Zealand
    Posts
    20
    It seems scary to me that you declare an array of students, index them with 'i' without ever setting 'i' to a number!

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    13
    thanx guyz for the help .. but i still don't get it .. (still bigenner)

    I want to save an array of structure in a binary file and then display ALL the information i have saved.
    I have been sittin on this chair for 2 dayz but i couldn't solve it. can u guyz help me PLZ ..
    thanX....

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    13
    This is my code .. accually with this code i can only save one student data .. i want to save more than one and display them all ....

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <fstream>
    
    using namespace std;
    
    
    int add();
    void display();
    void save();
    void load();
    
    struct student{
           char fname[100];
           char lname[100];
           int id;
           float cgpa;
               };
               
    int main(int argc, char *argv[])
    {
      int x;
      
     
     do{
      cout <<"============================\n";
      cout <<"            Menu            \n";
      cout <<"============================\n";
      cout <<endl;
      cout <<"={1}= Add a new student.\n";  
      cout <<"={2}= Display all\n";
      cout <<"={3}= Save\n";
      cout <<"={4}= Load\n";
      cout <<"={5}= Exit\n";
      cout <<"============================\n";
      
      
      cout <<"Your Choice: ";
      cin >>x;
      
      switch (x){
      case 1 : add();break; 
      case 2 : display();break;
      case 3 : save();break;
      case 4 : load();break;
      case 5 : ;break;
      default : cout <<"Invalid Choice\n";
      }
      }while (x != 5);
      
      system("PAUSE");	
      return 0;
    }
    
    //=====================================================
    student info[200];
    int i,num;
    //============================================================
    // Add a new student to the database
    int add()
    {
        
      cout << "Enter student's first name: ";
      cin >>info[i].fname;
      
      cout << "Enter student's second name: ";
      cin >>info[i].lname;
      
      cout <<"Enter the student id: ";
      cin >> info[i].id;
      
      cout << "Enter the student CGPA: ";
      cin >> info[i].cgpa; 
      
             
    }
    //===============================[ display ]=======================
    void display()
    {
            
              
            cout << "Name: "<<info[i].fname<<" "<<info[i].lname<<endl;
            cout << "ID: "<<info[i].id<<endl;
            cout << "CGPA: "<<info[i].cgpa<<endl;
    
            
    }
    //========================[ save ]=============================
    void save()
    {
            ofstream fout;
            fout.open("datab.txt",ios::out|ios::binary);
            fout.write(reinterpret_cast<char*>(&info[i]),sizeof(student));
            fout.close();
            
    }
    //=======================[load]=================================
    void load()
    {
            ifstream fin; 
            fin.open("datab.txt",ios::in|ios::binary);
            fin.read(reinterpret_cast<char*>(&info),sizeof(student));       
            fin.close();        
    }

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Quote Originally Posted by joshdick
    You need to open your ofstream with the ios::app option so that you'll append to your existing file rather than rewrite its contents.
    That means do this:
    Code:
    fout.open("datab.txt",ios::out|ios::app);
    The app means append.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    sorry for interrupting but it seems youve posted the same subject 3 times but with different aproaches, why not keep it in the same thread? lol such a c++ related question...

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