y/n command

GAME CODE : GUESSING THE NUMBER:
.
.
.
.

if(num == randnum)
{
printf("Excellent! You guessed the number with %d tries!\n\n", tries );
printf("Would you like to play again (y or n)? ");
scanf("%c", &choice);

THE CODE IS DONE THIS FAR BUT HOW TO GET CHAR INPUT ie y/n :

if (choice == n )

THIS IS THE WAY I AM TRYING BUT IT IS NOT WORKING.....
Replace
if (choice == n )
With
if (choice == 'n' )

You want to check if choice is equal to the character n, not the value of the variable n.
the assignment operation == shows an error in this case

char choice as been declared in the beginning...
plus how to exit the console when n is pressed ?
Does this work on your system?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream> 

int main()
{
	char choice;
	std::cin >> choice;

	if (choice == 'n')
		std::cout << "Choice was n";
	else
		std::cout << "Choice was y";
	return 0;
}
This should work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream> 

int main()
{
	char choice = 'y';
	while (choice != 'n')
	{
		//////////////////////////////////
		// Your random guess code here. //
		//////////////////////////////////

		std::cout << "Would you like to play again? (y/n): ";
		std::cin >> choice;	// If you enter anything but 'n' it will repeat.
	}
}
Last edited on
this might work but i am using c
if else while goto ... stuff ..


scanf("%c", &choice);
if(choice =='n') exit(-1);

is this correct? and can i use goto function for playing again if choice is y ?

and can you explain what is wrong with choice == 'n'???
exit, and goto are not good, even in C. There should be nothing wrong with choice == 'n'. It means that you've probably not declared choice as you've stated above or maybe you have brackets that aren't in the right place, or maybe it's not inside a function, or there could be any number of things wrong with that line. The == operator itself is not wrong. We'd have to see the code to tell you exactley what is wrong.

Use this then:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h> 

int main()
{
	char choice = 'y';
	while (choice != 'n')
	{
		//////////////////////////////////
		// Your random guess code here. //
		//////////////////////////////////

		printf("Would you like to play again? (y/n): ");
		scanf("%c",&choice);	// If you enter anything but 'n' it will repeat.
	}
}

HMM I TRIED THIS .. BUT IT REPEATS THE ONLY FROM EXCELLENT ...... AND SHOWS IT TWICE


#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <conio.h>




int main(void)
{
int num;
int randnum;
int tries;



randnum = 1 + rand() % 1000;
tries = 0;

printf("I have a number between 1 and 1000.\n");
printf("Can you guess it?\n");
printf("Please type your guess: ");
scanf("%d", &num);


char choice = 'y';

while (choice != 'n') {

while(num != randnum)
{
if(num < randnum)
{
printf("Too low. Try again.\n\n");
printf("Next guess:\t ");

scanf("%d", &num);
printf("\n");
}

if(num > randnum)
{
printf("Too high. Try again.\n");
printf("Next guess: \t");

scanf("%d", &num);
printf("\n"); }

tries ++;}

if(num == randnum)
{
printf("Excellent! You guessed the number with %d tries!\n\n", tries );

printf("Would you like to play again (y or n)? ");
scanf("%c", &choice);

}}






getchar();
getchar();

return 0;
}



Topic archived. No new replies allowed.