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!";
}
}
Re: If else statement displaying both options.
Quote:
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.
Re: If else statement displaying both options.
OK thanks, but now it only displays "That is a small number."
Re: If else statement displaying both options.
Quote:
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?
Re: If else statement displaying both options.
Re: If else statement displaying both options.
Quote:
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!";
}
}
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?
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().
Re: If else statement displaying both options.
What is the purpose of the following statement ?
What does it do ? And how does it effect the input stream ? Hint: output the value of "x"
after you read it in.
Edit: Sorry ... I did not look at all the posts. I see that ignore() was already mentioned.
Re: If else statement displaying both options.
Quote:
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?
Re: If else statement displaying both options.
Quote:
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?
Re: If else statement displaying both options.
Quote:
Originally Posted by
Philip Nicoletti
What is the purpose of the following statement ?
What does it do ? And how does it effect the input stream ? Hint: output the value of "x"
after you read it in.
Edit: Sorry ... I did not look at all the posts. I see that ignore() was already mentioned.
As far as I can tell, in a console program, it makes it so enter doesn't close the console.
Re: If else statement displaying both options.
Quote:
Originally Posted by
Sensei Nacho
How do I do this?
Begin with reading the Announcement: Before you post....
Re: If else statement displaying both options.
Quote:
Originally Posted by
Sensei Nacho
As far as I can tell, in a console program, it makes it so enter doesn't close the console.
When you don't understand what something means, try to look it up in msdn. Do a search in bing or google for "std::cin:ignore".
In the results look for something that contains msdn in the url.
The second link in the bing results is basic_istream::ignore which may not look promising, but it does have msdn... in the url.
http://msdn.microsoft.com/en-us/library/3w23zf49.aspx
A good place to start.
Pay particular attention to the sample code at the bottom of the page - especially the output that is shown after the code snippet.
Re: If else statement displaying both options.
Quote:
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
Re: If else statement displaying both options.
Quote:
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?
Re: If else statement displaying both options.
Quote:
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.
Re: If else statement displaying both options.
Quote:
Hi, I am new to coding
You might like to look at these sites
http://www.learncpp.com/
http://www.cplusplus.com/doc/tutorial/
Going back to your issue re numbers being considered small.
As suggested earlier, the problem is cin.ignore() which ignores the next character. So if you entered 21, it would be considered as 1 and 67 would be considered as 7 etc.
Re: If else statement displaying both options.
Quote:
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.
Re: If else statement displaying both options.
Quote:
Originally Posted by
Sensei Nacho
Quote:
Before posting, please format your code properly and use code tags. Go advanced, select the code and click '#'.
How do I do this?
What don't you understand about the instruction given? Press the 'Go Advanced' button, select the code using normal selection process and then click the '#' button.