Jump to content

Need advice constructing a loop

Working with C++, I'm struggling to create a loop (just learning them) and am just trying to accomplish something simple.

 

Goal: Program will ask a question (only certain inputs are acceptable) and if the user doesn't answer the question correctly it will re ask the question but in a different way. For example, instead of just asking "Please enter your weight:", if they didn't enter a valid weight (like 5000, -7, etc.) it would ask something like "Invalid input, please try again". The sequence would keep repeating until until they user finally entered a valid input. 

 

 

For this example, I figured a weight between 1 and 500 was reasonable. This code works, except it just repeats over and over with the same question if not true. Ultimately it servers the purpose, but I want the program to ask a DIFFERENT question if user input is invalid as described above. I'm familiar with if / else statements but couldn't figure out a way to make it work, however I'm not familiar with "else if" statements. 

 

I could really use some help, thanks.

 

 

 

while (weight < 1 || weight > 500)
{
    cout << "Please enter your weight: " << endl;
    cin >> weight;
}

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to comment
https://linustechtips.com/topic/542170-need-advice-constructing-a-loop/
Share on other sites

Link to post
Share on other sites

i think you want the weight to be between 1 and 500, right?
then you need to do AND not OR, (IIRC and is &&)  and you need weight>1 and weight<500

you have your ><><  the wrong way

 

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 post
Share on other sites

1 minute ago, Enderman said:

i think you want the weight to be between 1 and 500, right?
then you need to do AND not OR, and you need weight>1 and weight<500

you have your ><><  the wrong way

 

I'm not sure what you mean. The code works, only a valid number between 1 and 500 is acceptable, that code works fine. But if you enter an invalid number it just repeats and asks the question, until the condition is true. If the input is invalid, I want it to ask a different question, as I described above.

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to post
Share on other sites

8 minutes ago, Spev said:

I'm not sure what you mean. The code works, only a valid number between 1 and 500 is acceptable, that code works fine. But if you enter an invalid number it just repeats and asks the question, until the condition is true. If the input is invalid, I want it to ask a different question, as I described above.

then you just need to add an if statement after the cin that checks if the input was between 1 and 500 then say something, then an else statement that asks a different question

unless you want the new question to replace the weight question, then thats more complicated because you need to make the question a variable string which gets modified in the else statement

 

sort of like this:

(my c++ hasnt been used in a long time)

 

 

x="What is your weight?";

loop starts here
    cout<<x<<endl;
    cin>>weight;
    if (weight>1 && weight<500)
        cout<<"your weight is between 1 and 500 lol"<<endl;
    else
        x="how old are you?";

 

 

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 post
Share on other sites

int weight, height, age;
bool isWeightValid = false, isHeightValid = false, isAgeValid = false;
do
{
	cout << "What's your weight? (1 - 500)";
	cin >> weight;
	if (weight > 1 && weight < 500)
        isWeightValid = true;
    cout << "what's your height? (1 - 500)";
    cin >> height;
	if (height > 1 && height < 500)
        isHeightValid = true;
    cout << "what's your age? (1 - 500)";
    cin >> age;
	if (age > 1 && age < 500)
        isAgeValid = true;
    cout << "Entered weight is " << ( (isWeightValid) ? "valid" : "not valid" );
    cout << "Entered height is " << ( (isHeightValid) ? "valid" : "not valid" );
    cout << "Entered age is " << ( (isAgeValid) ? "valid" : "not valid" );
}while( !isWeightValid || !isHeightValid || !isAgeValid );

If there's other fields, test if that code works. Been a long time I've coded in c++.

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

×