Looping help

Hey guys! I'm writing a simple program with ifs and else if and etc,
below is something...

cout << "pick either red or blue" << endl;
cin >> color;

if (color==red)||(color==blue)
cout << "good job" << endl;
cout << "please pick either red or blue!" <<endl;

NOW if the user inputs something other than red or blue,
how can i loop and bring the program back to square one, and let the user input again, rather than closing the program.

thanks in advance
Put your code inside a while loop.
hey hekri could you be a bit more specific?
the below is the simple program I wrote

cout << "red or blue?" << endl;
cin >> color;

if(color == "red")
cout << "you picked red" << endl;
else if(color == "blue")
cout << "you picked blue" << endl;
cout << "pick a damn color" << endl;

now when the "else" happens, I want it to loop back to the beginning...where do I code the while loop? I tried to put the while loop in the beginning but it just loops infinitely...
#include <iostream>
#include <string.h>
using namespace std;


bool ExitLoop = false;
char UserColor[10];

while (ExitLoop == false){
    cout << endl << "Pick a color, either red or blue." << endl;
    cin >> UserColor;

    if (strcmp(UserColor, "red") == 0){
        cout << endl << "You selected red." << endl;
        ExitLoop = true;
    else if (strcmp(UserColor, "blue") == 0){
        cout << endl << "You have selected blue." << endl;
        ExitLoop = true;
        cout << "You did not select a valid color.";

The while loop needs to do the loop while the condition is true, so you just need to break that condition to exit. And use the code button. It helps to read it.

My original code was written incorrectly, and I have edited this post to correct it. This code works now.
Last edited on
thanks Meden.
not to go too off topic,
why did you chose char for UserColor?
isn't it supposed to be string?
I learned char was used for single words, i.e. "m" or "f" etc,
what does the [10] represent?

sorry for asking so many questions.
I used char because I don't know enough about strings yet. I learned about char arrays first.

char is used for single letters, but a char array can be used for a sequence of them. The [10] allows UserColor to store the sequence. Just as int numbers[10] would allow numbers to hold a set of ten different numbers such as int numbers[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}, char UserColor[10] = {'r', 'e', 'd', 'i', 's', 'h', 'b', 'l', 'u', 'e'} holds 10 characters, except there's a null terminator in there somewhere. The no bounds checking is confusing.
Last edited on
@meden: a string is also a char array and has the null terminator
New here, and I was looking for something like this, although what I'm doing is a simple exit loop at the end of a menu (when they choose to say "no, I didn't really want to close the program").

My question originally was "how do I get the computer to recognize a key press outside of a number, and actually treat it as a letter rather than a variable (similar to what is going on here). Now I put this into my code and tweaked it to fit context, but I don't actually know what makes it work.

What does "strcmp()" do? it appears to be the one thing that made this code work and mine not, as I identified my code as characters, but I couldn't set them =, or == otherwise they wanted to work like numbers.

From what I see and understand, strcmp() takes the first thing and compares it to see if the second thing matches it, but then what is the "== 0" for at the end of the if statement? it's a conditional, but why would the two subjects within strcmp create a 0 relationship within the function to ==0 in the first place?
Topic archived. No new replies allowed.