Thread: code wont run plz help structs

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

    Unhappy code wont run plz help structs

    Hello everyone i'm new to the site and i've been reading through the tutorials on this site as a new programmer to C++. I'm in need of some help with structs.
    I'm trying to make a program where it asks the user for the employee First initial, last initial, age, and ID. The ID will increment each time an employee is added.
    Here is the code i have so far:

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct database; {
        char Finitial;
        char Linitial;
        int age;
        int ID;
    }
    
    int main() {
    
    database employee;
    char YN;
    int counter;
    
    while (YN=='y') {
    
        cout<<"Please enter your first Initial\n";
        cin>>employee.Finitial;
        cout<<"Please enter your last Initial\n";
        cin>>employee.Linitial;
        cout<<"Please enter your age\n";
        cin>>employee.age;
        employee.ID=counter;
        counter=counter++;
        cout<<"Would you like to enter a new employee\n";
        cin>>YN;
        }
    cout<<employee.Finitial;
    cout<<employee.Linitial;
    cout<<employee.age;
    cout<<employee.ID;
    return 0;
        }
    Last edited by real138; 02-21-2010 at 06:17 PM.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    And what's the problem?

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    6
    o woops im sorry i forgot to insert the errors.

    C:\Users\robert\Desktop\Programming\C++\Sample Codes\Testing.cpp|12|error: new types may not be defined in a return type|

    C:\Users\robert\Desktop\Programming\C++\Sample Codes\Testing.cpp|12|note: (perhaps a semicolon is missing after the definition of `database')|

    C:\Users\robert\Desktop\Programming\C++\Sample Codes\Testing.cpp|12|error: extraneous `int' ignored|

    C:\Users\robert\Desktop\Programming\C++\Sample Codes\Testing.cpp|12|error: `main' must return `int'|
    ||=== Build finished: 3 errors, 0 warnings ===|
    All these errors point to the beginning of the function
    int main ();
    Last edited by real138; 02-21-2010 at 06:08 PM.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    The compiler is telling you what is wrong.

    C:\Users\robert\Desktop\Programming\C++\Sample Codes\Testing.cpp|12|note: (perhaps a semicolon is missing after the definition of `database')|
    Woop?

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    C:\Users\robert\Desktop\Programming\C++\Sample Codes\Testing.cpp|12|note: (perhaps a semicolon is missing after the definition of `database')|
    Did you try to understand the error message?

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    6
    ok thanks but i still get 2 errors these ones.

    C:\Users\robert\Desktop\Programming\C++\Sample Codes\Testing.cpp|5|error: expected unqualified-id before '{' token|
    C:\Users\robert\Desktop\Programming\C++\Sample Codes\Testing.cpp||In function `int main()':|

    C:\Users\robert\Desktop\Programming\C++\Sample Codes\Testing.cpp|14|error: aggregate `database employee' has incomplete type and cannot be defined|
    ||=== Build finished: 2 errors, 0 warnings ===|
    plz help with these also i spent like 2 hours trying to fix it.

    Also thanks for the quick response guys really appreciate it.
    Last edited by real138; 02-21-2010 at 06:22 PM.

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Well aside from the most glaring error at the top:
    Code:
    struct database; {
        char Finitial;
        char Linitial;
        int age;
        int ID;
    }
    Which should be...
    Code:
    struct database {
        char Finitial;
        char Linitial;
        int age;
        int ID;
    };
    it looks like the code keeps asking the user for more information and then never does anything with it (except for sticking it into the same structure over and over until the user enters a "n" where it spits out the last data entered)...

    What did you *intend* for this to do?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    There is another semi-colon in front of the struct.

    Plus your logic is flawed for multiple employees. Every time one is entered it will overwrite the existing data. You will need to use an array or preferable a vector to allow for multiple employees.
    Woop?

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    6
    i was just told that that should be there by 2 ppl above you...
    well the code isn't finished i was hoping to get the basic down and then expand it to what i wanted. I'm gonna make employee an array and it's gonna store each employee. then when the user is done inputting employees it will show the employee's inputted. and then later ill add a search for employees.

  10. #10
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Real: maybe you have been told this and if so, ignore this tip but every compiler on the planet has the issue of the error-snowball effect: The first error you see is always the most important one and the errors that follow it tend to become more and more useless as they go on. Fix the first error and recompile and many times, errors later in the code vanish like a puff of smoke...when faced with a stack of errors from the compiler, trying to fix them all in one go is often an exercise in frustration, usually resulting in new and colorful curse-words being invented..
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    6
    yes i know thanks jeff but im getting mixed review about the semicolon after the database. so im really confused now what the problem is.

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by real138 View Post
    yes i know thanks jeff but im getting mixed review about the semicolon after the database. so im really confused now what the problem is.
    Jeff gave you the answer on how to fix the struct definition. The compiler hinted to you that there should be a semicolon after the definition of database, not after the word database itself. Noone here has said otherwise.
    Also
    Code:
    char YN;
    while (YN=='y') {
    ....
    }
    Because YN is not set to 'y' before the loop then the code inside the while statement will never be run.

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    6
    OO i got it thanks for not giving me the answer guys and just hinting at what needed to be done thanks again. i needed to put it after the whole struct. and i didnt properly look at jeffs code T_T sorry about that guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 10-18-2004, 09:14 AM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  4. structs, array, code has problems, Im lost
    By rake in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2002, 02:02 AM