Look what i did!!

This is a discussion on Look what i did!! within the C++ Programming forums, part of the General Programming Boards category; It's okay. It's a very easy mistake for a person new to C++ to make....

  1. #16
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    It's okay. It's a very easy mistake for a person new to C++ to make.

  2. #17
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Code:
      if (false) {
      cout << "Way off";
      }
    This code will never execute. You might want to put the else back in there like you originally had it.
    Code:
      else {
      cout << "Way off";
      }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #18
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    OK i got it, thanks all!!!!!!
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() 
    {
      int nunb;  
      cout << "Number:";
      cin>> nunb;
      cin.ignore();
      if ( nunb == 20 ){
      cout << "You read my mind!";
      }
      else {
      cout << "Way off";
      }
      cin.get();
    }
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  4. #19
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Or you could put:

    Code:
    if(nunb!=20)
    {
         cout<<"Way off!";
    }
    You might want to put a loop in there to give the user multiple guesses:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() 
    {
      int nunb;
      bool guess = false;  
      while(guess!=true){
      cout << "Number:";
      cin>> nunb;
      cin.ignore();
      if ( nunb == 20 ){
      cout << "You read my mind!";
      guess = true;
      }
      if (nunb!=20) {
      cout << "Way off"<<endl;
      }
      }
      cin.get();
    }

  5. #20
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() 
    {
      int nunb;  
      cout << "Number:";
      cin>> nunb;
      cin.ignore();
      if ( nunb == 20 ){
      cout << "You read my mind!";
      }
      if (false) {
      cout << "Way off";
      }
      cin.get();
    }
    No offense, but some of this doesn't make any sense.

    Code:
      if (false) {
      cout << "Way off";
      }
    This statement will NEVER be evaluated. "false" will NEVER evaluate to true and therefore the contents of the if statement will never be executed.

    Try this:

    else {
    cout << "Way off";
    }

    -Edit-

    Sorry about that, I was slow to respond! Old news - move along, nothing to see here

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

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