Calculator's deffect ( not complete character intake )

This program, is the simple calculator. I'd a task, to write something like this, but the "original" one is a little bit more complicated. Problem in input ( again :D ), when i enter decimal numbers, the result if 0, in any case. How should I take the input?
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
using namespace std;
 
 float number()
 {
       int res = 0;
       for (;;)
       {    
           char c = cin.get();
           if (c == '=') {
            cout << " You have entered wrong symbol, '=', \n program will be terminated."<< endl;
            system("pause");
            exit(1);
                          }
             if ( c >= '0' && c <='9')
           res=res*10 + c - '0'; 
           else
           {
               cin.putback(c);
               return res;
           }
      }
  }

float expr();
float skobki()
      {
       char c = cin.get();
      
            if ( c == '(')
                 {
                  float x = expr();
                  cin.get();
                  return x;     
                 }
            else 
                 {
                  cin.putback(c);
                  return number();
                 }
      }
float factor ()
      {
       float x = skobki ();
       for (;;)
      {
                 char c = cin.get();
                 switch (c)
          {
                 case '*':
                      x*= skobki();
                      break;
                 case '/':
                      x/=skobki();
                      break;
                 default:
                         cin.putback (c);
                         return x;
          }
      }
      }

float expr ()
{
      float x = factor ();
      for (;;)
      {
          char c = cin.get();
          switch (c)
          {
                 case '+':
                      x+= factor();
                      break;
                 case '-':
                      x-=factor();
                      break;
                 default:
                         cin.putback (c);
                         return x;
                 }
      }
}


 int main()
     {
     cout << "Enter your equation: ";
     float res = expr();
     cout << " Result = " << res << endl; 
     system ("pause");
     
     
     }
You may have to get the whole number and the decimal separately.
1
2
3
4
 cin >> whole_number;
cin >> decimal;
decimal = decimal/100
actual_number = whole_number + decimal;

However, this wouldn't be very flexible.
I would use std::getline(cin, myString); then parse the information you need from the string. Then use std::stringstream to convert from the string bits into floats/doubles for your calculations.
So, what about operational signs in this case? When i convert the string into double, signs are missing, othervice decimals are out
You need to split your string up before doing the conversion. Split it up into the signs, and the numbers etc.
And where do you advice to store them? Vector?

That depends on how you decide to split the string and pass the pieces off to your other methods. Start simple, ask them to insert and equation and work on isolating the various pieces as strings. Print these to the screen.

Once you accomplish this, work on the conversion and actually doing some calculations. Having them split nicely in the beginning will make you work a lot easier as you progress.
I'm interested in such way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 for (int j=0; j < div.size(); j++)
 {
     if ( div[j] == '*')
     {
          int *ptr = div[j];
          
     int x = div[--j] - 48;
     int y = div[j+2] - 48;
     mult = x*y;
     break ;
     }
     else if ( div[j] == '+')
     {
     int x = div[--j] - 48;
     int y = div[j+2] - 48;
     sum += x+y;     
     break;
     }
 }    


If i continue so, in case of 3+2*5, where shall I store 2*5's result?
Topic archived. No new replies allowed.