Thread: Lesson 5

  1. #1
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50

    Angry Lesson 5

    Hey guys. This is gettin annoying. ill post the stuff im struggling wid

    #include <iostream.h>
    #include <conio.h>
    int main()
    {
    int input;
    cout<<"1. Play game";
    cout<<"2. Load game";
    cout<<"3. Play multiplayer";
    cout<<"4. Exit";
    cin>>input;
    switch (input)
    {
    case 1: playgame();
    break;
    case 2:
    loadgame();
    break;
    case 3: //Note use of : not ;
    playmultiplayer();
    break;
    case 4:
    return 0;
    default:
    cout<<"Error, bad input, quitting";
    }
    return 0;
    }
    This program will not compile yet, but it serves as a model (albeit simple) for processing input.

    If you do not understand this then try mentally putting in if statements for the case statements. Note that using return in the middle of main will automatically end the program. Default simply skips out of the switch case construction and allows the program to terminate naturally. If you do not like that, then you can make a loop around the whole thing to have it wait for valid input. I know that some functions were not prototyped. You could easily make a few small functions if you wish to test the code.

    i dont understand hot to put a loop around it. also, do i have to include conio.h for all switch casing thankz cya
    Last edited by polonyman; 09-09-2004 at 02:45 AM.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    #include <iostream> 
    using namespace std;
    
    //FUNCTION PROTOTYPES
    void display_menu();
    ??? playgame();     
    ??? loadgame();
    ??? playmultiplayer();
    void exit_message();
    
    
    int main()
    { 
    
         int input = 0;
    
              do
              {
    
                   display_menu();
    
                   cin >> input
        
                        switch (input) 
                        { 
    
                            case 1: playgame();
                                        break; 
    
                            case 2: loadgame(); 
                                        break; 
    
                            case 3: playmultiplayer();
                                        break; 
    
                            case 4: exit_message();
    
                            default: cout<<"\n\nError, bad input, quitting\a"; 
                      
                       }
    
              }while (input != 4);
    
    return 0;
    
    }
    
    
    
    //FUNCTION DEFINITIONS
    
    void display_menu()
    {
     
         cout << endl << endl;
    
         cout << "1. Play game\n" 
              << "2. Load game\n" 
              << "3. Play multiplayer\n" 
              << "4. Exit"; 
    
         cout << endl << endl;
    
    }
    
    
    
    ??? playgame()
    {
    
         ???;
    
    }
    
    
    
    ??? loadgame()
    {
    
         ???;
    
    }
    
    
    
    ??? playmultiplayer()
    {
    
         ???;
    
    }
    
    
    
    void exit_message()
    {
    
         cout << "\n\nThank you for playing.\n\n";
    
    }
    ??? indicates where code is unknown




    1.) ".h" header files are no longer standard

    2.) conio is non-standard

    3.) don't forget to use namespace std;

    3.) function prototypes placed at the beginning of the program

    4.) initialize variables to eliminate, "garbage data" example: int input = 0;

    5.) format your code... just like if you were reading a beautiful story out of a book.

    6.) use code tags when posting your code on this forum.. (this will also help you with indenting)


    As far as flow control, you have your choice of "while", "do/while", and "for" loops to chose from. In this instance, I chose to use a do/while loop.
    Last edited by The Brain; 09-09-2004 at 05:40 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50

    confused

    sorry mate but that didn't really help me. im sure evryone was a noob at one stage but try and dumb it down a bit. i get everythin cept the loop bit.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by polonyman
    sorry mate but that didn't really help me. im sure evryone was a noob at one stage but try and dumb it down a bit. i get everythin cept the loop bit.
    The construct demonstrated is called a do-while loop. Other loops are while and for loops. A do-while loop always executes at least once since the conditional part of the code, the piece that determines if we need to run through the loop again, comes at the end rather than at the beginning as it would in a while loop. In the case above, the conditional part of the loop is input != 4, which in this case means that as long as the value stored in input is not 4 the execution of the program will continue with the code in the loop. In the code example given above, it appears that the value 4 is meant to indicate that the user has choosen the exit option from the menu.

    The code given also needs a break; statement after the call to the exit_mesage(); function call to work properly.

    Code:
    int main()
    { 
        int input = 0;
    
        do// This means to do everything below in a loop...
        {
            display_menu();
    
            cin >> input
        
            switch (input) 
            { 
    
                case 1: playgame();
                        break; 
    
                case 2: loadgame(); 
                        break; 
    
                case 3: playmultiplayer();
                        break; 
    
                case 4: exit_message();
                        break;
    
                default: cout<<"\n\nError, bad input, quitting\a"; 
                      
            }
    
        }while (input != 4);//...as long as input is not 4
    
        return 0;
    
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Read the code like it is a beautiful story..

    Do the stuff inside the loop while 'input' does not equal 4


    edit: good catch on my missing break.
    Last edited by The Brain; 09-09-2004 at 06:04 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Also forgot this:

    Code:
    cin >> input;
    Missing that semicolon as well.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lesson #4 - The Quiz
    By oval in forum C# Programming
    Replies: 0
    Last Post: 04-27-2006, 08:31 AM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Lesson #2 for New Comers to C#
    By oval in forum C# Programming
    Replies: 0
    Last Post: 04-18-2006, 05:36 PM
  4. now im on lesson 5
    By NiVaG in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2004, 09:40 PM
  5. Lesson 4 of Tutorial
    By polonyman in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2004, 06:36 AM