Problem with looping

This is a discussion on Problem with looping within the C++ Programming forums, part of the General Programming Boards category; Hello I'm currently creating a guessing game program where the user will be asked to guess the computer generated number. ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    6

    Question Problem with looping

    Hello I'm currently creating a guessing game program where the user will be asked to guess the computer generated number. Once the program is finished, at the end the user will be asked if they want to play again. If the user types "Y or Y" the program will loop and when the user types "n or N" it will not loop. Check out my code below.

    Code:
    #include<iostream.h>#include<stdio.h>
    #include<dos.h>
    #include<stdlib.h>
    #include<conio.h>
    #define g gotoxy
    
    
    void main(){
    int a,l,b,guess,row,col,answer,num,clue=5;
    textcolor(YELLOW);
    clrscr();
    randomize();
    a=random(30)+1;
    b=random(100)+20;
    num=random(a)+b;
    clrscr();
    for(col=1;col<=79;col++)
    {
    g(col,19);
    cout<<"Í";
    g(col,21);
    cout<<"Í";
    }
    for(row=1;row<=50;row++)
    {
    g(1,20);
    cout<<"º";
    g(79,20);
    cout<<"º";
    }
    for(col=1;col<=77;col++)
    {
    g(2,18);
    cout<<"Wait while the game is loading..."<<endl;
    g(col+1,20);
    cout<<">";
    delay(100);
    }
    do{
    clrscr();
    g(15,15);
    cout<<"Guess the number!";
    g(15,17);
    cout<<"Clue: The number is between:\t"<<a <<"\tand\t" <<b+a;
    g(15,19);
    cout<<"Remaining number of trials:\t"<<clue;
    textcolor(GREEN);
    if((guess<num))
    cout<<"\n\nEnter higher!";
    if((guess>num)&&(clue<5))
    cout<<"\n\nEnter lower!";
    g(17,22);
    cout<<"\nYour estimate is:";
    cin>>guess;
    if(guess!=num)
    textcolor(CYAN);
    clue--;
    if(guess==num)
    {
    textcolor(MAGENTA);
    clue=0;
    l=1;
    cout<<"Wow! You're right! The number is\t" << num;
    }
    }while(clue!=0);
    if(l==0)
    cout<<"\nSorry,you failed to guess the number, the number is\t" << num;
    cout<<"\n---------------------------------------------------------";
    cout<<"\n\tWould you like to play again?[Y/N]";
    cin>>answer;
    if((answer='N')||(answer='n'))
    clrscr();
    textcolor(GREEN);
    cout<<"\n\n\n\n\t\tYou have chosen to quit. Thanks for playing!"<<endl;
    getch();
    }


    I can't loop this program please help me with it. Thanks in advance!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    Your code is unreadable - SourceForge.net: Indentation - cpwiki
    Fix the indentation.

    > #define g gotoxy
    Don't do this.
    Write it out properly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    oh my mistake, here is the code:

    Code:
    #include<iostream.h>
    #include<stdio.h>
    #include<dos.h>
    #include<stdlib.h>
    #include<conio.h>
    #define g gotoxy
    
    
    void main(){
    int a,l,b,guess,row,col,answer,num,clue=5;
    textcolor(YELLOW);
    clrscr();
    randomize();
    a=random(30)+1;
    b=random(100)+20;
    num=random(a)+b;
    clrscr();
    for(col=1;col<=79;col++)
    {
    g(col,19);
    cout<<"Í";
    g(col,21);
    cout<<"Í";
    }
    for(row=1;row<=50;row++)
    {
    g(1,20);
    cout<<"º";
    g(79,20);
    cout<<"º";
    }
    for(col=1;col<=77;col++)
    {
    g(2,18);
    cout<<"Wait while the game is loading..."<<endl;
    g(col+1,20);
    cout<<">";
    delay(100);
    }
    do{
    clrscr();
    g(15,15);
    cout<<"Guess the number!";
    g(15,17);
    cout<<"Clue: The number is between:\t"<<a <<"\tand\t" <<b+a;
    g(15,19);
    cout<<"Remaining number of trials:\t"<<clue;
    textcolor(GREEN);
    if((guess<num))
    cout<<"\n\nEnter higher!";
    if((guess>num)&&(clue<5))
    cout<<"\n\nEnter lower!";
    g(17,22);
    cout<<"\nYour estimate is:";
    cin>>guess;
    if(guess!=num)
    textcolor(CYAN);
    clue--;
    if(guess==num)
    {
    textcolor(MAGENTA);
    clue=0;
    l=1;
    cout<<"Wow! You're right! The number is\t" << num;
    }
    }while(clue!=0);
    if(l==0)
    cout<<"\nSorry,you failed to guess the number, the number is\t" << num;
    cout<<"\n---------------------------------------------------------";
    cout<<"\n\tWould you like to play again?[Y/N]";
    cin>>answer;
    if((answer='N')||(answer='n'))
    clrscr();
    textcolor(GREEN);
    cout<<"\n\n\n\n\t\tYou have chosen to quit. Thanks for playing!"<<endl;
    getch();
    }

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Focus first on Salem's post.

    Actually I can't tell what of what he said did you actually followed.

    For example, this code is not good in terms of indentation
    Code:
    if(3==4)
    {
    cout<<"What?"<<endl;
    }
    A better indentation would produce something like this
    Code:
    if(3==4)
    {
         cout << "What?" << endl;
    }
    Then read, what Salem said said about the define you use.

    Finally, focus on his picture.
    You should have
    Code:
    int main()
    {
       /* your program*/
       return 0;
    }
    rather than
    Code:
    void main()
    ..
    And something from me (line 72)
    Code:
    if((answer='N')||(answer='n'))
    clrscr();
    You probably want to use the equality sign, which is == . One equal sign, means assignment.

    Nothing more from me, until you follow Salem's post
    Last edited by std10093; 02-11-2013 at 08:28 AM. Reason: code tag broken
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Code:
    #include<iostream.h>
    #include<stdio.h>
    #include<dos.h>
    #include<stdlib.h>
    #include<conio.h>
    
    
    int main(){
    int a , l , b , guess , row , col , answer , num , clue = 5;
    
    
    	textcolor(YELLOW);
    	clrscr();
    	randomize();
    	a = random(30) + 1;
    	b = random(100) + 20;
    	num = random(a) + b;
    	clrscr();
    
    
    for(col = 1; col <= 79 ; col++)
    {
    	gotoxy(col, 19);
    	cout << "Í";
    	gotoxy(col, 21);
    	cout << "Í";
    }
    
    
    for(row = 1; row <= 50; row++)
    {
    	gotoxy(1, 20);
    	cout << "º";
    	gotoxy(79, 20);
    	cout << "º";
    }
    
    
    for(col = 1; col <= 77; col++)
    {
    	gotoxy(2, 18);
    	cout << "Wait while the game is loading..." <<endl;
    	gotoxy(col + 1, 20);
    	cout << ">" ;
    	delay(100);
    }
    
    
    do{
    	clrscr();
    	gotoxy(15 , 15);
    	cout << "Guess the number!" ;
    	gotoxy(15 , 17);
    	cout << "Clue: The number is between:\t" << a << "\t and \t" << b + a;
    	gotoxy(15 , 19);
    	cout << "Remaining number of trials: \t" << clue;
    	textcolor(GREEN);
    
    
    		if((guess < num))
    			cout << "\n\n Enter higher!";
    		if((guess > num)&&(clue < 5))
    			cout << "\n\n Enter lower!";
    			gotoxy(17, 22);
    			cout << "\n Your estimate is:";
    			cin >> guess;
    		if(guess!= num)
    			textcolor(CYAN);
    			clue--;
    		if(guess == num)
    			{
    				textcolor(MAGENTA);
    				clue = 0;
    				l = 1;
    				cout << "Wow! You're right! The number is\t" << num;
    			}
    			
    }while(clue!= 0);
    if(l == 0)
    	cout <<"\nSorry,you failed to guess the number, the number is\t" << num;
    	cout <<"\n---------------------------------------------------------";
    	cout <<"\n\tWould you like to play again?[Y/N]";
    	cin >> answer;
    if((answer == 'N')||(answer == 'n'))
    	clrscr();
    	textcolor(GREEN);
    	cout<< "\n\n\n\n\t\tYou have chosen to quit. Thanks for playing!" << endl;
    return 0;
    }

    There you go sir. I'm sorry about the indention thing, I'm completely a newbie with this thing. I tried to run this new code but it still doesn't loops

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Good job about indentation. Thank you

    Which loop is not working?

    In this part of your code
    Code:
    if((guess < num))
                cout << "\n\n Enter higher!";
            if((guess > num)&&(clue < 5))
                cout << "\n\n Enter lower!";
                gotoxy(17, 22);
                cout << "\n Your estimate is:";
                cin >> guess;
            if(guess!= num)
                textcolor(CYAN);
                clue--;
            if(guess == num)
                {
                    textcolor(MAGENTA);
                    clue = 0;
                    l = 1;
                    cout << "Wow! You're right! The number is\t" << num;
                }
    are you sure about the brackets? When there are no brackets below an if statement, then the body of the if, is considered to be only the very next line of code.
    So, your code is equivalent to this
    Code:
            if((guess < num))
            {
                cout << "\n\n Enter higher!";
            }
            if((guess > num)&&(clue < 5))
            {
                cout << "\n\n Enter lower!";
            }
                gotoxy(17, 22);
                cout << "\n Your estimate is:";
                cin >> guess; 
            if(guess!= num)
            {
                textcolor(CYAN);
             }
                clue--;
            if(guess == num)
                {
                    textcolor(MAGENTA);
                    clue = 0;
                    l = 1;
                    cout << "Wow! You're right! The number is\t" << num;
                }
    I would suggest to always use brackets (when you are in your first steps ).
    So, you might want to place some brackets in other positions, judging from your indentation.

    // no need to call me sir
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Code:
    #include<iostream.h>
    #include<stdio.h>
    #include<dos.h>
    #include<stdlib.h>
    #include<conio.h>
    
    
    int main(){
    int a , l , b , guess , row , col , answer , num , clue = 5;
    
    
    	textcolor(YELLOW);
    	clrscr();
    	randomize();
    	a = random(30) + 1;
    	b = random(100) + 20;
    	num = random(a)+b;
    	clrscr();
    
    
    for(col = 1; col <= 79 ; col++)
    {
    	gotoxy(col, 19);
    	cout << "Í";
    	gotoxy(col, 21);
    	cout << "Í";
    }
    
    
    for(row = 1; row <= 50; row++)
    {
    	gotoxy(1, 20);
    	cout << "º";
    	gotoxy(79, 20);
    	cout << "º";
    }
    
    
    for(col = 1; col <= 77; col++)
    {
    	gotoxy(2, 18);
    	cout << "Wait while the game is loading..." <<endl;
    	gotoxy(col+1,20);
    	cout << ">";
    	delay(100);
    }
    
    
    do{
    	clrscr();
    	gotoxy(15, 15);
    	cout << "Guess the number!" ;
    	gotoxy(15, 17);
    	cout << "Clue: The number is between:\t" << a << "\tand\t" << b+a;
    	gotoxy(15, 19);
    	cout << "Remaining number of trials:\t" << clue;
    	textcolor(GREEN);
    
    
    		if((guess < num))
    		{
    			cout << "\n\nEnter higher!";
    		}
    		if((guess > num)&&(clue < 5))
    		{
    			cout << "\n\nEnter lower!";
    		}
    			gotoxy(17, 22);
    			cout << "\nYour estimate is:";
    			cin >> guess;
    
    
    		if(guess!= num)
    		{
    			textcolor(CYAN);
    			clue--;
    		}
    		if(guess == num)
    			{
    				textcolor(MAGENTA);
    				clue = 0;
    				l = 1;
    				cout << "Wow! You're right! The number is\t" << num;
    			}
    			
    }while(clue!=0);
    if(l==0)
    {
    	cout <<"\nSorry,you failed to guess the number, the number is\t" << num;
    	cout <<"\n---------------------------------------------------------";
    	cout <<"\n\tWould you like to play again?[Y/N]";
    	cin >> answer;
    }
    if((answer=='N')||(answer=='n'))
    {
    	clrscr();
    	textcolor(GREEN);
    	cout<< "\n\n\n\n\t\tYou have chosen to quit. Thanks for playing!" << endl;
    }
    return 0;
    }
    I tried what you said about the brackets but the program still doesn't loop. What I really want to happen is that when, for example, I finished the game I then will be asked if I want to play again, once I hit "y or Y" the program should loop. Argh I'm really having a hard time figuring out this thing

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I didn't suggested anything about where you should place your brackets. That's your choice

    Well, then you should have your game inside a loop. No if I input Yes when your program asks me if I want to play again, then I will jump from line 92 to line 100 (since the conditions on line 94 are both false). Line 100 is the end of main, thus the end of your game.

    So, place your game in a do while loop, which will have as condition this
    Code:
    while(answer != 'N' || answer != 'n');
    This way, as long as the user does not input N or n, the code inside the body of the do while I suggest will be executed again. So, if the code is your game, your game will be executed again
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  9. #9
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Quote Originally Posted by std10093 View Post
    I didn't suggested anything about where you should place your brackets. That's your choice

    Well, then you should have your game inside a loop. No if I input Yes when your program asks me if I want to play again, then I will jump from line 92 to line 100 (since the conditions on line 94 are both false). Line 100 is the end of main, thus the end of your game.

    So, place your game in a do while loop, which will have as condition this
    Code:
    while(answer != 'N' || answer != 'n');
    This way, as long as the user does not input N or n, the code inside the body of the do while I suggest will be executed again. So, if the code is your game, your game will be executed again

    I already have a do while statement in my program, I tried to put lines 49 to 99 inside another do while statement but I only got an error I don't have any idea where should I put the do while statement you're telling me. Please help me with this.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I don't feel comfortable giving you the direct solution, but I will make an example that does pretty much what you want to do.

    I will create an example, that asks the user to guess a number. He has 3 tries available. If he finds the answer, then we will print "Bravo". If not, we will ask him or her if he or she wants to play again. If not, he will terminate by saying goodbye.
    Read the code and the comments and then try to modify your main
    Code:
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int key = 5;
        int input;
        bool found = false; // flag
        char answer;
        int round;
        do {
            // initialize inside the loop, in case
            // we play again
            round = 0;
            do{
                cout << "Please input your guess" << endl;
                cin >> input;
                if(input == key)
                {
                    found = true;
                }
                round++;
            }while(round < 3 && !found);
            if(found)
            {
                cout << "Bravo" << endl;
            }
            else
            {
                cout << "Sorry, you lost" << endl;
            }
            cout << "Would you like to play again?"  << endl;
            cin >> answer;
            
            // Different conditions than yours
        }while(answer == 'y' || answer == 'Y');
        
        cout << "Goodbye!" << endl;
        return 0;
    }
    Of course my code can be improved, but I think you will get the logic
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  11. #11
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Quote Originally Posted by std10093 View Post
    I don't feel comfortable giving you the direct solution, but I will make an example that does pretty much what you want to do.

    I will create an example, that asks the user to guess a number. He has 3 tries available. If he finds the answer, then we will print "Bravo". If not, we will ask him or her if he or she wants to play again. If not, he will terminate by saying goodbye.
    Read the code and the comments and then try to modify your main
    Code:
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int key = 5;
        int input;
        bool found = false; // flag
        char answer;
        int round;
        do {
            // initialize inside the loop, in case
            // we play again
            round = 0;
            do{
                cout << "Please input your guess" << endl;
                cin >> input;
                if(input == key)
                {
                    found = true;
                }
                round++;
            }while(round < 3 && !found);
            if(found)
            {
                cout << "Bravo" << endl;
            }
            else
            {
                cout << "Sorry, you lost" << endl;
            }
            cout << "Would you like to play again?"  << endl;
            cin >> answer;
            
            // Different conditions than yours
        }while(answer == 'y' || answer == 'Y');
        
        cout << "Goodbye!" << endl;
        return 0;
    }
    Of course my code can be improved, but I think you will get the logic

    What specific software are you using sir? I used Turbo C++ version 3.0 because our professor required us to use it. I believe this version do not support boolean type. I'm totally messed up argh . I got errors in line 1 unable to open include file iostream, line 2 declaration syntax error, line 8 undefined symbol bool and statement missing ";" , line 16 undefined symbol and undefined symbol endl, line 17 undefined symbol cin and in line 20 undefined symbol found and undefined symbol true. Would you please give me the answer sir? I'm trying to fix this program for 7 straight hours and I still can't create a solution for this.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    Fair warning: you are using an extremely outdated pre-C++ compiler that does not follow the C++ standard. It will be utterly and completely useless in real life.
    So I suggest you complete the course as best you can (there is little you can do about the tools most likely, so...) and the completely forget everything. Every little thing you've learned because it's complete and utter nonsense. The best thing you can do it re-learn C++ from the beginning later if/when you need to use it.

    And as for a solution, sorry, not going to happen. You are required to be able to properly code a task yourself. Otherwise, you will not learn anything.
    The logic - the flow of code - is something that every programmer must know and master. If there any problems in the code, you need to learn how to use a debugger. That, or make a flowchart to trace the flow of the code (not ideal).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The problem is what Elysia states. I wanted to say that the headers you use are really old, but I wanted first you to complete the program.

    What is happening is that your compiler does not recognize what mines does.
    But, if using this compiler is a must by your professor, then use your own headers to accomplish this program, but then have a conversation with your professor about all this. The headers you are using are what Elysia said.

    So, instead of a boolean variable use an int, and represent false as zero and true as 1.
    Example of using an int variable as a boolean one.
    Code:
    int main()
    {
          int found = 0;
       
          /* the game */
          
          if(found)
          {
                 /* Win */
          }
          else
          {
                  /* Lose */
          }
          return 0;
    }
    So do this, and try to modify your code then
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! Looping problem.
    By Meerul264 in forum C++ Programming
    Replies: 20
    Last Post: 12-13-2012, 11:26 AM
  2. looping problem?
    By nyekknyakk in forum C Programming
    Replies: 9
    Last Post: 08-20-2010, 12:02 PM
  3. looping problem
    By trixxma in forum C Programming
    Replies: 10
    Last Post: 05-20-2006, 11:17 PM
  4. looping problem
    By swagatob in forum C Programming
    Replies: 7
    Last Post: 10-13-2004, 12:50 AM
  5. Looping problem
    By romeoz in forum C++ Programming
    Replies: 8
    Last Post: 09-01-2003, 09:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21