Noobie question, all help greatly appreciated!

This is a discussion on Noobie question, all help greatly appreciated! within the C++ Programming forums, part of the General Programming Boards category; I just started learning C++, and after doing some smaller programs, I decided to make a blind hack at a ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    4

    Noobie question, all help greatly appreciated!

    I just started learning C++, and after doing some smaller programs, I decided to make a blind hack at a crude RPG type game (a thing I like to do with new programming languages). There are some things that are happening that I can't understand and I need your help. Here is the code (some pieces omitted):
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int u_level;
    int u_exp;
    int u_hp;
    //other global variables named
    
    
    //nothing was omitted in following function
    void Startup ()
    {
        cout<<"It has begun\n";
        system("Pause"); //pause works here
        cout<<"Did you doubt it?\n";
        system("Pause"); //pause works here
        system("cls");
        
        int selection;
        
        cout <<"1. New Game" <<"\n";
        cout <<"2. Load Game" <<"\n";
        cout <<"3. Exit" <<"\n";
        cout <<"Please enter the number of your selection: ";
        cin >> selection
        cout << selection //added for debugging, does NOT out put the variable
        system("PAUSE") //added for debugging, does NOT pause
        switch (selection) {
        case 1:
             cout << "Your in case 1"; //added for debugging, does NOT display
             system("Pause"); //added for debugging, does NOT pause
             break;
        case 2:
             //SET UP SAVING SYSTEM
             break;
        case 3:
             cout << "Thanks for playing!" <<"\n"; //displayed breifly
             system("Pause"); //Pause does not work
             system("exit");
             break;
        default:
             cout<< "Enter a number listed above.";
        }
    }
    
    
    int main()
    {
        Startup (); 
        cout<<"It has been a while young one\n";
        cout<<"my memory is beginning to fade.\n";
        system("Pause");
        cout<<"What is your name again?";
        //more lines follow
    }
    So, whenever I run the program void Startup () is automatically called, even when I comment out the definition line AND the line where it is called in int main(). Also, as commented above, several of the pauses don't work. My desired result is for the function to be called, run, and if the user selects 1 then the program will continue in int main(), and if they select 3, then it will display Thanks for playing!, pause and exit. But, when the user selects 1 the program closes with no text being displayed (as commented) And, when the user selects 3 the program will briefly display the text, then close. So, why is it happening like it is? All thoughts and opinions about the code and my question are greatly appreciated. Also, if there any questions about what I am asking, or you want to see more code, just ask.

    thanks in advance
    ---axehero

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Everything works fine for me (after adding a few missing semicolons). Did you try re-compiling?

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I don't know if this has to do with your problem, but the code you posted is missing quite a few semicolons:
    Code:
    >    cin >> selection
    >    cout << selection //added for debugging, does NOT out put the variable
    >    system("PAUSE") //added for debugging, does NOT pause
    Every one of these lines needs a semicolon to end the statement.

    As for your other problem, if Startup() is commented, it shouldn't be called, so somehow the code isn't being recompiled before you try running it.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Are you sure your changes are being compiled? If you comment out something and it still runs, chances are that you are running an old version of the executable.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    Yes, if you make the previously mentioned changes it should work.

    If you use cout, and then want to clear the screen, you should always first flush the stream with
    Code:
    cout<<flush;
    The system call to exit doesn't work for me unless I flush cout. One thing you could consider doing instead is having the startup function return an integer. If the user quits in the function you can return zero, otherwise return nonzero. This can be used as follows:
    Code:
    int Startup ()
    {
        cout<<"It has begun\n";
        system("Pause");
        cout<<"Did you doubt it?\n";
        system("Pause");
        cout << flush;
        system("cls");
        
        int selection;
        
        cout <<"1. New Game" <<"\n";
        cout <<"2. Load Game" <<"\n";
        cout <<"3. Exit" <<"\n";
        cout <<"Please enter the number of your selection: ";
        cin >> selection;
        cout << selection;
        system("PAUSE");
        switch (selection) {
        case 1:
             return 1;
             break;
        case 2:
             return 2;
             break;
        case 3:
             cout << "Thanks for playing!" <<"\n";
             system("Pause");
             return 0;
             break;
        default:
             cout<< "Enter a number listed above.";
        }
    }
    
    
    int main()
    {
        if(!Startup())
    	return 0;
        cout<<"It has been a while young one\n";
        cout<<"my memory is beginning to fade.\n";
        system("Pause");
        cout<<"What is your name again?";
        //more lines follow
    }

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    I have tried all suggestions, and the program still isn't working. I am stumped here...

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    Is your compiler throwing any errors for you? You should have received some errors atleast for missing semi-colons. If not than it is definently your compiler not running. What programming environment are you using? ex: Dev C++(What program are you using to create C++ files).

    Also try making sure that when you go to try it, that you are saying Compile and Run, or something similiar, not just run.
    My Favorite Programming Line:
    Code:
    #define true ((rand() % 2) ? true : false)

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    I am using Dev-C++ version 4.9.9.2 if that helps at all...

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    HAHAA! I figuered it out. I told you all I was a noob, and I proved it. I didn't know that if the compile found errors, it wouldn't run the version with errors. Thanks all for the help though!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 11:13 AM
  2. Question about binary trees and files
    By satory in forum C Programming
    Replies: 9
    Last Post: 03-06-2006, 05:28 AM
  3. noobie loop question
    By sandingman1 in forum C++ Programming
    Replies: 12
    Last Post: 12-24-2005, 03:13 PM
  4. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 02:52 PM
  5. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM

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