So I'm working on building a calculator that mimics the ones we use everyday. What I have shown is the logic within my function. Previously when I had my cout lines (deleted, not shown anymore) to see if my "y" was being correctly stores, and my two variables, finalNum1 & 2, everything was working, but when going back, trying to add new cout lines, nothing prints. If there are more issues, feel free to point them out, but this code is unfinished, my main concern is nothing is printing, I understand this code still needs work. If anyone can help that'd be greatly appreciated!
int Calculator::calculate()
{
if (userInput[0] != 'q' || 'Q') // Checks for user input "Quit" or "quit"
{
int stringSize;
std::cin >> userInput; // User Input
stringSize = userInput.length();
int y = 0;
while (y < stringSize)
{
if (isdigit(userInput[y]))
{}
else
{
posi = y;
}
y++;
}
first = userInput.substr(0,posi);
second = userInput.substr(posi+1,y);
finalNum1 = std::stoi(first); // Sigbart error
finalNum2 = std::stoi(second);
std::cout << finalNum1 + finalNum2; // won't print
switch (userInput[posi])
{
case '+':
std::cout << finalNum1 + finalNum2;
break;
case '-':
std::cout << finalNum1 - finalNum2;
break;
case '*':
std::cout << finalNum1 * finalNum2;
break;
case '/':
std::cout << finalNum1 / finalNum2;
break;
case '%':
std::cout << finalNum1 % finalNum2;
break;
}
}
else
std::cout << "Goodbye";
return 0;
}
For printing integer it is easier if you cast them to string.
using namespace std;
float tmp = finalNumber1 + finalNumber2;
string out = to_string(tmp);
cout << tmp << endl;
Then another thing. You should use for
instead of while
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.