what did i do wrong ?

This is a discussion on what did i do wrong ? within the C++ Programming forums, part of the General Programming Boards category; i design a guessing game #include <iostream.h> int main() { int guess; cout << "Guess what number i am thinking... ...

  1. #1
    Unregistered
    Guest

    Unhappy what did i do wrong ?

    i design a guessing game

    #include <iostream.h>

    int main()

    {
    int guess;
    cout << "Guess what number i am thinking... : ";
    cin >> guess;
    if (guess != 56)
    {
    if (guess != 56)
    cout << "Guess Again....: ";
    cin >> guess;
    }
    else if (guess == 56)
    {
    cout << "You are correct: ";
    }
    return 0;
    }

    when i run guess.exe the statement guess what number i am thinkning comes up

    i input a number thats not 56 i get guess again
    if i enter the any number weither or not it is correct it goes to the prompt c:\debug>_ instead of letting me guess again or outputing the You are correct statement.

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    1)This:
    Code:
    if (guess != 56) 
    { 
    if (guess != 56)
    isn't right. Take out the second if. Right now, you're just checking it twice. It won't kill anything, but there's no reason for it to be there.

    2)For what you're trying to do, you'll want smth more like this:
    Code:
    #include <iostream.h> 
    
    int main() 
    { 
    int guess = 0; 
    while (guess != 56)
    {
       cout << "Guess what number i am thinking... : "; 
       cin >> guess; 
       if (guess == 56)
           cout << "You are correct!";
       else
           cout << "Guess again"<<endl;
    }
    return 0;
    }

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    try this....
    Code:
    #include <iostream.h> 
    #include<stdlib.h>
    int main() 
    
    { 
    int guess; 
    cout << "Guess what number i am thinking... : "; 
    cin >> guess; 
    if (guess != 56) 
    { 
    cout << "Guess Again....: "; 
    cin >> guess; 
    } 
    else if (guess == 56) 
    { 
    cout << "You are correct: "; 
    } 
    system("PAUSE"); // this will alow you to see the output
    return 0; 
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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