Jump to content

[HELP] C++ while true loop

Go to solution Solved by Nineshadow,

Your code has quite a few very obvious syntax issues.

Anyway :

#include <iostream>
using namespace std;

main()
{

    int usersGuess;
    cout << "I'm thinking of a number between 1-100\n";
    cout << "Guess what it is: ";
    cin >> usersGuess;
    while(true)
    {
        if( usersGuess == 7) break;
        if( usersGuess > 7 )
        {
            cout << "That's too high.\n";
            cout << "Guess what it is: ";
            cin >> usersGuess;
        }

        if( usersGuess < 7)
        {
            cout << "That's too low.\n";
            cout << "Guess what it is: ";
            cin >> usersGuess;
        }
    }
    cout << "Jackpot!";



}

This should fix it.

 

Learn the proper if statement syntax in C :

if(<condition>)
{
<instructions>
}

 

Spoiler

#include <iomanip>
#include <iostream>
using namespace std;
#include <cmath>

main() {
    
    int usersGuess; //
    
    cout << "I'm thinking of a number between 1-100\n";
    cout << "Guess what it is: ";
    cin >> usersGuess;
    cin.ignore(1000, 10);

    while(true) {

    if( usersGuess == 7) break; // see if the guess is right

    if( usersGuess > 7 );
    cout << "That's too high.\n";
    cout << "Guess what it is: ";
    cin >> usersGuess;
    cin.ignore(1000, 10);
    
    if( usersGuess < 7);
    cout << "That's too low.\n";
    cout << "Guess what it is: ";
    cin.ignore(1000, 10);
    }
    
    
    

    cout << "Jackpot!";
    
    
    
}

 

If I input greater than 7 it should say it "That's too high".

The problem is if I input greater than 7 again it says "That's too low" when it should have been that's too high.

 

Im confused

 

 

Yeah, we're all just a bunch of idiots experiencing nothing more than the placebo effect.
Link to comment
https://linustechtips.com/topic/559572-help-c-while-true-loop/
Share on other sites

Link to post
Share on other sites

first use elif and else statements instead of just going if if if all the time

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/559572-help-c-while-true-loop/#findComment-7364995
Share on other sites

Link to post
Share on other sites

Quote

#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
#include <cmath>
main() {
    
    int usersGuess; //
    
    cout << "I'm thinking of a number between 1-100\n";
    cout << "Guess what it is: ";
    cin >> usersGuess;
    cin.ignore(1000, 10);
    while(true) {
    if( usersGuess == 7) break; // see if the guess is right
    
    elif( usersGuess > 7 );
    cout << "That's too high.\n";
    cout << "Guess what it is: ";
    cin >> usersGuess;
    cin.ignore(1000, 10);
    
    else;
    cout << "That's too low.\n";
    cout << "Guess what it is: ";
    cin.ignore(1000, 10);
    }

Maybe as @Enderman said, like this

                     .
                   _/ V\
                  / /  /
                <<    |
                ,/    ]
              ,/      ]
            ,/        |
           /    \  \ /
          /      | | |
    ______|   __/_/| |
   /_______\______}\__}  

Spoiler

[I5-12600k | 32gb DDR5 6000 | RTX5070 | 2x1tb M.2]

 

[Ryzen 5 1600 | 16gb DDR4 3200 | GTX1030 | 4x 8tb HDD] 

 

Link to comment
https://linustechtips.com/topic/559572-help-c-while-true-loop/#findComment-7365019
Share on other sites

Link to post
Share on other sites

Quote

#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
#include <cmath>
main() {
    
    int usersGuess; //
    
    cout << "I'm thinking of a number between 1-100\n";
    cout << "Guess what it is: ";
    cin >> usersGuess;
    cin.ignore(1000, 10);
    while(true) {
    
    if( usersGuess > 7 );
       cout << "That's too high.\n";
       cout << "Guess what it is: ";
       cin >> usersGuess;
       cin.ignore(1000, 10);
    
    else if( usersGuess < 7) 
       cout << "That's too low.\n";
       cout << "Guess what it is: ";
       cin.ignore(1000, 10);
    
    else;
       break;

    }
    

Maybe switch the values around so that break is the last option. 

                     .
                   _/ V\
                  / /  /
                <<    |
                ,/    ]
              ,/      ]
            ,/        |
           /    \  \ /
          /      | | |
    ______|   __/_/| |
   /_______\______}\__}  

Spoiler

[I5-12600k | 32gb DDR5 6000 | RTX5070 | 2x1tb M.2]

 

[Ryzen 5 1600 | 16gb DDR4 3200 | GTX1030 | 4x 8tb HDD] 

 

Link to comment
https://linustechtips.com/topic/559572-help-c-while-true-loop/#findComment-7365137
Share on other sites

Link to post
Share on other sites

This is what your code is doing

( the semicolons after the if expressions mean that they do nothing)

cout << "I'm thinking of a number between 1-100\n";
    cout << "Guess what it is: ";
    cin >> usersGuess;
    cin.ignore(1000, 10);
    while(true) {
    if( usersGuess == 7) { break; } // see if the guess is right
    if( usersGuess > 7 ){
        // do nothing
    }
    cout << "That's too high.\n";
    cout << "Guess what it is: ";
    cin >> usersGuess;
    cin.ignore(1000, 10);
    
    if( usersGuess < 7){
        // do nothing
    }
    cout << "That's too low.\n";
    cout << "Guess what it is: ";
    cin.ignore(1000, 10);
    }

 

You should use braces like this:

cout << "I'm thinking of a number between 1-100\n";
while(true) {
    cout << "Guess what it is: ";
    cin >> usersGuess;
    if( usersGuess > 7 ){
    	cout << "That's too high.\n";
    }else if( usersGuess < 7){
    	cout << "That's too low.\n";
    }else{ // usersGuess == 7
    	break;
    }
}

 

I remain,  

msevilgenius

Link to comment
https://linustechtips.com/topic/559572-help-c-while-true-loop/#findComment-7365207
Share on other sites

Link to post
Share on other sites

6 minutes ago, RedWulf said:

Maybe switch the values around so that break is the last option. 

I can't do that because the break should be initiated if the correct answer is guess which is 7.

 

The problem is I want to keep looping "That's too high" if the user inputs greater than 7 and skip the "That's too low".

Yeah, we're all just a bunch of idiots experiencing nothing more than the placebo effect.
Link to comment
https://linustechtips.com/topic/559572-help-c-while-true-loop/#findComment-7365230
Share on other sites

Link to post
Share on other sites

Then replace else with else if from the last code revision or @msevilgenius suggestion and include another else with the loop call, I'm not sure if c++ uses it though, I'm a python guy

maybe this is a problem? 

http://www.cplusplus.com/forum/beginner/144641/

                     .
                   _/ V\
                  / /  /
                <<    |
                ,/    ]
              ,/      ]
            ,/        |
           /    \  \ /
          /      | | |
    ______|   __/_/| |
   /_______\______}\__}  

Spoiler

[I5-12600k | 32gb DDR5 6000 | RTX5070 | 2x1tb M.2]

 

[Ryzen 5 1600 | 16gb DDR4 3200 | GTX1030 | 4x 8tb HDD] 

 

Link to comment
https://linustechtips.com/topic/559572-help-c-while-true-loop/#findComment-7365291
Share on other sites

Link to post
Share on other sites

If nothing else heres a loop from another article that has claimed to work, you'd need to replace the int number from the random pattern to the number you want (7) 

Quote

#include <iostream>
#include <cstdlib>
#include <ctime>

int main(void) {
	srand(time(NULL)); // To not have the same numbers over and over again.
	
	while(true) { // Main loop.
		// Initialize and allocate.
		int number = rand() % 99 + 2; // System number is stored in here.
		int guess; // User guess is stored in here.
		int tries = 0; // Number of tries is stored here.
		char answer; // User answer to question is stored here.
		
		//std::cout << number << "\n"; // Was used for debug...
		
		while(true) { // Get user number loop.
			// Get number.
			std::cout << "Enter a number between 1 and 100 (" << 20 - tries << " tries left): ";
			std::cin >> guess;
			std::cin.ignore();
			
			// Check is tries are taken up.
			if(tries >= 20) {
				break;
			}
			
			// Check number.
			if(guess > number) {
				std::cout << "Too high! Try again.\n";
			} else if(guess < number) {
				std::cout << "Too low! Try again.\n";
			} else {
				break;
			}
			
			// If not number, increment tries.
			tries++;
		}
		
		// Check for tries.
		if(tries >= 20) {
			std::cout << "You ran out of tries!\n\n";
		} else {
			// Or, user won.
			std::cout<<"Congratulations!! " << std::endl;
			std::cout<<"You got the right number in " << tries << " tries!\n";
		}
		
		while(true) { // Loop to ask user is he/she would like to play again.
			// Get user response.
			std::cout << "Would you like to play again (Y/N)? ";
			std::cin >> answer;
			std::cin.ignore();
			
			// Check if proper response.
			if(answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') {
				break;
			} else {
				std::cout << "Please enter \'Y\' or \'N\'...\n";
			}
		}
		
		// Check user's input and run again or exit;
		if(answer == 'n' || answer == 'N') {
			std::cout << "Thank you for playing!";
			break;
		} else {
			std::cout << "\n\n\n";
		}
	}
	
	// Safely exit.
	std::cout << "\n\nEnter anything to exit. . . ";
	std::cin.ignore();
	return 0;
}

 

                     .
                   _/ V\
                  / /  /
                <<    |
                ,/    ]
              ,/      ]
            ,/        |
           /    \  \ /
          /      | | |
    ______|   __/_/| |
   /_______\______}\__}  

Spoiler

[I5-12600k | 32gb DDR5 6000 | RTX5070 | 2x1tb M.2]

 

[Ryzen 5 1600 | 16gb DDR4 3200 | GTX1030 | 4x 8tb HDD] 

 

Link to comment
https://linustechtips.com/topic/559572-help-c-while-true-loop/#findComment-7365372
Share on other sites

Link to post
Share on other sites

Your code has quite a few very obvious syntax issues.

Anyway :

#include <iostream>
using namespace std;

main()
{

    int usersGuess;
    cout << "I'm thinking of a number between 1-100\n";
    cout << "Guess what it is: ";
    cin >> usersGuess;
    while(true)
    {
        if( usersGuess == 7) break;
        if( usersGuess > 7 )
        {
            cout << "That's too high.\n";
            cout << "Guess what it is: ";
            cin >> usersGuess;
        }

        if( usersGuess < 7)
        {
            cout << "That's too low.\n";
            cout << "Guess what it is: ";
            cin >> usersGuess;
        }
    }
    cout << "Jackpot!";



}

This should fix it.

 

Learn the proper if statement syntax in C :

if(<condition>)
{
<instructions>
}

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/559572-help-c-while-true-loop/#findComment-7365422
Share on other sites

Link to post
Share on other sites

3 minutes ago, Nineshadow said:

Your code has quite a few very obvious syntax issues.

Anyway :


#include <iostream>
using namespace std;

main()
{

    int usersGuess;
    cout << "I'm thinking of a number between 1-100\n";
    cout << "Guess what it is: ";
    cin >> usersGuess;
    while(true)
    {
        if( usersGuess == 7) break;
        if( usersGuess > 7 )
        {
            cout << "That's too high.\n";
            cout << "Guess what it is: ";
            cin >> usersGuess;
        }

        if( usersGuess < 7)
        {
            cout << "That's too low.\n";
            cout << "Guess what it is: ";
            cin >> usersGuess;
        }
    }
    cout << "Jackpot!";



}

This should fix it.

Holy crap it did fixed it :D

 

BTW why did you not use cin.ignore(1000, 10)?

my Prof always says to include it when making a prompt.

Yeah, we're all just a bunch of idiots experiencing nothing more than the placebo effect.
Link to comment
https://linustechtips.com/topic/559572-help-c-while-true-loop/#findComment-7365459
Share on other sites

Link to post
Share on other sites

Just now, rcarlos243 said:

Holy crap it did fixed it :D

 

BTW why did you not use cin.ignore(1000, 10)?

my Prof always says to include it when making a prompt.

It won't really make much of a difference here honestly.

Cin leaves the newline character in the stream. Adding cin.ignore() to the next line clears/ignores the newline from the stream. This is used mainly with combinations of cin and getline.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/559572-help-c-while-true-loop/#findComment-7365472
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×