Thread: Stuck again!

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    15

    Stuck again!

    Ok, well I got the menu set up and when the user enters a option it displays the string. Then I want the menu to reapear again so they may choose another option but i keep getting the press any key to continue comand.

    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <conio.h>
    #include <windows.h>
    using namespace std;
    
    void overview();
    void races();
    void world();
    void menu();
    
    int main()
    {
     char name[50];
     int input;
    
       cout << " Welcome to my test Proggy!" << endl;
    Sleep(1000);
       cout << " What is your name?" << endl;
       cin.getline(name, 50, '\n');
       cout << " Hello " << name;
    Sleep(2000);
       cout << endl << " Please choose one of the options below ";
       cout << endl;
       cout << " 1. Overview " << endl;
       cout << " 2. Races." << endl;
       cout << " 3. World." << endl;
       cin >> input;
    
      switch (input)
    {
       case 1: overview ();
    	   break;	 
       case 2: races ();
           break;
       case 3: world ();
    	   break;
    
      default: 	  
            cout<<"Error, bad input, quitting";	
    }
    
       void menu();
    
     system("pause");
    
     return 0;
    }
    
     void overview()
    {
       cout << " An Overview of DragonWars:" << endl;
       cout << " DragonWars (Not official) is a MultiPlayer RPG where" << endl;
       cout << " the player can choose between four Races: Draconians," << endl;
       cout << " Humans, Elves and Drow. The abyssal forces (Deamons)" << endl;
       cout << " are approching over the lands and upsetting the" << endl;
       cout << " balance between good and evil. The Draconians are" << endl;
       cout << " at a constant war with the abyssal forces to protect" << endl;
       cout << " their homelands which border the portals between " << endl;
       cout << " the two planes of exsistence. The elves are fighting" << endl;
       cout << " thier own battles with the drows and the abyssal" << endl;
       cout << " forces are slowly advancing to their homes. The human" << endl;
       cout << " race have been corupted by the abyssal forces and have" << endl;
       cout << " grown week. The Drows are at constant war with the" << endl;
       cout << " Elves and although they are evil they do not support" << endl;
       cout << " the Abyssal forces. Where as many individuals seek" << endl;
       cout << " thir own paths and adventures weather it be for sake" << endl;
       cout << " of their race's future or their personal gain. The" << endl;
       cout << " play can choose from several diffrent classes such as" << endl;
       cout << " Mages, Thaums, Wariors, Archers and crafters." << endl;
    }
    
     void races()
    {
       cout << " An Overview of the races";
    
    }
    
     void world()
    {
        cout << " An Overview of the World";
    
    }
    
     void menu()
    {
     int input;
    
    
       cout << " Please Choose one of the following:";
       cout << endl;
       cout << " 1. Overview " << endl;
       cout << " 2. Races." << endl;
       cout << " 3. World." << endl;
       cin >> input;
    
       switch (input)
    {
       case 1: overview ();
    	   break;	 
       case 2: races ();
           break;
       case 3: world ();
    	   break;
    
      default: 	  
            cout << " Error, bad input, quitting";
    }
    
    }

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    6
    I'm pretty new at this myself, but I think you need to take the 'void' out of your function call:

    FROM
    Code:
    void menu();
    TO
    Code:
    menu();
    Last edited by mfknitz; 10-22-2003 at 04:01 AM.

  3. #3
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    You need to use an infinite loop to achive what you want. This way you can display the menu and if the user chooses to quit the program then you break from the loop and exit the program.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    here's one solution

    cool Continue = true;

    while(Continue)
    start while loop
    cout enter selection
    cout 0: exit voluntarily
    .
    .
    .
    switch
    start switch
    case 0:
    Continue = false;
    break;
    .
    .
    .
    default
    Continue = false;
    end switch
    end while loop

    Other "infinite" loops will work too.
    Last edited by elad; 10-22-2003 at 12:25 PM.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    thanks so much. I've added the while loop and removed the function menu. I do have a few tiny errors such as parse error before certin symbols. What exactly is a parse error?

    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <conio.h>
    #include <windows.h>
    using namespace std;
    
    void overview();
    void races();
    void world();
    
    int main()
    {
     char name[50];
     int input;
     continue = true;
    
       cout << " Welcome to my test Proggy!" << endl;
    Sleep(1000);
       cout << " What is your name?" << endl;
       cin.getline(name, 50, '\n');
       cout << " Hello " << name;
    Sleep(2000);
       cout << endl << " Please choose one of the options below ";
       cout << endl;
       cout << " 1. Overview " << endl;
       cout << " 2. Races." << endl;
       cout << " 3. World." << endl;
       cin >> input;
    
    
       while(continue)
    {
       cout << endl << " Please choose one of the options below ";
       cout << endl;
       cout << " 1. Overview " << endl;
       cout << " 2. Races." << endl;
       cout << " 3. World." << endl;
       cin >> input;
    }
    
    
    
      switch (input)
    {
       case 1: overview ();
    	   break;	 
       case 2: races ();
           break;
       case 3: world ();
    	   break;
    
      default: 	  
            cout<<"Error, bad input, quitting";	
    }
    
       void menu();
    
     system("pause");
    
     return 0;
    }
    
     void overview()
    {
       cout << " An Overview of DragonWars:" << endl;
       cout << " DragonWars (Not official) is a MultiPlayer RPG where" << endl;
       cout << " the player can choose between four Races: Draconians," << endl;
       cout << " Humans, Elves and Drow. The abyssal forces (Deamons)" << endl;
       cout << " are approching over the lands and upsetting the" << endl;
       cout << " balance between good and evil. The Draconians are" << endl;
       cout << " at a constant war with the abyssal forces to protect" << endl;
       cout << " their homelands which border the portals between " << endl;
       cout << " the two planes of exsistence. The elves are fighting" << endl;
       cout << " thier own battles with the drows and the abyssal" << endl;
       cout << " forces are slowly advancing to their homes. The human" << endl;
       cout << " race have been corupted by the abyssal forces and have" << endl;
       cout << " grown week. The Drows are at constant war with the" << endl;
       cout << " Elves and although they are evil they do not support" << endl;
       cout << " the Abyssal forces. Where as many individuals seek" << endl;
       cout << " thir own paths and adventures weather it be for sake" << endl;
       cout << " of their race's future or their personal gain. The" << endl;
       cout << " play can choose from several diffrent classes such as" << endl;
       cout << " Mages, Thaums, Wariors, Archers and crafters." << endl;
    }
    
     void races()
    {
       cout << " An Overview of the races";
    
    }
    
     void world()
    {
        cout << " An Overview of the World";
    
    }

  6. #6
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    I sugest putting your switch into the loop so that it actually does something, and use:
    Code:
    while (x != 4)
    and add 4. to the list as exit.
    As for a parsing error, I myself am wondering what is parsing?

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    Ok, thanks that makes alot of sence. Another question. How would I declare x? As x = 4?

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    NM, I did that. It working really well. Only qwhen i choose 4 to quit it keeps repeating the menu lol.

  9. #9
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Parsing errors are simply syntax errors. Such as forgetting a ; or ( or even a ).
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  10. #10
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    x = input; or just replace the x in the while with input

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    thanks for the tip~ ^_^ I think i remeber reading about a function that automaticly ends the program when called but I cant think of what its called?

  12. #12
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    exit(int nExitCode);

    However, this is not the best way to go about it. You should structure your program so that it flows logically, and when it exits it should return from the main() function. Post up what code you have now.

    EDIT: Until you do that, here is a simple code snippet:

    Code:
    BOOL bExit=FALSE;
    int iInput;
    
    while (bExit==FALSE)
    {
         cout << "1. Do something" << endl;
         cout << "2. Do something else" << endl;
         cout << "3. Quit" << endl;
    
         cin >> iInput;
         switch (iInput)
         {
          case 1:
              DoSomething();
              break;
    
          case 2:
              DoSomethingElse();
              break;
    
          case 3:
              bExit=TRUE;
              break;
          default:
              break;
          }
    }
    Last edited by bennyandthejets; 10-22-2003 at 04:27 PM.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  13. #13
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    Thank you. I want the program to terminate when the user hits 4

    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <conio.h>
    #include <windows.h>
    using namespace std;
    
    void overview();
    void races();
    void world();
    
    int main()
    {
     char name[50];
     int input;
     int x;
     x=input;
    
       cout << " *****************************************" << endl;
       cout << " ***    Welcome to my test Proggy!     ***" << endl;
       cout << " ***    CopyRight SheWolf (C)2003!     ***" << endl;
       cout << " ***  This is an Overview of my future ***" << endl;
       cout << " ***  RPG and a practice menu system.  ***" << endl;
       cout << " *****************************************" << endl;
    Sleep(1000);
       cout << " What is your name?" << endl;
       cin.getline(name, 50, '\n');
       cout << " Hello " << name;
    Sleep(2000);
    
       while(input)
    {
       cout << endl << " Please choose one of the options below: type 1,2,3 or 4 ";
       cout << endl;
       cout << " 1. Overview " << endl;
       cout << " 2. Races." << endl;
       cout << " 3. World." << endl;
       cout << " 4. Quit." << endl;
       cin >> input;
    Sleep(1000);
       switch (input)
    {
       case 1: overview ();
    	   break;	 
       case 2: races ();
           break;
       case 3: world ();
    	   break;
    
    
      default: 	  
            cout<<"Error, bad input, quitting";	
    }
    
    
    }
    
    
    
    
     system("pause");
    
     return 0;
    }
    
     void overview()
    {
       cout << " An Overview of DragonWars:" << endl;
       cout << " DragonWars (Not official) is a MultiPlayer RPG where" << endl;
       cout << " the player can choose between four Races: Draconians," << endl;
       cout << " Humans, Elves and Drow. The abyssal forces (Deamons)" << endl;
       cout << " are approching over the lands and upsetting the" << endl;
       cout << " balance between good and evil. The Draconians are" << endl;
       cout << " at a constant war with the abyssal forces to protect" << endl;
       cout << " their homelands which border the portals between " << endl;
       cout << " the two planes of exsistence. The elves are fighting" << endl;
       cout << " thier own battles with the drows and the abyssal" << endl;
       cout << " forces are slowly advancing to their homes. The human" << endl;
       cout << " race have been corupted by the abyssal forces and have" << endl;
       cout << " grown week. The Drows are at constant war with the" << endl;
       cout << " Elves and although they are evil they do not support" << endl;
       cout << " the Abyssal forces. Where as many individuals seek" << endl;
       cout << " thir own paths and adventures weather it be for sake" << endl;
       cout << " of their race's future or their personal gain. The" << endl;
       cout << " player can choose from several diffrent classes such as" << endl;
       cout << " Mages, Thaums, Wariors, Archers and crafters." << endl;
    }
    
     void races()
    {
       cout << " An Overview of the races";
    
    }
    
     void world()
    {
        cout << " An Overview of the World";
    
    }

  14. #14
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Using return 0; or return 1; (or any other number) inside main() will end the main function which effectively ends the program.

    Also, using exit(0); or exit(1); (or any other number) anywhere in the program will end the program.

    Usually 0 means success and non-zero means failure.

    In your case, it would NOT make as much sense to use either of these. What would make more sense would be to break out of the while loop and let the program end on its own. Since you have code after the while loop (system("pause")), I would assume that you want that code to be called.

    The way to break out of the while loop is to set continue = false; when the user selects 4. Then, the next time the while loop checks continue, it is false and so it no longer will loop. That is the more logical way of ending the program.

    [Edit] - Oops, too late. You don't need the x anymore. That was just an example. What happened to the bool continue = true; code that you used for your while loop?
    Last edited by jlou; 10-22-2003 at 04:27 PM.

  15. #15
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    Yes exit thats it! Thanks so much. It now runs smoothy. I will also try the continue false. Thanks,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM