프로그램에서 x를 정수로 허용하고 다른 정수 y를 요구하는 중입니다. 그러나 내가 x에 부동 소수점을 입력하면 입력의 소수 부분을 가져 와서 y 값이됩니다. 나는 나의 실수를 확신 할 수 없다. C++의 모든 콘솔 입력이 다음의 getInt() 다음 작업을 수행 할 것입니다 방법에 따라 문자열로 취급되기 때문에C++ 사용자가 정수 대신 부동 소수점을 입력합니다.
#include <iostream>
#include <string>
#include <limits>
using namespace std;
int getInt()
{
int x = 0;
while (!(cin >> x))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Please input a proper 'whole' number: " << endl;
}
return (x);
}
int toobig()
{
cout << "Your number is too large, please enter something smaller: " << endl;
int x = getInt();
return (x);
}
int toosmall()
{
cout << "your number is negative, please enter a positive number: " << endl;
int x = getInt();
return (x);
}
int main()
{
cout << "your number please:-" << endl;
int x = getInt();
if (x>100000)
{
toobig();
}
else if (x<0)
{
toosmall();
}
int y = 0;
cout << "enter y " << endl;
cin >> y;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
system("PAUSE");
return 0;
}
"Y"값은 무엇을 의미합니까? "123.456"을 입력하고이를 정수로 읽으면 123은 정수로 읽혀질 것입니다. – user3344003
사용자가 첫 번째 정수 뒤에'. '을 입력했는지 확인할 수 있고 그 경우 오류를 반환 할 수 있습니까? – KABoissonneault
예 x는 123입니다. 그러나이 값을 입력하면 프로그램은 즉시 X = 123 Y = 0을 인쇄합니다. –