Thread: A question on what i think is my if statement

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    35

    A question on what i think is my if statement

    in this little program i am trying to make, just to help me get used to programing in general. I can not get the second part to work correctly. I can't get the first response to happen on Y and the second to happen on N, just the second happens on either Y or N. i know there is no N in the code but when i add an N i just get the first response.

    any help is appreciated. im teaching my self so when im stuck the way for me to learn is to see it and not to make the mistake again.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    float number;
    
    cout<<"What is your favorite number? ";
    cin>>number;
    cin.ignore();
    cout<<"Why is your favorite number " << number << "? Mentally tell me. HIT ENTER WHEN FINISHED\n" ;
    cin.ignore();
    
    char Y = 0;
    
    cout<<"Are you sure your favorite number isn't 21? Y/N:   ";
    cin>>Y;
    cin.ignore();
    if ( Y == 0 ) {
        cout<<"I think we will get along well. \n";
    }
    else if ( Y != 0 ) {
        cout<<"You should re-evaluate your decision. \n";
    }
    cin.get();
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So after cin>>Y your variable Y will hold what they typed in. It might hold 'Y', or 'N', or 'Q', or whatever the first character your user typed in. It is unlikely -- maybe even impossible -- for Y to have the value 0. (Note that if they type 0, Y will have the value '0', which is not the same thing.)

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    alright, but how to i sign Y and N to its own seperate responses? i tried identifying both seperatly char Y; and then next line down char N; but that didnt work

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, it's not "char Y" that determines what happens - it's the other side of your if-statement, where you currently have 0 that should change to 'Y' or 'N'.
    you may want to change the variable name "Y" to "answer" or something else similarly descriptive - but from a functional perspective, the variables name has absolutely no meaning - "mmmasagsdahjad" is just as good as "answer" or "Y" - but the first one will be a lot harder to remember next time you need to type it!]
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    alright i changed it to this but now it tell me i still have to define the Y, y, N, n. i think i might be missing something or i am just looking to deep?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    float number;
    
    cout<<"What is your favorite number? ";
    cin>>number;
    cin.ignore();
    cout<<"Why is your favorite number " << number << "? Mentally tell me. HIT ENTER WHEN FINISHED\n" ;
    cin.ignore();
    
    char answer;
    
    cout<<"Are you sure your favorite number isn't 21? Y/N:   ";
    cin>>answer;
    cin.ignore();
    if ( answer == Y || answer == y ) {
        cout<<"I think we will get along well. \n";
    }
    else if ( answer == N || answer == n ) {
        cout<<"You should re-evaluate your decision. \n";
    }
    cin.get();
    
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Y is a variable. 'Y' is a constant representing the character Y that you get when reading a character from the console.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    Wow i cant believe it was really that simple. all that time and just needed a '. i will never forget that. thanks for putting up with me

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brian75 View Post
    Wow i cant believe it was really that simple. all that time and just needed a '. i will never forget that. thanks for putting up with me
    Yes, but as you probably understand, YOU doing the work will mean that you DO NOT forget it. If I had just posted the correction, you would have pasted it back in again, and never learnt it. This is exactly why I (and many others here) do not just post the corrected result of someone elses code. If you have to work at fixing it, it's much more likely to stick around in your head, because many more neurons where involved.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    well that for sure was something i wont forget, thanks. i cant even remember all the combinations i tried.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about break in switch statement
    By alone2002dj in forum C Programming
    Replies: 1
    Last Post: 12-09-2007, 08:42 PM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM