i want to create a porgram where user enters a num then enter an operation and it performs operation and ; ends the operations so like
5
+
2
-
7
;
would get 0
also if character is ^ its pow 2
5
^
;
is 25
and
5
^
-
25
;
is 0;
im getting stuck with the code i have so far
#include <iostream>
usingnamespace std;
int main()
{
int num; // enter numbers
char character; // character + or - or ^
int total = 0;
cin>>num;
total += num; // total sum
while(cin>>character>>num){
if(character == ';'){// new lines
cout << total << endl;
total = 0;
total = total + num;
}
if(character == '-'){// subtraction
total = total - num;
}
//if (character == '^'){//square
//total = total * num;
//}
elseif(character == '+'){// addition
total = total + num;
}
}
cout << total << endl;//print out
return 0;
}
is there a method to like instead of while(cin>>character>>num) to check while(as long as user is inputting something) char or num order does not matter like a char can follow another char then a num?
// Example program
#include <iostream>
usingnamespace std;
int main()
{
int result = 0;
int num;
if (!(cin >> num))
{
cout << "Error: Input failure\n";
return 1;
}
result = num;
while (true)
{
char operation;
if (cin >> operation)
{
if (operation == '^')
{
cout << "You entered ^. Squaring the current number\n";
result *= result;
}
elseif (operation == ';')
{
cout << "You entered ;. Loop will stop.\n";
break;
}
elseif (operation == '+')
{
if (cin >> num)
{
result += num;
}
else
{
cout << "Error: Input failure\n";
return 1;
}
}
elseif (operation == '-')
{
if (cin >> num)
{
result -= num;
}
else
{
cout << "Error: Input failure\n";
return 1;
}
}
cout << "Result so far: " << result << '\n';
}
else
{
cout << "Error: Invalid operation\n";
return 1;
}
}
cout << "Final result: " << result << '\n';
return 0;
}
3
+
4
Result so far: 7
+
4
Result so far: 11
-
4
Result so far: 7
^
You entered ^. Squaring the current number
Result so far: 49
;
You entered ;. Loop will stop.
Final result: 49
so the ^operation takes the current number and squares it, like 5+6^ would be 41; i have this code so far which works for all case except cases such as 5^+5^ or 5+5^, 5-5^, if i tried modifying with goto my (^) case but then cases such as 5^+5+5 would not work
you are making a tiny language, really, and a parser for it.
When you make a language, you might like to have exceptions from the normal behavior; your ^ operation is an exception to the normal behavior. That is fine, but the thing to do with exceptions is to do those last. Get it working for everything else first, in other words.
Does it work for everything except expressions with the ^ in them now?
become 4 + 9 = 13,
then you need to keep memory to hold the last number entered before parsing the '^', because the 3 is entered before it's known whether or not it should be added to the 4.
that may be overkill but it can't hurt to see it.
I think the 'keep the last one in memory' is the key to setting up the special behavior for ^
grumble ... algebraic parsing nightmares... when the student is ready, the reverse polish is waiting...