structs tutorial question?

This is a discussion on structs tutorial question? within the C++ Programming forums, part of the General Programming Boards category; Im compiling in dev c++ 5 beta Code: #include <iostream> struct database { int id_number; int age; float salary; }; ...

  1. #1
    Registered User code2d's Avatar
    Join Date
    Nov 2006
    Location
    Canada
    Posts
    87

    structs tutorial question?

    Im compiling in dev c++ 5 beta

    Code:
    #include <iostream>
    
    struct database {
      int id_number;
      int age;
      float salary;
    };
    
    int main()
    {
      database employee;  //There is now an employee variable that has modifiable 
                          // variables inside it.
      employee.age = 22;
      employee.id_number = 1;
      employee.salary = 12000.21;
    return 0;
    }
    When I compile and run, it boot's the console app window and then exits.
    I believe its just my bad beginners programming.
    Last edited by code2d; 01-06-2007 at 10:57 PM.
    Compiler in use: MinGW

    OS: Windows ME

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User rynoon's Avatar
    Join Date
    Dec 2006
    Location
    London, ON
    Posts
    26
    Add std::cin.get() after your your last statement in main and it will wait for user input before it exits.

    return 0;

    Add that too.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by rynoon
    return 0;

    Add that too.
    This is optional in main. If return statement in the main function is missing the 0 is returned anyway
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User code2d's Avatar
    Join Date
    Nov 2006
    Location
    Canada
    Posts
    87
    I added return 0 and it compiles; but std::cin.get() I do not now were to put and anywere I do it gives me errors?
    Compiler in use: MinGW

    OS: Windows ME

  6. #6
    Registered User rynoon's Avatar
    Join Date
    Dec 2006
    Location
    London, ON
    Posts
    26
    Quote Originally Posted by code2d
    I added return 0 and it compiles; but std::cin.get() I do not now were to put and anywere I do it gives me errors?
    Before your return statement.
    Courage, my friends; 'tis not too late to build a better world. - Tommy Douglas

  7. #7
    Registered User code2d's Avatar
    Join Date
    Nov 2006
    Location
    Canada
    Posts
    87
    When I do that if gives me these in the error log
    struct.cpp: In function `int main()':
    struct.cpp:17: error: expected `;' before "return"

    Execution terminated

    Code:
    #include <iostream>
    
    struct database {
      int id_number;
      int age;
      float salary;
    };
    
    int main()
    {
      database employee;  //There is now an employee variable that has modifiable 
                          // variables inside it.
      employee.age = 22;
      employee.id_number = 1;
      employee.salary = 12000.21;
    std::cin.get() 
    return 0;
    }
    Compiler in use: MinGW

    OS: Windows ME

  8. #8
    Registered User rynoon's Avatar
    Join Date
    Dec 2006
    Location
    London, ON
    Posts
    26
    What does every line of your code have in common except for std::cin.get()?
    Courage, my friends; 'tis not too late to build a better world. - Tommy Douglas

  9. #9
    Registered User code2d's Avatar
    Join Date
    Nov 2006
    Location
    Canada
    Posts
    87
    ......lost??? I must be stupid or something lol...
    Compiler in use: MinGW

    OS: Windows ME

  10. #10
    Registered User code2d's Avatar
    Join Date
    Nov 2006
    Location
    Canada
    Posts
    87
    ow I see ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; it compiles know.
    am I suppost to get anything on the screen because i am not....
    Compiler in use: MinGW

    OS: Windows ME

  11. #11
    Registered User rynoon's Avatar
    Join Date
    Dec 2006
    Location
    London, ON
    Posts
    26
    Quote Originally Posted by code2d
    ow I see ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; it compiles know.
    am I suppost to get anything on the screen because i am not....
    Well, you're not outputting anything in your code. You're just assigning values to the variables in your employee object.
    Courage, my friends; 'tis not too late to build a better world. - Tommy Douglas

  12. #12
    Registered User code2d's Avatar
    Join Date
    Nov 2006
    Location
    Canada
    Posts
    87
    Well, you're not outputting anything in your code. You're just assigning values to the variables in your employee object.
    That is strangly what I thought so i must be on the right path lol

    For anyone that might be interested here is the final working code.
    Code:
    #include <iostream>
    
    struct database {
      int id_number;
      int age;
      float salary;
    };
    
    int main()
    {
      database employee;  //There is now an employee variable that has modifiable 
                          // variables inside it.
      employee.age = 22;
      employee.id_number = 1;
      employee.salary = 12000.21;
    
      std::cin.get(); 
    
    return 0;
    }
    Thx for all the help guys.
    Compiler in use: MinGW

    OS: Windows ME

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    Generally, when you write a program, at the end of the program you output something to the console window to prove that your code actually did what you wanted it to. So, you might want to try outputing the employee's various member variables.

    ...
    Last edited by 7stud; 01-08-2007 at 10:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Structs Question
    By DarkDot in forum C++ Programming
    Replies: 3
    Last Post: 04-04-2007, 01:10 PM
  2. tutorial quizz question for switch operator
    By asem0525 in forum C Programming
    Replies: 7
    Last Post: 11-18-2006, 04:56 PM
  3. Question about small tutorial closing
    By Matamoros123 in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2006, 09:55 AM
  4. Problem with tutorial (Vector class)
    By OdyTHeBear in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2002, 01:49 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

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