Need help fixing code

I'm creating a code for guessing a random number that a user put in. I come across a problem where I ask the user if the number generated was their number, and it always registers as a yes, even when I put a no. Any ideas on how to fix this?

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
77
78
79
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main ()
{
cout << "Think of a number between 0 and 10. Now let me try and guess it." << endl;
int a,b,y,n;
y = 90;
n = 60;
bool c;
/*planned values for variables:
a= their number
b= their value for if random number is true or false
if b=Y,y then c=true
if b=N,n then c=false
if c= true then "congrat i win"
if c= false then "boo i lose, lemme guess again" */
srand ( time(NULL) );
cout << endl;
cout << "Input your number. Don't worry, I wont know it." << endl;
//Here im going to set a limit on the input of the number so that it is between 1 and 10
cout << endl;
cout << "Your Number: ";
cin >> a;
cout << endl;
if (a >= 10 && a >= 1)
//If the number is out of the bounds, then I will have a message appearing for them to put in a number between 1 and 10.
{
	
	cout << "Sorry, " << a << " is either too high or too low. Using a number out of the range of 0" <<endl << "and 10 will make this program inoperable and will cause it to exit. Please try" <<endl << "again." << endl;
	cout << endl;
	cout << "Your Number (Between 0 and 10): ";
	cin >> a;
	if (a >= 10 && a >= 1)
	{
		cout << endl;
		cout << "The program will now exit." << endl << endl;
		system("pause");
		exit(0);
	}
} 
printf ("Is the number %d", rand () % 10);
cout << endl;
cout << endl;
cout << "y/n: ";
cin >> b;
cout << endl;
//Here I'll make it so that b is true or false
if (b = 90)
{
	c = true;
}
if (b = 60)
{
	c = false;
}
if (c = true)
//if c is true then say "wow i figureed out the number or something
{
	cout << "Yes! I succefully guessed your number!" << endl;
	//I would like to include an "it only took *#* many tries for me to guess it!"
}
if (c = false)
//if c is false, then go back and and ask random number, but dont repeat numbers if possible
{
	cout << endl;
	cout << "Hmm, that was wrong, let me try again.";
}



cout << endl; 
system ("pause");
return 0;
}


And I considered scrapping most of the if/true-false lines and going with a loop where it breaks when true. I've been learning C++ for about a month and I've only written math programs, so I'm willing to take any suggestions.
"=" is the assignment operator. In each of your if statements you should use "==" (comparison) (without quotes)
Last edited on
it is better to use else if's after the first if, and else at the end, this website covers it

it would probably be better to do something like
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

#include <iostream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()

{

int var, boo=0;

cout<<"have a bash"<<endl;
cin>>var;


while(boo==0)
{
 int var2 = (rand()%10);
 
 if(var==var2){
   cout<<"well done to me!"<<endl;
   boo++;
   }
 else
   cout<<"woops"<<endl;

}

return 9;


}




each time the loop iterates a new random number is made, and the boo variable controls whether you leave the loop or not
except the <stdio.h> is pointless because <iostream> is the c++ equivalent. time.h >> ctime and stdlib.h>> cstdlib. and why are you using printf if your writing in c++
Topic archived. No new replies allowed.