Thread: Newbie's If Statement

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    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;
    
      cout<<"Please enter your birth month:";
      cin>> month;
      cin.ignore();
      cout<<"Please enter your birth year:";
      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. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    16
    Code:
    #include <iostream>
    
    using namespace std;
    
    // Hello World!
    
    int main()
    {
        //Variables For Hello World!
        int month, year, specialnumber;
    
      cout<<"Please enter your birth month:";
      cin>> month;
      cout<<"Please enter your birth year:";
      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. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    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. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    16
    "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. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote 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. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    That code automatically closes the window, within a second of entering the second amount, the birth year. Is there anyway to combat that?
    Last edited by Gallivan; 03-01-2006 at 08:32 PM.

  7. #7
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    three little letters: FAQ
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Alright, alright, don't eat me.

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

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Try running it from a console. IE, cd dir-name; ./prog-name

    This is all in the FAQ that Richie T posted.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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 subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM