# Thread: Newbie's If Statement

1. ## Newbie's If Statement

Here is my code:

Code:
```#include <iostream>

using namespace std;

// Hello World!

int main()
{
//Variables For Hello World!
int month, year, specialnumber;

cin>> month;
cin.ignore();
cin>> year;
cin.ignore();
cout<<"Your special number is "<< month * year + year - month<<"\n";
cin>> specialnumber;
cin.ignore();
if ( specialnumber < 150 ) {
cout<<"Your number is unlucky!\n";
}
else if ( specialnumber == 151 ) {
cout<<"Go buy a lottery ticket!\n";
}
else {
cout<<"Your special number is lucky.  Pet a wet dog!\n";
}
cin.get();

}```
It is supposed to change the inputted numbers and then check to see if the number is higher, lower, or equal to a certain number. Then it reads back whether or not your "lucky". It compiles fine, but it will not show the text after the number is calculated.

2. Code:
```#include <iostream>

using namespace std;

// Hello World!

int main()
{
//Variables For Hello World!
int month, year, specialnumber;

cin>> month;
cin>> year;
specialnumber = (month * year + year - month);
cout<<"Your special number is "<< specialnumber <<"\n";
if ( specialnumber < 150 )
{
cout<<"Your number is unlucky!\n";
}
else if ( specialnumber == 151 )
{
cout<<"Go buy a lottery ticket!\n";
}
else
{
cout<<"Your special number is lucky.  Pet a wet dog!\n";
}
cin.get();
return 0;
}```
you had cin >> specialnumber which was calling for a number to be inputted. Then that number would have been specialnumber.

3. Oh I see, thanks!

Also, while I am on this tutorial, what does ignore do? And what does return=0 do? I read the tutorial but it is a bit confusing in those areas.

4. "return 0;" tells the computer that your function executed with no errors.
"cin.ignore();" is used to skip remaining characters, how many times and what character, (I'm not 100% sure so don't quote me on that).

5. Originally Posted by rabbit
"return 0;" tells the computer that your function executed with no errors.
"cin.ignore();" is used to skip remaining characters, how many times and what character, (I'm not 100% sure so don't quote me on that).
without any arguments, ignore() means "throw away the next character" (quite often used to get rid of a newline, delimiter or whitespace).

With 2 arguments, it may throw away up-to a number of characters, or up-to a delimiter character of your choosing (whichever comes first)

the maximum size of the stream is found in the standard library -
std::numeric_limits<std::streamsize>::max()

(you can pass that as the first argument to ignore() if you wish to empty the stream - the 2nd argument will be the delimiter character)

6. That code automatically closes the window, within a second of entering the second amount, the birth year. Is there anyway to combat that?

7. three little letters: FAQ

8. Alright, alright, don't eat me.

But the thing is, it doesn't just pop-up at the start, it closes halfway through.

9. Try running it from a console. IE, cd dir-name; ./prog-name

This is all in the FAQ that Richie T posted.

10. The cin.ignore()'s were removed in the second bit of code, so the trailing newline after the user input was not ignored. Later, the cin.get() call (which usually would pause the program) immediately reads in the newline left from the user input and does not leave the window open until you hit enter again.

To fix it, put the ignore()'s back in, or at the very least, add one before the cin.get() at the end.

Popular pages Recent additions