-
October 21st, 2013, 06:06 PM
#1
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!";
}
}
-
October 21st, 2013, 06:13 PM
#2
Re: If else statement displaying both options.
Originally Posted by Sensei Nacho
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.
-
October 21st, 2013, 07:02 PM
#3
Re: If else statement displaying both options.
OK thanks, but now it only displays "That is a small number."
-
October 21st, 2013, 07:04 PM
#4
Re: If else statement displaying both options.
Originally Posted by GCDEF
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?
-
October 21st, 2013, 07:45 PM
#5
Re: If else statement displaying both options.
-
October 21st, 2013, 08:56 PM
#6
Re: If else statement displaying both options.
Originally Posted by GCDEF
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!";
}
}
-
October 21st, 2013, 10:33 PM
#7
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?
-
October 22nd, 2013, 08:24 PM
#8
Re: If else statement displaying both options.
Originally Posted by Arjay
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?
-
October 23rd, 2013, 01:48 AM
#9
Re: If else statement displaying both options.
Originally Posted by Sensei Nacho
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
-
October 23rd, 2013, 04:41 PM
#10
Re: If else statement displaying both options.
Originally Posted by Arjay
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?
-
October 22nd, 2013, 05:00 AM
#11
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 22nd, 2013, 08:25 PM
#12
Re: If else statement displaying both options.
Originally Posted by 2kaud
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?
-
October 23rd, 2013, 01:12 AM
#13
Re: If else statement displaying both options.
Originally Posted by Sensei Nacho
How do I do this?
Begin with reading the Announcement: Before you post....
Victor Nijegorodov
-
October 23rd, 2013, 04:45 PM
#14
Re: If else statement displaying both options.
Originally Posted by VictorN
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.
-
October 23rd, 2013, 06:03 PM
#15
Re: If else statement displaying both options.
Originally Posted by Sensei Nacho
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|