Thread: structs tutorial question?

  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 11:57 PM.
    Compiler in use: MinGW

    OS: Windows ME

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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,663
    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 11: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, 05: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, 02:49 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM