I am very new to c++ and am trying to program a simple calculator. I would like to add an input option were if you type 'ans' it will replace the first number with the answer to the previous calculation. Unfortunately I already defined that input as a float which causes it not to read any string input. If I defined that input as a string, the calculator would crash because you cant multiply two strings. This is the piece of code I am stuck on.
#include <iostream>
int main()
{
float a;
std::cin >> a;
if (a == 'ans') {
std::cout << "this is a string input";
}
else {
std::cout << a * 2;
}
}
I believe that when ever I enter 'ans' it goes straight to the else part of the if and tries to multiply a * 'ans' because it returns 0.
If anybody has any idea... Thanks