My program crashes :|

This is a discussion on My program crashes :| within the C++ Programming forums, part of the General Programming Boards category; And I don't know why. I'm a bit of a newbie to coding and I know some C/C++. I'm playing ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Location
    Derbyshire, England.
    Posts
    5

    My program crashes :|

    And I don't know why. I'm a bit of a newbie to coding and I know some C/C++.
    I'm playing around with classes and objects, trying to get the hang of them, so I'm trying to create a database that keeps records of students-their ID number, their first and last names, and their mark. This worked fine, until I introduced a feature to allow the user to enter an ID number to look up a student, and for some reason my program crashes after displaying "ID number " with an error message.
    So, without further ado, here is my code. Sorry if I've posted more than was nessesary, but feedback on what how I've done any thing would be appreciated
    Code:
    #include <iostream>
    using namespace std;
    class student
    {
        public:
        int id,mark;
        char fname[20],lname[20],grade; 
        void assignfname()
        {
            cout << "Enter the pupil's first name" << endl;
            cin >> fname;
        };
        void assignlname()
        {
            cout << "Enter the pupil's last name" << endl;
            cin >> lname;
        };
        void assignid()
        {
            cout << "Enter the studant's I.D. number" << endl;
            cin >> id;
        };
         void marki()
        {
            cout << "Input the pupil's mark" << endl;
            cin >> mark;
        };
        void display()
        {
            cout << "I.D.      \t" << id << endl;
            cout << "First name\t" << fname << endl;
            cout << "Last name \t" << lname << endl;
            cout << "Mark      \t" << mark << endl;
        }
    };
    
    int main()
    {
        int db;
        student studentdb[1000];
        for(int x=0;x<2;x++)
        {
            studentdb[x].assignfname();
            studentdb[x].assignlname();
            studentdb[x].assignid();
            studentdb[x].marki();  
            system("cls");
        }
        cout << "Enter the id number of the database you wish to view" << endl;
        cin >> db;
        studentdb[db].display();
        system("PAUSE");
    }
    Thanks in advance!

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    you have colon's after the }'s. Delete them. Except for the one after the entire class definition.

    Code:
    class A
    {
       function B { ...do something; }
    };

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    Well, the id number recorded of the student is different from the "the id number of the database you wish to view". You should be searching the array (the used portion of the array) for the student with the requested id number.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2007
    Location
    Derbyshire, England.
    Posts
    5
    Do'h!
    Would I do that with some if/elses?
    And thanks for the suggestion, indigo, I've deleted those semi-colons.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    Would I do that with some if/elses?
    With only two entries that approach will work. With all 1000 possible entries, I fear that you will find that rather tedious. Use a loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2007
    Location
    Derbyshire, England.
    Posts
    5
    Hmm. Code is as above, but I added this do loop:
    Code:
    cout << "Enter the id number of the database you wish to view" << endl;
        cin >> db;
        for(int x=0;x<2;x++)
        {
            if(studentdb[x].id=db)
            {
                studentdb[x].display();
                break;  
            }  
        }
    Problem is, it only displays the ID number I entered, the other variables displayed are the ones in the first element. What causes this?

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Use ==, not = to do comparisons.

  8. #8
    Registered User
    Join Date
    Jun 2007
    Location
    Derbyshire, England.
    Posts
    5
    Gah!
    I'm such a noob Thanks.

  9. #9
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Firstly:

    Code:
    if(studentdb[x].id == db) // comparison: == NOT =
    Why it only displays the ID is a question to me too.

    Edit: MacGyver beats everyone!
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Because of the single =, it's equivalent to this.
    Code:
        for(int x=0;x<2;x++)
        {
            studentdb[x].id=db;
            if(studentdb[x].id != 0)
            {
                studentdb[x].display();
                break;  
            }  
        }
    So as long as db!=0, the if will execute.

    [edit] So the program probably uses the ID that was entered, but that loop uses the first student for all of the other data. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Jun 2007
    Location
    Derbyshire, England.
    Posts
    5
    Thanks for the help, everyone
    Did I use objects okay? Were there any problems with my class?

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Well, declaring an array of 1000 elements is overkill when you only access the first two elements. I see that your ID can be over 2, which is probably why you do that. But elements 3..999 are not initialized (since you don't have a constructor), so accessing them is probably useless anyway.

    The best way to fix this would probably be to limit db (in main()) from 0 to 1, or whatever the maximum number is.

    [edit] Also, since you have declared the member functions in the class inside the actual class, they are inline. (Well, it's like adding the inline keyword to a function: you suggest to the compiler that the function be inline.) This can be a bad thing for large member functions. The alternative is to declare the functions outside the class:
    Code:
    class c {
    public:
        void func();
    };
    
    void c::func() {
        // ...
    }
    BTW, studant is spelled student. [/edit]
    Last edited by dwks; 06-27-2007 at 11:50 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    Did I use objects okay? Were there any problems with my class?
    Typically we would declare member variables as private as that makes it easier to change the implementation later without changing the public interface of the class.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple login program - Crashes :(
    By spadez in forum C Programming
    Replies: 1
    Last Post: 03-23-2009, 04:16 PM
  2. Replies: 3
    Last Post: 02-29-2008, 12:29 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. program crashes on closure.
    By ssjnamek in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2005, 04:55 PM
  5. My program crashes with this code
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-27-2002, 11:28 PM

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