Help with my code

This is a discussion on Help with my code within the C++ Programming forums, part of the General Programming Boards category; Hello. I have to write a program that lets the user guess a number from a random number and say ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    Help with my code

    Hello. I have to write a program that lets the user guess a number from a random number and say if its too high/low/correct.

    However, the part I am stuck on is that after the user guesses it correctly, I need to have it ask if the user wants to play again, whereas the user enters y for yes and n for no.

    Here is what my code is so far:

    Code:
    #include <iostream>
    
    #include <cstdlib>
    
    using namespace std;
    
    int main ()
    
    {
    
    	int a;
    
    	int b;
    
    	srand(a);
    
    	a = rand()%1001;
    
    	cout << "I have a number between 1 and 1000." << endl;
    
    	cout << "Can you guess my number?" << endl;
    
    	
    	while (1)
    
    	{
    
    	char again='';
    
    	do
    
    	{
    
    	cout << "Please type your first guess." << endl;	
    
    	cin >> b;
    	
    
    	if (b > a)
    
    	
    		cout << "Too high. Try again" << endl;
    
    
    	if (b < a)
    
    		cout << "Too low. Try again." << endl;
    
    	} while (b != a);
    
    		cout << "Excellent! You guessed the number!" << endl;
    
    	while (again != 'y' || again != 'n')
    
    	{
    		cout << "Would you like to play again (y or n)?" << endl;
    
    		cin >> again;
    	
    	}
    
    	if (again == 'n')
    
    		break;
    
    	}
    	
    	return 0;
    }
    I am getting an empty character constant in regards to this line:

    Code:
    char again='';
    How do I go about fixing it?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Make it a character, like 'a' or 'q' or even ' '. (That is, with a space between the quotes.)

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    How do I go about fixing it?
    What are you trying to assign to this variable? If you just want to assign some default value that is not 'n' or 'y', then just do:
    Code:
    char again = 'x';
    or
    Code:
    char again = '\0';

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    3
    Quote Originally Posted by tabstop View Post
    Make it a character, like 'a' or 'q' or even ' '. (That is, with a space between the quotes.)
    That seems to have worked, but when it prompts 'Would you like to play again?', entering y or n does not do anything but have it show 'Would you like to play again?'

    What is wrong with my while loop?

    Code:
    while (again != 'y' || again != 'n')
    
    	{
    		cout << "Would you like to play again (y or n)?" << endl;
    
    		cin >> again;
    	
    	}
    
    	if (again == 'n')
    
    		break;

  5. #5
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    If a character is equal to 'y', it cannot be equal to 'n'. and vice-versa (Think about what || does).
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  6. #6
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    again cannot be both y and n at the same time, which is the only time that condition will be false
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    3
    I've changed my code:

    Code:
    #include <iostream>
    
    #include <cstdlib>
    
    using namespace std;
    
    int main ()
    
    {
    
    	int a;
    
    	int b;
    
    	srand(a);
    
    	a = rand()%10 + 1;
    
    	cout << "I have a number between 1 and 1000." << endl;
    
    	cout << "Can you guess my number?" << endl;
    
    	
    	while (1)
    
    	{
    
    	char again = '\0';
    
    	do
    
    	{
    
    	cout << "Please type your first guess." << endl;	
    
    	cin >> b;
    	
    
    	if (b > a)
    
    	
    		cout << "Too high. Try again" << endl;
    
    
    	if (b < a)
    
    		cout << "Too low. Try again." << endl;
    
    	} while (b != a);
    
    	{
    
    		cout << "Excellent! You guessed the number!" << endl;
    
    		cout << "Would you like to play again (y or n)?" << endl;
    
    		cin >> again;
    	}
    
    
    	if (again == 'n')
    
    		break;
    
    	}
    	
    	return 0;
    }
    now when it runs the program again, the number is still the same as before, how can i fix that?

  8. #8
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    change a inside your loop
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 09:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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