Thread: if/ else if will not return else if when it is true

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    4

    if/ else if will not return else if when it is true

    I have a code which creates a random number, the user has to guess it in 10 tries. The first cout is the option to "Cheat? (Y/N)" which will display the secret number (the random number), mostly just for me to see whats happening.

    If the user selects Y the number is displayed. But also it will be displayed if they select N. This is one point of confusion.

    After this they are prompted to enter a guess. If the guess is wrong they will be asked if they want to try again. Whether they say Y or N, the program just quits and outputs nothing.

    Here is the code:
    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    using namespace std;
    
    int main() 
    { 
    	srand ( time(NULL) );
    	int number = (rand() % 15) + 1;
    	int guess; 
    	int trycount = 0;
    	char choice;
    	cout << "Cheat? (Y/N) \n";
    	cin >> choice;
    	if (choice == 'Y' || 'y')
    		cout << "The secret number is " << number << ".\n";
    	else if (choice == 'N')
    		cout << "Good for you.";
    
    	cout << "Please enter a guess: ";
    	cin >> guess;
    	while (guess != number && trycount < 10) 
    		{
    			int choice;
    			cout << "Wrong, guess again? (Y/N): ";
    			cin >> choice;
    			if (choice == 'Y')
    			{
    				cout << "Guess again: ";
    				cin >> guess;
    			}
    			else if (choice != 'Y')
    				break;
    			{
    			if (guess < number) 
    				cout << "Too low \n";
    			else if (guess > number)
    				cout << "Too high \n";
    			else (guess == number);
    				cout << "You guessed the number!";
    			}
    			trycount++; 
    		} 
    return 0;
    }
    Here are some outputs/inputs:

    Cheat? (Y/N)
    Y
    The secret number is 13.
    Please enter a guess: 6
    Wrong, guess again? (Y/N): Y

    The Debugger has exited with status 0.

    ----------

    Cheat? (Y/N)
    N
    The secret number is 11.
    Please enter a guess: 7
    Wrong, guess again? (Y/N): N

    The Debugger has exited with status 0.

    Can anyone give me a hint about what I am missing? I feel like it is a simple solution but I've tried it for a bit and I can't seem to figure out what is going wrong. Thank you!
    Last edited by nnizzle; 08-16-2010 at 08:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

Tags for this Thread