인프런 커뮤니티 질문&답변
제가 알고 있는게 맞는지 훈수좀요!!
작성
·
313
·
수정됨
답변 1
0
넵 :)
ignore과 fail의 경우에는..
std::를 붙여도 되고 떼도 됩니다.
아래 코드로 확인해보시겠어요?
#include <iostream>
using namespace std;
int getInt()
{
cout << "Enter an integer number : ";
int x;
cin >> x;
if (cin.fail())
{
cin.clear();
cin.ignore(32767, '\n');
cout << "Invaild number, please try again" << endl;
}
else
{
std::cin.ignore(32767, '\n');
return x;
}
}
char getOperator()
{
cout << "Enter an operator (+,-) : ";
char op;
cin >> op;
return op;
}
void printResult(int x, char op, int y)
{
if (op == '+') cout << x + y << endl;
else if (op == '-') cout << x - y << endl;
else
{
cout << "Invalid operator" << endl;
}
}
int main()
{
int x = getInt();
char op = getOperator();
int y = getInt();
printResult(x, op, y);
return 0;
}





호오 확인했습니다 감사합니다!