Thread: problems running simple prog

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    7

    problems running simple prog

    Hi all, I am very new to c++, in fact I have only read a few beginners tutorials in the last 2 days, however... I do script for a game NWN which involves some c++ based programming although there are alot of similarities theres also alot of differences, Anyway I just thought I would experiment by creating a simple number guesssing game using the Dev-c++ compiler, the problem is that the program will not even run, could this be a bug or is it in the program I created?


    Code:
    ////////////////////////////////////////////////////////////////////////////////
    //:: Number guessing game
    //:: First ever c++ program written by Scott Clarke
    ////////////////////////////////////////////////////////////////////////////////
    #include <iostream>
    using namespace std;
    int NumberCheck(int nNumIGuess, int nYourNum, int nNumOfGuesses);
    int main()
    {
        int nNumIGuess = rand() % (1 - 10 + 1);
        int nYourNum;
        int nNumOfGuesses;
        cout<<"You have three guesses to work out which number I am thinking of ";
              "between 1 - 10. \nEach time you guess I will tell you if the number "
              "I am thinking of is higher or lower. ";
        while (nNumOfGuesses < 3)
        {      
            cout<<"\nGuess what number I am thinking of? ";
            cin.ignore();
            cin>>nYourNum;
            cin.get();
            NumberCheck(nNumIGuess, nYourNum, nNumOfGuesses);
        }
    }
    int NumberCheck(int nNumIGuess, int nYourNum, int nNumOfGuesses)
    {
        nNumOfGuesses++;
        if (nYourNum < nNumIGuess)
        {
            cout<<"\nIncorrect, the number I am thinking of is higher";            
        }  
        else if (nYourNum > nNumIGuess)
        {
            cout<<"\nIncorrect, the number I am thinking of is lower";
        }
        else 
        {
            cout<<"\nCorrect!!!! You win";       
        }
        return nNumOfGuesses; 
    }
    Thanks for your time.
    Scott
    Last edited by scar; 10-29-2005 at 04:44 AM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    ////////////////////////////////////////////////////////////////////////////////
    //:: Number guessing game
    //:: First ever c++ program written by Scott Clarke
    ////////////////////////////////////////////////////////////////////////////////
    #include <iostream>
    using namespace std;
    int NumberCheck(int nNumIGuess, int nYourNum, int nNumOfGuesses);
    int main()
    {
        int nNumIGuess = rand() % (1 - 10 + 1);   // I'm not gonna fix it, but this rand function
                                                  // will always return 1.
        int nYourNum;
        int nNumOfGuesses = 0; // You have to initialize this variable.
        /* Careful with your semicolons on multiline cout statements. */
        cout<< "You have three guesses to work out which number I am thinking of " <<
               "between 1 - 10. \n Each time you guess I will tell you if the number " <<
               "I am thinking of is higher or lower. ";
        while (nNumOfGuesses < 3)
        {      
            // You had alot of unnessasary cin statements in here.
            cout<<"\nGuess what number I am thinking of? ";
            cin>>nYourNum;
            NumberCheck(nNumIGuess, nYourNum, nNumOfGuesses);
        }
        cin.get();  // Trailing cin.get() cause Dev-Cpp closes when it returns a value
    }
    int NumberCheck(int nNumIGuess, int nYourNum, int nNumOfGuesses)
    {
        nNumOfGuesses++;
        if (nYourNum < nNumIGuess)
        {
            cout<<"\nIncorrect, the number I am thinking of is higher";            
        }  
        else if (nYourNum > nNumIGuess)
        {
            cout<<"\nIncorrect, the number I am thinking of is lower";
        }
        else 
        {
            cout<<"\nCorrect!!!! You win";       
        }
        return nNumOfGuesses;
    Here you go. Just fix the rand function. I'll leave it up to your logic.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    7
    Thanks bro, I will try to figure out how to use the rand function in the correct way.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    7
    Hmmm, well if I hit compile and run it won't open a command line window, this happened yesturday with a pre-written program I tried, it did not run then later I tried once again and it did run. Is anyone familiar with the Dev-C++ compiler maybe I am not using it in the correct way I guess, sorry for all of the questions but I am very eager to learn.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes, I'm familiar with Dev-C++. Is it opening and closing real quick or is it not opening at all? Because if your not getting any compiler or linker errors, then the program should open.

    Edit: Oh, if you copied my code exactly, then you'll get an error cause I didn't paste the last end brace by accident, I changed it now. If that wasn't your problem, then please answer my other question.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    7
    I had noticed the missing bracket, there were no errors, it seems like its running because if I go to run the a second time it only has an option to reset prog yet theres no program for me to see if you get what I mean, the same thing happened yesturday, one minute it would not show, the next time I tried it would but I have tried numerous times.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Hmm... check your Windows Task Manager and see if the program is open there. If it isn't then reboot or something.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    7
    Yes it was a running process in task manager and it was using nigh on 100% of my cpu usage!!!!, I have rebooted my pc and still has not helped my dilemma

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Remove the rand() function from your program temporarily and try compiling that. Your cpu might be getting hooked on that function for some reason and that's why it's eating your cpu usage.

    Edit: Dooohhh. I'm not thinking. We're returning the value of guesses, but we aren't updating the variable in main. Try compiling this;

    Code:
    ////////////////////////////////////////////////////////////////////////////////
    //:: Number guessing game
    //:: First ever c++ program written by Scott Clarke
    ////////////////////////////////////////////////////////////////////////////////
    #include <iostream>
    using namespace std;
    int NumberCheck(int nNumIGuess, int nYourNum, int nNumOfGuesses);
    int main()
    {
        int nNumIGuess;
        int nYourNum;
        int nNumOfGuesses = 0; // You have to initialize this variable.
        /* Careful with your semicolons on multiline cout statements. */
        cout<< "You have three guesses to work out which number I am thinking of " <<
               "between 1 - 10. \n Each time you guess I will tell you if the number " <<
               "I am thinking of is higher or lower. ";
        while (nNumOfGuesses < 3)
        {      
            // You had alot of unnessasary cin statements in here.
            cout<<"\nGuess what number I am thinking of? ";
            cin>>nYourNum;
            nNumOfGuesses = NumberCheck(nNumIGuess, nYourNum, nNumOfGuesses);
            cin.ignore();
        }
        cout << "You ran out of guesses.";
        cin.get();  // Trailing cin.get() cause Dev-Cpp closes when it returns a value
    }
    int NumberCheck(int nNumIGuess, int nYourNum, int nNumOfGuesses)
    {
        nNumOfGuesses++;
        if (nYourNum < nNumIGuess)
        {
            cout<<"\nIncorrect, the number I am thinking of is higher \n";            
        }  
        else if (nYourNum > nNumIGuess)
        {
            cout<<"\nIncorrect, the number I am thinking of is lower \n";
        }
        else 
        {
            cout<<"\nCorrect!!!! You win \n";       
        }
        return nNumOfGuesses; 
    }
    Last edited by SlyMaelstrom; 10-29-2005 at 06:02 AM.
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    7
    Ok I changed it to int nNumIGuess = 5; temporarily, compiled and run but still made no difference. The prog is running in processes but not in applications in the task manager, are there any other good free windows based compilers out there maybe that could help.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ok this is the whole program with working code on everything. If you have trouble with this then it's your computer. I compiled this on DevC++ and it works fine.

    Code:
    ////////////////////////////////////////////////////////////////////////////////
    //:: Number guessing game
    //:: First ever c++ program written by Scott Clarke
    ////////////////////////////////////////////////////////////////////////////////
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    using namespace std;
    int NumberCheck(int nNumIGuess, int nYourNum, int nNumOfGuesses);
    int main()
    {
        srand((unsigned)time(NULL));   
        int nNumIGuess = (rand() % 10) + 1;
        int nYourNum;
        int nNumOfGuesses = 0; // You have to initialize this variable.
        /* Careful with your semicolons on multiline cout statements. */
        cout<< "You have three guesses to work out which number I am thinking of " <<
               "between 1 - 10. \n Each time you guess I will tell you if the number " <<
               "I am thinking of is higher or lower. ";
        while (nNumOfGuesses < 3)
        {      
            // You had alot of unnessasary cin statements in here.
            cout<<"\nGuess what number I am thinking of? ";
            cin>>nYourNum;
            nNumOfGuesses = NumberCheck(nNumIGuess, nYourNum, nNumOfGuesses);
            cin.ignore();
        }
        if(nYourNum != nNumIGuess)
            cout << "You ran out of guesses.";
        cin.get();  // Trailing cin.get() cause Dev-Cpp closes when it returns a value
    }
    int NumberCheck(int nNumIGuess, int nYourNum, int nNumOfGuesses)
    {
        nNumOfGuesses++;
        if (nYourNum < nNumIGuess)
        {
            cout<<"\nIncorrect, the number I am thinking of is higher \n";            
        }  
        else if (nYourNum > nNumIGuess)
        {
            cout<<"\nIncorrect, the number I am thinking of is lower \n";
        }
        else 
        {
            cout<<"\nCorrect!!!! You win \n";       
        }
        return nNumOfGuesses; 
    }
    Sent from my iPadŽ

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    7
    Looks like its my computer then thanks for your help anyway SlyMaelstrom.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C - need help with a simple prog.
    By Milano in forum C Programming
    Replies: 5
    Last Post: 07-06-2006, 12:23 AM
  2. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 10:59 PM
  3. Help- Problems running outside of compiler
    By Derek5272 in forum C++ Programming
    Replies: 17
    Last Post: 05-07-2003, 08:19 AM
  4. Re: Function problems in simple C++ prog.
    By cj348879 in forum C++ Programming
    Replies: 3
    Last Post: 11-11-2001, 07:00 PM
  5. Simple Server problems...
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 08:51 PM