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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#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!
after your if statements, you need {} to indicate where they begin and end.

void main() should be int main()

you don't have return 0; at the end of main.

your code is not formated, which I think would help you and me see the logic.

EDIT
your also missing
using namespace std;
Last edited on
I already changed void to int main.

Sir what do you exactly mean with
after your if statements, you need {} to indicate where they begin and end.


how should I do that? Would you please show me?
You should put

1
2
3
4
5
6
7
8
int main(){
     double x = 0;
     if ( x < 1 ){        // if ( Expression )
           cout << "Hip-hip-hurrei" << endl;         // Statement 
           cout << "Hio hoi << endl;
     }
     return 0;
} 


This is valid

1
2
3
if ( x < 1 )
cout << "Hip-hip-hurrei" << endl;         // Statement 
cout << "Hio hoi << endl; 


But "Hio hoi" is independent on expression. I think that the first option is what you need.
Topic archived. No new replies allowed.