CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2011
    Posts
    1

    C++ Remainder Basic Problem...

    include <iostream>
    using namespace std;

    int main() {


    int n;

    cout << "Enter a Number and press enter";
    cin >> n;

    if (n % 2 == 0 )
    cout << "The Number is even";
    else
    cout << "The Number is odd";

    return 0;
    }

    instead of showing me the cout it just end after cin >> enter.

    adding system("pause"); do make it work but isnt system("pause") bad?

    i was told to use cin.get(); instead but in this case cin.get(); doesnt work. WHY?

  2. #2
    Join Date
    Oct 2010
    Location
    Australia
    Posts
    98

    Re: C++ Remainder Basic Problem...

    Try something like this:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int n;
        while(n != 0)
        {
            cout << "Enter a Number and press enter :";
    
            cin >> n;
            cout << endl;
    
            if (n % 2 == 0 )
            cout << "The Number is even" << endl;
    
            else
            cout << "The Number is odd" << endl;
        }
    
        return 0;
    }
    It is not perfect but you have something to work with now.

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: C++ Remainder Basic Problem...

    That changes the OP's original intent by looping until the user enters a '0' (which he/she won't know will terminate the program, as it was never stated). The reason the OP's original code (without the 'system' call) doesn't work is that the "\n" is still sitting in the cin stream. To the OP, try this:
    Code:
    include <iostream>
    using namespace std;
    
    int main() {
       int n;
    
       cout << "Enter a Number and press enter";
       cin >> n;
       cin.ignore(10000, '\n');
    
       if (n &#37; 2 == 0 )
          cout << "The Number is even";
       else
          cout << "The Number is odd";
    
       cin.get();
       return 0;
    }
    Viggy

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured