Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

question about break the loop

taco_beltaco_bel Member Posts: 30
hi there,
is there anybody can help me about this problem. i'm a begginer of c++ programming. i'm try to do this "guessing number" program and the end of the program will ask user to input "y" for yes and "n" for no. i'm using "While" loop. but when i enter n. the program won't terminate the program. please help.here is my program.


#include
#include
#include

int correctNum, i;
char input, n, y;
bool flag= true;

int main()
{
while (flag= true){

cout<<"I have a number between 1 and 100."<<endl
<<"Can you guess my number?"<<endl
<<"Please type your first guess: ";
cin>>i;

srand(time(0));
correctNum=rand()%100;

while (i!=correctNum){

if (i>correctNum)
cout<<"Too high. Try again.";
if (i<correctNum)
cout<<"Too low. Try again.";

cin>>i;
}
if (i=correctNum)
cout<<"Excellent! You guessed the number!"<<endl
<<"Would you like to play again? ( y or n): ";

cin>>input;
if (input=y)
flag= true;
if (input=n){
flag=false;
break;
}
}
return 0;
}

Comments

  • SteveCSteveC Member Posts: 63
    : while (flag= true){
    Use ==
    == is comparison, = is assignment
    Most compilers with high enough warning levels will warn you about this.

    : srand(time(0));
    Do this outside your loop - you should only need to call srand() ONCE

    : if (i=correctNum)
    Use ==

    : if (input=y)
    Getting the idea about == yet?

    : if (input=n){
    Ditto

  • Geeth AsokanGeeth Asokan Member Posts: 127
    Hi,

    I have modified your program. Try this out but note where I have made the changes. I have used [b]'do - while'[/b] loops in order to repeat the program and to break the loop, if necessary.

    [code]
    #include
    #include
    #include

    int main()
    {
    // Declare the variables
    int correctNum, guess;
    char input;

    // Creates a loop for the main program
    do
    {

    // User enters their guess
    cout << "I have a number between 1 and 100." << endl;
    cout << "Can you guess my number?" << endl;

    // Generates a random number
    srand(time(0));
    correctNum = rand()%100;

    // Creates a loop for the guessing stages
    do
    {

    cout << "Please type your guess: ";
    cin >> guess;

    // A 'if - else' statement to check whether
    // guess = correctNum (random number generated)

    if (guess == correctNum)
    {
    cout << "Excellent! You guessed the number!

    "<<endl;
    }
    else
    {
    if (guess > correctNum)
    {
    cout<< "Too high! Try again!
    ";
    }
    else
    {
    cout<<"Too low! Try again!
    ";
    }
    }

    }
    while (guess!=correctNum);

    // Commands if user wishes to play again or not
    cout << "Would you like to play again? (y or n): ";
    cin >> input;

    }
    while (input!='n');

    return 0;
    }
    [/code]

    Hope this helps,

  • taco_beltaco_bel Member Posts: 30
    Hi,
    thank you for youu help, now i know i can do it with Do- While loop. nice!! but i also find out another question. can we change "Do- while loop" to pure "while" loop? because upon your design. i also try to change my code below. when i run it, the compiler shows nothing. i guess the reason is i should use compairson before "while (input != n)",but i was wrong.....whould you be kindly tell me what wrong with my idea?


    #include
    #include
    #include

    int correctNum, guess;
    char input, n;


    int main()
    {
    srand(time(0));

    while (input != n){ //outter loop for break loop

    cout<<"I have a number between 1 and 100."<<endl
    <<"Can you guess my number?"<<endl
    <<"Please type your first guess: ";
    cin>>guess;

    correctNum=rand()%100;

    while (guess != correctNum){ //inner loop for guess number

    if (guess>correctNum)
    cout<<"Too high. Try again.";
    if (guess<correctNum)
    cout<<"Too low. Try again.";

    cin>>guess;
    } //end inner loop

    if (guess == correctNum)
    cout<<"Excellent! You guessed the number!"<<endl
    <<"Would you like to play again? ( y or n): ";
    cin>>input;

    } //end outter loop
    return 0;
    }


  • taco_beltaco_bel Member Posts: 30
    Hi steve,
    thank you for your correcting. i really can't id the difference between "=" and "==", now i got it. actually i study this by myself. my teacher couldn't give me more help bcause he does not speak "English" English. it is really hard to catch what he said in the class. so i have to learn it by my own.
    i correct my comparison"==" and assignment"=", but the loop still running again and again even if i input "n", i still can't find any mistake in there. is that possible i missing something?
    thank you for you spend much time for helping me.

    here is my program:

    #include
    #include
    #include

    int correctNum, i;
    char input, n, y;
    bool flag= true;

    int main()
    {
    srand(time(0));

    while (flag == true){ //outter loop
    cout<<"I have a number between 1 and 100."<<endl
    <<"Can you guess my number?"<<endl
    <<"Please type your first guess: ";
    cin>>i;

    correctNum=rand()%100;

    while (i!=correctNum){ //inner loop

    if (i>correctNum)
    cout<<"Too high. Try again.";
    if (i<correctNum)
    cout<<"Too low. Try again.";

    cin>>i;
    } //end inner loop
    if (i==correctNum)
    cout<<"Excellent! You guessed the number!"<<endl
    <<"Would you like to play again? ( y or n): ";
    cin>>input;

    if (input==y)
    flag=true;
    if (input==n){
    flag=false;
    break;
    }
    } //end outter loop
    return 0;
    }


  • Geeth AsokanGeeth Asokan Member Posts: 127
    Hi,

    Yes, your idea is perfectly feasible. You can do the same thing by using a pure 'while' loop. This is how to do it (I have provided the modified code below):

    [code]
    #include
    #include
    #include

    int main()
    {
    // Declare the variables
    int correctNum, guess;
    char input;

    // Creates a 'while' loop for the main program
    // instead of using the 'do - while' loop
    while (input!='n')
    {

    // User enters their guess
    cout << "I have a number between 1 and 100." << endl;
    cout << "Can you guess my number?
    " << endl;

    // Generates a random number
    srand(time(0));
    correctNum = rand()%100;

    // Creates a loop for the guessing stages
    // using a 'while' loop instead of the
    // 'do -while' loop
    while (guess!=correctNum)
    {

    cout << "Please type your guess: ";
    cin >> guess;

    // A 'if - else' statement to check whether
    // guess = correctNum (random number generated)

    if (guess == correctNum)
    {
    cout << "Excellent! You guessed the number!

    "<<endl;
    }
    else
    {
    if (guess > correctNum)
    {
    cout<< "Too high! Try again!
    ";
    }
    else
    {
    cout<<"Too low! Try again!
    ";
    }
    }

    }

    // Commands if user wishes to play again or not
    cout << "Would you like to play again? (y or n): ";
    cin >> input;

    }

    return 0;
    }
    [/code]

    Try it out! and note the changes.

    Take it easy,
  • Geeth AsokanGeeth Asokan Member Posts: 127
    Hi,

    Yes, your idea is perfectly feasible. You can do the same thing by using a pure 'while' loop. This is how to do it (I have provided the modified code below):

    [code]
    #include
    using std::cout; //never do this in a header
    using std::endl;
    #include
    #include

    int main()
    {
    // Declare the variables
    int correctNum, guess;
    char input;

    // Creates a 'while' loop for the main program
    // instead of using the 'do - while' loop
    while (input!='n')
    {

    // User enters their guess
    cout << "I have a number between 1 and 100." << endl;
    cout << "Can you guess my number?
    " << endl;

    // Generates a random number
    srand(time(0));
    correctNum = rand()%100;

    // Creates a loop for the guessing stages
    // using a 'while' loop instead of the
    // 'do -while' loop
    while (guess!=correctNum)
    {

    cout << "Please type your guess: ";
    cin >> guess;

    // A 'if - else' statement to check whether
    // guess = correctNum (random number generated)

    if (guess == correctNum)
    {
    cout << "Excellent! You guessed the number!

    "<<endl;
    }
    else
    {
    if (guess > correctNum)
    {
    cout<< "Too high! Try again!
    ";
    }
    else
    {
    cout<<"Too low! Try again!
    ";
    }
    }

    }

    // Commands if user wishes to play again or not
    cout << "Would you like to play again? (y or n): ";
    cin >> input;

    }

    return 0;
    }
    [/code]

    Try it out! and note the changes.

    Take it easy, and hope this helps,


  • PeterTheMasterPeterTheMaster Member Posts: 636
    1. no iostream.h
    2. no global vars
    3. you compare to the uninitialized chars y and n
    4. use code tags ([ code ] and [ /code ] without spaces)
    5. define vars as late as possible (helps to avoid 3.)
    6. do it and if necessary ask again.


    : hi there,
    : is there anybody can help me about this problem. i'm a begginer of c++ programming. i'm try to do this "guessing number" program and the end of the program will ask user to input "y" for yes and "n" for no. i'm using "While" loop. but when i enter n. the program won't terminate the program. please help.here is my program.
    :
    :
    : #include
    : #include
    : #include
    :
    : int correctNum, i;
    : char input, n, y;
    : bool flag= true;
    :
    : int main()
    : {
    : while (flag= true){
    :
    : cout<<"I have a number between 1 and 100."<<endl
    : <<"Can you guess my number?"<<endl
    : <<"Please type your first guess: ";
    : cin>>i;
    :
    : srand(time(0));
    : correctNum=rand()%100;
    :
    : while (i!=correctNum){
    :
    : if (i>correctNum)
    : cout<<"Too high. Try again.";
    : if (i<correctNum)
    : cout<<"Too low. Try again.";
    :
    : cin>>i;
    : }
    : if (i=correctNum)
    : cout<<"Excellent! You guessed the number!"<<endl
    : <<"Would you like to play again? ( y or n): ";
    :
    : cin>>input;
    : if (input=y)
    : flag= true;
    : if (input=n){
    : flag=false;
    : break;
    : }
    : }
    : return 0;
    : }
    :
    :

  • taco_beltaco_bel Member Posts: 30
    Hi,
    i think i found the problem. i believe i made mistake at this part,
    "you compare to the uninitialized chars y and n"
    thank you for spending time to help me...^_^.

  • taco_beltaco_bel Member Posts: 30
    Hi Asokan,
    thank you for you spend much time with me, i believe that i found what wrong is it. i'm such a fool to frogot the difference between 'n' and "n"..... you really show me lot of writing style in c++ that i won't see it in the class.thanks again......^_^.



  • Chris BrownChris Brown USAMember Posts: 4,624 ✭✭

    _________ || http://forcoder.org || free ebooks and video tutorials about | Visual Basic .NET Delphi Objective-C C C++ PHP Ruby Go PL/SQL Python Assembly C# MATLAB Swift Visual Basic Java JavaScript Scratch Perl R Lisp Transact-SQL Alice Scala Fortran Dart Ada LabVIEW Crystal Julia Erlang COBOL D Bash Apex ABAP Lua Rust Clojure Prolog F# VBScript SAS Kotlin Scheme FoxPro Logo Hack ML Awk |

Sign In or Register to comment.