|
-
January 22nd, 2011, 12:28 AM
#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?
-
January 22nd, 2011, 04:25 AM
#2
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.
-
January 24th, 2011, 12:00 PM
#3
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 % 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|