이 코드의 몇 가지 변형을 시도해 보았습니다. 나는 C++에 대해 매우 익숙하지 만 아직 설명 할 수는 없다. 이 코드는 간단한 계산기의 일부입니다. 기본적으로 사용자에게 두 개의 숫자를 입력하도록 요청한 다음 (플로트 수 있음) 사용자에게 수학 연산자를 요청한 다음 작업을 수행합니다. 사용자가 숫자가 아닌 것을 입력 한 다음 if 문에서 다시 번호를 입력하라는 메시지가 표시되면 숫자를 입력하면 콘솔에 "-9.25596e + 061"이 인쇄됩니다. 당신은 getUserInput
에 재귀 호출에 return
을 추가 놓친입력 된 값이 float인지 확인하고 cin을 지우십시오.
#include "stdafx.h"
#include <iostream>
#include <cctype>
using namespace std;
double getUserInput()
{
//Make double, assign double, return double if number
double dUserInput;
//checks if it failed to cin
if (!(cin >> dUserInput))
{
//statement is true
cin.clear();
cin.ignore(99999, '\n');
cout << "You did not enter a number, please try again: ";
getUserInput();
}else
//statement if false
cout << "Number entered"; //for debugging purposes
return dUserInput;
}
값을 인쇄하는 위치가 표시되지 않습니다. –
소수점이있는 숫자와 그렇지 않은 숫자를 구별합니까? 예를 들어, 숫자 2는 'double'과 'int'일 수 있습니다. –