I have following function within a class, which gets real part of a complex number:
double inputReal(){
double real;
cout << "Enter real part: ";
cin >> real;
bool compare = typeid(real) == typeid(double);
while (compare != true){
cout << "Incorrect input. Try another input: ";
cin >> real;
}
return real;
}
It should check if real is actually a number and if real isn't a number - it should get another input. However, if I try inputing something like: "aa" or "#@", it skips the while part and proceeds to output, ignoring getting the imaginary part:
double inputImag(){
double imaginary;
cout << "Enter imaginary part: ";
cin >> imaginary;
bool compare = typeid(imaginary) == typeid(double);
while (compare != true){
cout << "Incorrect input. Try another input: ";
cin >> imaginary;
}
return imaginary;
}
Output, if I try inputing "aa" as a real part:
Enter real part: aa
Enter imaginary part: Absolute value of complex number: 0
Argument of complex value: 0
Program ended with exit code: 0
The code works just fine, if I input correctly, so I suspect it's the comparison of types within those functions. I need to know what did I do wrong and how I can fix it. I'm relatively new to C++ and my college professor requires checking, that user inputs correctly. Feel free to ask anything, if the question seems unclear.