Computer Science Canada

loop question

Author:  randomname [ Thu Apr 01, 2010 3:28 pm ]
Post subject:  loop question

im doing this project, and basically it asks the user a few questions and then it asks them if they would like to answer the questions again, so i need somesort of loop (and an if) where when it asks yes or no -- yes starting the loop over and letting them answer the questions again and -- no basically the end of the program

i know how to do loops and if's but im not quite sure how to implement (use it) in this program. any help would be great

Author:  randomname [ Thu Apr 01, 2010 3:29 pm ]
Post subject:  Re: loop question

and oyea i forgot to add im using Dev-C++

Author:  jbking [ Thu Apr 01, 2010 3:37 pm ]
Post subject:  Re: loop question

Do you know about Boolean variables at all? If you do, combine a Boolean variable with a while loop and an if and you should have most of the code aside from the simple I/O parts so the user knows what questions are asked and the program uses the answers that are entered.

Author:  randomname [ Thu Apr 01, 2010 3:55 pm ]
Post subject:  Re: loop question

No sorry, ive never done booleans in C++ before

Author:  Insectoid [ Thu Apr 01, 2010 3:56 pm ]
Post subject:  RE:loop question

You don't even need a boolean. I don't know jack about C++ (Except that it's a headache waiting to happen) but in most languages you can just use a 0 or 1 (or, in fact, anything other than 0) and it can be evaluated as true or false.

Author:  jbking [ Thu Apr 01, 2010 4:01 pm ]
Post subject:  Re: loop question

New Fundamanental Type - bool covers some of the basics on using this type. It isn't necessary, true, but I'd find it helpful in getting a picture of how I'd likely write it, to try to keep things in their proper place.

Author:  TerranceN [ Thu Apr 01, 2010 4:59 pm ]
Post subject:  RE:loop question

You can use a break statement to exit out of your loop (although this can make it harder to read and understand your code). Also you asked how to implement it, hope this helps:

c++:
while (1) // This will loop forever unless it reaches a break statement
{
    // get input here

    if (input == "no")
    {
        // this breaks out of the closest loop
        break;
    }

    // if the input is not "no" then the program will loop and ask for input again
}


: