If else statement displaying both options.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: If else statement displaying both options.

Hybrid View

  1. #1
    Join Date
    Oct 2013
    Posts
    9

    Question If else statement displaying both options.

    Hi, I am new to coding and have a question with my if else statement. This is a VERY simple program that I have made something like in Javascript before, but when I do it in C++ it does not work correctly. It displays both the options. Here is my code.
    // Practice.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <iostream>

    using namespace std;

    int main()
    // Asking for a number
    {
    int x;
    cout << "Give me a number.";
    cin.ignore();
    cin >> x;
    if (x < 25)
    {
    cout << "That is a small number.";
    }
    else (x >= 25);
    {
    cout << "That is a big number!";
    }
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: If else statement displaying both options.

    Quote Originally Posted by Sensei Nacho View Post
    Hi, I am new to coding and have a question with my if else statement. This is a VERY simple program that I have made something like in Javascript before, but when I do it in C++ it does not work correctly. It displays both the options. Here is my code.
    // Practice.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <iostream>

    using namespace std;

    int main()
    // Asking for a number
    {
    int x;
    cout << "Give me a number.";
    cin.ignore();
    cin >> x;
    if (x < 25)
    {
    cout << "That is a small number.";
    }
    else (x >= 25);
    {
    cout << "That is a big number!";
    }
    }
    You either need else without the condition, or add an if after the else. By itself (x >= 25) doesn't do anything. Further, since you have a semi-colon after your (x >=25) statement, the big number part isn't even part of the condition. It will always output.

  3. #3
    Join Date
    Oct 2013
    Posts
    9

    Re: If else statement displaying both options.

    OK thanks, but now it only displays "That is a small number."

  4. #4
    Join Date
    Oct 2013
    Posts
    9

    Re: If else statement displaying both options.

    Quote Originally Posted by GCDEF View Post
    You either need else without the condition, or add an if after the else. By itself (x >= 25) doesn't do anything. Further, since you have a semi-colon after your (x >=25) statement, the big number part isn't even part of the condition. It will always output.
    Thanks but now it only displays "That is a small number.". What am I doing wrong?

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: If else statement displaying both options.

    Post your current code.

  6. #6
    Join Date
    Oct 2013
    Posts
    9

    Re: If else statement displaying both options.

    Quote Originally Posted by GCDEF View Post
    Post your current code.
    // Practice.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <iostream>

    using namespace std;

    int main()
    // Asking for a number
    {
    int x;
    cout << "Give me a number.";
    cin.ignore();
    cin >> x;
    if (x < 25)
    {
    cout << "That is a small number.";
    }
    else
    {
    cout << "That is a big number!";
    }
    }

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,967

    Re: If else statement displaying both options.

    In c++ always initialize your variables. Also step through your code or use trace statements to see what is wrong. Btw what does cin.ignore do?

  8. #8
    Join Date
    Oct 2013
    Posts
    9

    Re: If else statement displaying both options.

    Quote Originally Posted by Arjay View Post
    In c++ always initialize your variables. Also step through your code or use trace statements to see what is wrong. Btw what does cin.ignore do?
    What do these mean?

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,967

    Re: If else statement displaying both options.

    Quote Originally Posted by Sensei Nacho View Post
    What do these mean?
    Initialize your variables means to declare them and then set them. You can do this all in one step. In C++, initializing variables prevents a variable from starting out with garbage and it's a good practice to get into (because they are often a source of bugs).

    Code:
    int x;  // uninitialized variable it's anyone's guess what its starting value will be.
    
    int y = 0;  variable declared and initialized to 0

  10. #10
    Join Date
    Oct 2013
    Posts
    9

    Re: If else statement displaying both options.

    Quote Originally Posted by Arjay View Post
    Initialize your variables means to declare them and then set them. You can do this all in one step. In C++, initializing variables prevents a variable from starting out with garbage and it's a good practice to get into (because they are often a source of bugs).

    Code:
    int x;  // uninitialized variable it's anyone's guess what its starting value will be.
    
    int y = 0;  variable declared and initialized to 0
    So if I want the variable to be something the user enters, how do I initialize it?

  11. #11
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,001

    Re: If else statement displaying both options.

    Before posting, please format your code properly and use code tags. Go advanced, select the code and click '#'.

    Arjay has given you the hint asking about cin.ignore().
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  12. #12
    Join Date
    Oct 2013
    Posts
    9

    Re: If else statement displaying both options.

    Quote Originally Posted by 2kaud View Post
    Before posting, please format your code properly and use code tags. Go advanced, select the code and click '#'.

    Arjay has given you the hint asking about cin.ignore().
    How do I do this?

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011

    Re: If else statement displaying both options.

    Quote Originally Posted by Sensei Nacho View Post
    How do I do this?
    Begin with reading the Announcement: Before you post....
    Victor Nijegorodov

  14. #14
    Join Date
    Oct 2013
    Posts
    9

    Re: If else statement displaying both options.

    Quote Originally Posted by VictorN View Post
    Begin with reading the Announcement: Before you post....
    Sorry, I despise forums and only did this one to get help from the community because I thought it would be very useful, so I had no idea about things like this.

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,967

    Re: If else statement displaying both options.

    Quote Originally Posted by Sensei Nacho View Post
    Sorry, I despise forums and only did this one to get help from the community because I thought it would be very useful, so I had no idea about things like this.
    Not sure what there is to despise about forums where volunteers help folks solve issues without compensation or in many cases, thanks.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center