Thread: Coding error?

  1. #1
    Registered User
    Join Date
    Jul 2019
    Posts
    7

    Coding error?

    Hello, I am fairly new to c++ and I have been pulling my hair out trying to figure out whats going on with this code and I can't seem to find the error. I am just trying to make a console game that will randomly generate a number 0-777 and when it finally generates 777 it will say jackpot! with some command prompt artwork.

    The errors I have been experiencing is it never prints random numbers more than 1 time. It will print for example 589 and then every single debug after that it will still print 589. I also can't seem to get the "press the enter key to close the window" to go away either... I'd like it to be endless guesses in the same window until the 777 finally comes in. I get no build errors so I don't understand what happening. I also attached the code in attachments as well. Any help would be appreciated!
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Beware of ; on the ends of statements like
    for (int index = 0; index < 20; index++);
    and
    else if (random_integer >= high);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2019
    Posts
    7
    Quote Originally Posted by Salem View Post
    Beware of ; on the ends of statements like
    for (int index = 0; index < 20; index++);
    and
    else if (random_integer >= high);
    Thank you, that fixed it so the random number would print. I am still having a problem where when it finally hits the 777 it just says "try again" just like with numbers 0-776 when it is suppose to do the 'else if' part of the code with the jackpot.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code? Post it within code tags.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2019
    Posts
    7
    Quote Originally Posted by laserlight View Post
    What is your current code? Post it within code tags.
    Code:
    #include <iostream> #include <ctime> 
    #include <cstdlib>
    using namespace std;
    // Need to add a command to keep the window from closing so you can keep guessing.
    int main()
    {
    	srand((unsigned)time(0));
    	int random_integer;
    	int low = 0;
    	int high = 777;
    	int range = (high - low) + 1;
    
    
    
    
    	for (int index = 0; index < 20; index++);
    	random_integer = low + int(range * rand() / (RAND_MAX + 1.0));
    
    
    
    
    	if (random_integer <= low)
    	{
    		cout << "\n";
    		cout << "                                *******\n";
    		cout << "                                *"; cout << " "; cout << random_integer; cout << " "; cout << "*\n";
    		cout << "                                *******\n";
    		cout << "\n";
    		cout << "                               Try Again!\n";
    	}
    	else if (random_integer >= high);
    	{
    		cout << "                                        ****"; cout << " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ";
    		cout << "\n";
    		cout << "                                      ********";
    		cout << "\n";
    		cout << "                                   **************";
    		cout << "\n";
    		cout << "                                 *******"; cout << " "; cout << random_integer; cout << " "; cout << "*******";
    		cout << "\n";
    		cout << "                                   **************";
    		cout << " \n";
    		cout << "                                      ********";
    		cout << "\n";
    		cout << "                                        ****\n";
    		cout << "\n";
    		cout << "                                     [ JACKPOT ]\n";
    		cout << "\n";
    		cout << "                               $10,000,000 in gold bricks!\n";
    		cout << "\n";
    		cout << "\n";
    		cout << "                    [ ]                  [ ]                 [ ]   \n";
    		cout << "                  [ ][ ]                [ ][ ]              [ ][ ]  \n";
    		cout << "                 [ ][ ][ ]            [ ][ ][ ]           [ ][ ][ ] \n";
    		cout << "                [ ][ ][ ][ ]         [ ][ ][ ][ ]        [ ][ ][ ][ ] \n";
    		cout << "              [ ][ ][ ][ ][ ]       [ ][ ][ ][ ][ ]     [ ][ ][ ][ ][ ]  \n";
    		cout << "             [ ][ ][ ][ ][ ][ ]   [ ][ ][ ][ ][ ][ ]   [ ][ ][ ][ ][ ][ ] \n";
    		return 0;
    	}
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like you still have that semi-colon terminating the for loop early. You need to use braces to mark the body of the for loop because it consists of more than one statement.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2019
    Posts
    7
    I copied from the file I had attached which still had the semi colon. I removed it but its still printing everything 0-777 as try again. It should only be doing that for 0-776 and then 777 should do the jackpot. How could I make Int low 0-776 so that high will only be 777 and print the jackpot? I just tried making Int low = 0-776 but then it just prints random numbers with a negative sign in front of them lol

  8. #8
    Registered User
    Join Date
    Jul 2019
    Posts
    7
    Code:
    #include <iostream> #include <ctime> 
    #include <cstdlib>
    using namespace std;
    // Need to add a command to keep the window from closing so you can keep guessing.
    int main()
    {
        srand((unsigned)time(0));
        int random_integer;
        int low = 0;
        int high = 777;
        int range = (high - low) + 1;
    
    
    
    
        for (int index = 0; index < 20; index++)
            random_integer = low + int(range * rand() / (RAND_MAX + 1.0));
    
    
    
    
        if (random_integer <= low)
        {
            cout << "\n";
            cout << "                                *******\n";
            cout << "                                *"; cout << " "; cout << random_integer; cout << " "; cout << "*\n";
            cout << "                                *******\n";
            cout << "\n";
            cout << "                               Try Again!\n";
        }
        else if (random_integer >= high)
        {
            cout << "                                        ****"; cout << " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ";
            cout << "\n";
            cout << "                                      ********";
            cout << "\n"; 
            cout << "                                   **************";         
            cout << "\n";
            cout << "                                 *******"; cout << " "; cout << random_integer; cout << " "; cout << "*******";
            cout << "\n";
            cout << "                                   **************";
            cout << " \n";
            cout << "                                      ********";
            cout << "\n";
            cout << "                                        ****\n";
            cout << "\n";
            cout << "                                     [ JACKPOT ]\n";
            cout << "\n";
            cout << "                               $10,000,000 in gold bricks!\n";
            cout << "\n"; 
            cout << "\n";
            cout << "                    [ ]                  [ ]                 [ ]   \n";
            cout << "                  [ ][ ]                [ ][ ]              [ ][ ]  \n";
            cout << "                 [ ][ ][ ]            [ ][ ][ ]           [ ][ ][ ] \n";
            cout << "                [ ][ ][ ][ ]         [ ][ ][ ][ ]        [ ][ ][ ][ ] \n";
            cout << "              [ ][ ][ ][ ][ ]       [ ][ ][ ][ ][ ]     [ ][ ][ ][ ][ ]  \n";
            cout << "             [ ][ ][ ][ ][ ][ ]   [ ][ ][ ][ ][ ][ ]   [ ][ ][ ][ ][ ][ ] \n";
            return 0;
        }
    }
    this is the code now and it prints nothing.

    Code:
    #include <iostream> #include <ctime> 
    #include <cstdlib>
    using namespace std;
    // Need to add a command to keep the window from closing so you can keep guessing.
    int main()
    {
        srand((unsigned)time(0));
        int random_integer;
        int low = 770;
        int high = 777;
        int range = (high - low) + 1;
    
    
    
    
        for (int index = 0; index < 20; index++)
            random_integer = low + int(range * rand() / (RAND_MAX + 1.0));
    
    
    
    
        if (random_integer >= low)
        {
            cout << "\n";
            cout << "                                *******\n";
            cout << "                                *"; cout << " "; cout << random_integer; cout << " "; cout << "*\n";
            cout << "                                *******\n";
            cout << "\n";
            cout << "                               Try Again!\n";
        }
        else if (random_integer >= high)
        {
            cout << "                                        ****"; cout << " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ";
            cout << "\n";
            cout << "                                      ********";
            cout << "\n"; 
            cout << "                                   **************";         
            cout << "\n";
            cout << "                                 *******"; cout << " "; cout << random_integer; cout << " "; cout << "*******";
            cout << "\n";
            cout << "                                   **************";
            cout << " \n";
            cout << "                                      ********";
            cout << "\n";
            cout << "                                        ****\n";
            cout << "\n";
            cout << "                                     [ JACKPOT ]\n";
            cout << "\n";
            cout << "                               $10,000,000 in gold bricks!\n";
            cout << "\n"; 
            cout << "\n";
            cout << "                    [ ]                  [ ]                 [ ]   \n";
            cout << "                  [ ][ ]                [ ][ ]              [ ][ ]  \n";
            cout << "                 [ ][ ][ ]            [ ][ ][ ]           [ ][ ][ ] \n";
            cout << "                [ ][ ][ ][ ]         [ ][ ][ ][ ]        [ ][ ][ ][ ] \n";
            cout << "              [ ][ ][ ][ ][ ]       [ ][ ][ ][ ][ ]     [ ][ ][ ][ ][ ]  \n";
            cout << "             [ ][ ][ ][ ][ ][ ]   [ ][ ][ ][ ][ ][ ]   [ ][ ][ ][ ][ ][ ] \n";
            return 0;
        }
    }
    I put the Low at 770 so that I can see what happens when it finally hits 777 it will just print it like everything else with the try again!. I also changed if (random_integer >= low) so now it generates random numbers even when I change Low back to 0. Which the first code didnt have so it wasnt printing anything because nothing can be less than 0. So now the remaining problem is just to get the code to print the Else if part of the code when it hits 777 instead of the Try again!

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why are you generating the random integer 20 times, overwriting it each time except the last? You only need to generate it once, although I'm guessing you want the for loop to include more than just that, which is why I talked about braces.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jul 2019
    Posts
    7
    Quote Originally Posted by laserlight View Post
    Why are you generating the random integer 20 times, overwriting it each time except the last? You only need to generate it once, although I'm guessing you want the for loop to include more than just that, which is why I talked about braces.
    Code:
    #include <iostream> #include <ctime> 
    #include <cstdlib>
    using namespace std;
    // Need to add a command to keep the window from closing so you can keep guessing.
    int main()
    {
    	srand((unsigned)time(0));
    	int random_integer;
    	int low = 770;
    	int high = 777;
    	int range = (high - low) + 1;
    
    
    	{
    		for (int index = 0; index < 2; index++)
    		random_integer = low + int(range * rand() / (RAND_MAX + 1.0));
        }
    
    
    	if (random_integer >= low)
    	{
    		cout << "\n";
    		cout << "                                *******\n";
    		cout << "                                *"; cout << " "; cout << random_integer; cout << " "; cout << "*\n";
    		cout << "                                *******\n";
    		cout << "\n";
    		cout << "                               Try Again!\n";
    	}
    	else if (random_integer >= high)
    	{
    		cout << "                                        ****"; cout << " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ";
    		cout << "\n";
    		cout << "                                      ********";
    		cout << "\n"; 
    		cout << "                                   **************";         
    		cout << "\n";
    		cout << "                                 *******"; cout << " "; cout << random_integer; cout << " "; cout << "*******";
    		cout << "\n";
    		cout << "                                   **************";
    		cout << " \n";
    		cout << "                                      ********";
    		cout << "\n";
    		cout << "                                        ****\n";
    		cout << "\n";
    		cout << "                                     [ JACKPOT ]\n";
    		cout << "\n";
    		cout << "                               $10,000,000 in gold bricks!\n";
    		cout << "\n"; 
    		cout << "\n";
    		cout << "                    [ ]                  [ ]                 [ ]   \n";
    		cout << "                  [ ][ ]                [ ][ ]              [ ][ ]  \n";
    		cout << "                 [ ][ ][ ]            [ ][ ][ ]           [ ][ ][ ] \n";
    		cout << "                [ ][ ][ ][ ]         [ ][ ][ ][ ]        [ ][ ][ ][ ] \n";
    		cout << "              [ ][ ][ ][ ][ ]       [ ][ ][ ][ ][ ]     [ ][ ][ ][ ][ ]  \n";
    		cout << "             [ ][ ][ ][ ][ ][ ]   [ ][ ][ ][ ][ ][ ]   [ ][ ][ ][ ][ ][ ] \n";
    		return 0;
    	}
    }
    like this?

  11. #11
    Registered User
    Join Date
    Jul 2019
    Posts
    7
    The Else if was the problem. I changed it to IF for both commands and now it works!

  12. #12
    Registered User
    Join Date
    Jul 2019
    Posts
    2
    you should not change else if to if
    in your situation it should be
    Code:
    if (random_integer >= high)
    {
    // congatz
    }
    else
    {
    // random_integer now is less than high
    }
    the first note is :
    Code:
    int low = 770;
        int high = 777;
    Code:
    if (random_integer >= low)
    ok .
    Code:
    else if (random_integer >= high)
    why?
    this statement will never executed because you already check if random_integer>=770
    here .
    Code:
    if (random_integer >= low)
    the second note is :
    if you are using else if statement the first if statement must be false in order to execute the else if statement.
    in your case
    Code:
    if (random_integer >= low)
    is always true


    to keep the window open just put
    Code:
    std::cin.get();
    before the return statement
    remember to put return statement before the last curly bracket.

    .
    Last edited by Muqtada; 07-12-2019 at 07:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem regarding the coding error !
    By naughtyaashiq in forum C++ Programming
    Replies: 10
    Last Post: 12-10-2013, 11:26 AM
  2. error in filter design coding
    By loga in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2008, 04:38 AM
  3. Help on coding, possible memory allocation error.
    By NiHL in forum C Programming
    Replies: 1
    Last Post: 11-08-2004, 09:14 AM
  4. Coding Error
    By polonyman in forum C++ Programming
    Replies: 6
    Last Post: 09-10-2004, 02:17 AM
  5. Replies: 3
    Last Post: 03-27-2004, 12:15 PM

Tags for this Thread