calcualtor

closed account (jGAShbRD)
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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace 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;
      //}    
      else if(character == '+'){// addition
            total = total + num;
      }
   }
   cout << total << endl;//print out 
   
   return 0;
}

while(cin>>character>>num) expects two things, but the final ';' is just one thing.

Your code will run if you enter ";0" instead of just ";" as the terminator.
The problem is that using '^' and ';' does not following the letter num letter num letter num pattern, because there is no number that follows ^ or ;

You need to only take in the character at first, and depending on that character you decide if the user also needs to then enter a number.
Last edited on
closed account (jGAShbRD)
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?
If it makes it clearer, you can just make the loop be while (true) { ... } and then you handle all the logic inside the loop itself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Example program
#include <iostream>
using namespace 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;
            }
            else if (operation == ';')
            {
                cout << "You entered ;. Loop will stop.\n";
                break;
            }
            else if (operation == '+')
            {
                if (cin >> num)
                {
                    result += num;
                }
                else
                {
                    cout << "Error: Input failure\n";
                    return 1;
                }
            }
            else if (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
Last edited on
closed account (jGAShbRD)
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<cmath>

int main(){
  int total=0;
  int x,y;
  char c;
  std::cin>>x;
  total=x;
  while(std::cin>>c){
    A:if(c=='^'){
      x=pow(x,2);
      total=x;
    }
    B:if(c=='+'){
      std::cin>>x;
      total+=x;
    }
    C:if(c=='-'){
      std::cin>>x;
      total-=x;
    }
    if(c==';'){
      std::cout<<total<<std::endl;
      return main();
    }
  } 
}
closed account (jGAShbRD)
i know that because in my (c=='^') i set total to equal the value of x^2, but if i dont other cases 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?
closed account (jGAShbRD)
yeah
If you have
4
+
3
^
;
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.

In general, if you want to have proper parsing w/ order of operations, look into an algorithm like the shunting-yard algorithm. https://en.wikipedia.org/wiki/Shunting-yard_algorithm
Last edited on
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...
Last edited on
Topic archived. No new replies allowed.