Hello guys I was wondering how I could allow only integers to be entered. I've already tried it in the code below but when enter I enter a character is tells me that I entered a negative number. Can somebody point out what I'm doing wrong.
#include <iostream>
usingnamespace std;
int main()
{
int x;
cout << "Please enter an integer: ";
cin >> x;
if (x>0)
{
cout << "The number you entered is positive." << endl;
}
elseif (x<0)
{
cout << "The number you entered is negative." << endl;
}
elseif (x==0)
{
cout << "The number is you entered is 0." << endl;
}
else
{
cout << "Invalid integer." << endl;
}
system ("pause");
return 0;
}
if (!(cin >> x))
{
cout << "Invalid integer." << endl;
}
elseif (x>0)
{
cout << "The number you entered is positive." << endl;
}
elseif (x<0)
{
cout << "The number you entered is negative." << endl;
}
else
{
cout << "The number is you entered is 0." << endl;
}
#include <iostream>
#include "conio.h"
int main()
{
int x;
std::cout << "Please enter an integer: ";
if (!(std::cin >> x))
{
std::cout << "Invalid integer." << std::endl;
}
elseif (x>0)
{
std::cout << "The number you entered is positive." << std::endl;
}
elseif (x<0)
{
std::cout << "The number you entered is negative." << std::endl;
}
else
{
std::cout << "The number is you entered is 0." << std::endl;
}
_getch(); //instead of system pause
return 0;
}
Use #include <conio.h> and _getch(); instead of system("pause").
#include <iostream>
usingnamespace std;
int main ()
{
int x;
cout << "Please enter an integer; " << endl;
if (!(cin>>x))
{
cout << "Invalid integer" << endl;
}
elseif (x>0)
{
cout << "The number you entered is positive." << endl;
}
elseif (x<0)
{
cout << "The number you entered is negative." << endl;
}
else
{
cout << "The number you entered is 0." << endl;
}
system ("pause");
return 0;
}