How can I input decimals into my calculator?

I'm trying to write a calculator where the numbers are entered from right to left. In order to accomplish this task, I've put together the following code by searching through this forum and grabbing some ideas from other users. Now it works fine but only with integers. So if I want to enter 2.5 and multiply it by 2, it would simply not work. Can somebody give me an idea of how I can modify my program for this to work? I would greatly appreciate it!

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <cstdlib>
#include <stdio.h>
#include <conio.h>
#include <string>
#include <sstream>

using namespace std;

struct console_t
  {
  DWORD  mode;
  HANDLE hstdin;
  HANDLE hstdout;

  console_t()
    {
    hstdin  = GetStdHandle( STD_INPUT_HANDLE  );
    hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
    GetConsoleMode( hstdin, &mode );
    }

  ~console_t()
    {
    if (hstdin != INVALID_HANDLE_VALUE)
      SetConsoleMode( hstdin, mode );
    }

  bool iskeypressed( unsigned timeout_ms = 0 ) const
    {
    return WaitForSingleObject( hstdin, timeout_ms ) == WAIT_OBJECT_0;
    }

  int getkeypress() const
    {
    INPUT_RECORD inrec;
    DWORD        count;

    if (hstdin == INVALID_HANDLE_VALUE) return -1;
    SetConsoleMode( hstdin, 0 );

    do ReadConsoleInput( hstdin, &inrec, 1, &count );
    while ((inrec.EventType != KEY_EVENT) || inrec.Event.KeyEvent.bKeyDown);

    SetConsoleMode( hstdin, mode );

    return inrec.Event.KeyEvent.wVirtualKeyCode;
    }

  void gotoxy( int column, int line )
    {
    COORD coord;
    coord.X = column;
    coord.Y = line;
    SetConsoleCursorPosition( hstdout, coord );
    }

  void clearscreen()
    {
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD                      count;
    DWORD                      cellCount;
    COORD                      homeCoords = { 0, 0 };

    if (hstdout == INVALID_HANDLE_VALUE) return;

    if (!GetConsoleScreenBufferInfo( hstdout, &csbi )) return;
    cellCount = csbi.dwSize.X * csbi.dwSize.Y;

    if (!FillConsoleOutputCharacter(
      hstdout,
      (TCHAR) ' ',
      cellCount,
      homeCoords,
      &count
      )) return;

    if (!FillConsoleOutputAttribute(
      hstdout,
      csbi.wAttributes,
      cellCount,
      homeCoords,
      &count
      )) return;

    SetConsoleCursorPosition( hstdout, homeCoords );
    }
  };

int a,c,n,m;
char b,d;
inline int addizione(int a, int c){      //Function +
      return m=a+c;
      }
     inline int sottrazione(int a, int c){  //Function -
            return m=a-c;
            }
           inline int divisione(int a, int c){    //Function /
                  return m=a/c;
                  }
                 inline int moltiplicazione(int a, int c){    //Function *
                        return m=a*c;
                        }

int main()
{
   PROBANDO:
   console_t console;
   string pass ="";
   char ch;
   ch = _getch();
   while(ch != 13){
      pass.push_back(ch);
      int zerokey = 0;
      zerokey = console.getkeypress();
      if(zerokey == '0' || zerokey == '1' || zerokey == '2' || zerokey == '3' || zerokey == '4' || zerokey == '5' || zerokey == '6' || zerokey == '7' || zerokey == '8' || zerokey == '9')
      {
            switch(zerokey){
            case '0': zerokey = 0; break;
            case '1': zerokey = 1; break;
            case '2': zerokey = 2; break;
            case '3': zerokey = 3; break;
            case '4': zerokey = 4; break;
            case '5': zerokey = 5; break;
            case '6': zerokey = 6; break;
            case '7': zerokey = 7; break;
            case '8': zerokey = 8; break;
            case '9': zerokey = 9; break;
            return zerokey;
        }

        console.clearscreen();
        stringstream ss(pass);
        int thisVar;
        if( (ss >> thisVar).fail() ) {       //here you do the conversion to integer whole number...
        //error
        }
        a = thisVar;    //here you store the value into the variable a...
        cout << setw(11) << a;

        ch = _getch();
      }
      else if(zerokey == 189) { //This will subtract the numbers...
          zerokey = '-';
          b = zerokey;
          goto LOOP2;
      }

      else if(zerokey == 187) {
          zerokey = '+';
          b = zerokey;
          goto LOOP2;
      }

      else if(zerokey == 191) {
          zerokey = '/';
          b = zerokey;
          goto LOOP2;
      }

      else if(zerokey == 219) {
          zerokey = '*';
          b = zerokey;
          goto LOOP2;
      }

      else if(zerokey == 8) {
          console.clearscreen();
          goto PROBANDO;
      }

   } //closes the while statement...

          LOOP:
            int thiskey;
            thiskey = console.getkeypress();
            if(thiskey == 189) { //This will subtract the numbers...
                thiskey = 0;
                thiskey = '-';
                b = thiskey;
                goto LOOP2;
            }

            else if(thiskey == 187) {
                thiskey = 0;
                thiskey = '+';
                b = thiskey;
                goto LOOP2;
            }

            else if(thiskey == 191) {
                thiskey = 0;
                thiskey = '/';
                b = thiskey;
                goto LOOP2;
            }

            else if(thiskey == 219) {
                thiskey = 0;
                thiskey = '*';
                b = thiskey;
                goto LOOP2;
            }

            else if(thiskey == 8) {
                console.clearscreen();
                goto PROBANDO;
            }

          LOOP2:

   string pass2 ="";
   char ch2;
   ch2 = _getch();
   while(ch2 != 13){//character 13 is enter
      pass2.push_back(ch2);
      int onekey = 0;
      onekey = console.getkeypress();
      if(onekey == '0' || onekey == '1' || onekey == '2' || onekey == '3' || onekey == '4' || onekey == '5' || onekey == '6' || onekey == '7' || onekey == '8' || onekey == '9')
      {
            switch(onekey){
            case '0': onekey = 0; break;
            case '1': onekey = 1; break;
            case '2': onekey = 2; break;
            case '3': onekey = 3; break;
            case '4': onekey = 4; break;
            case '5': onekey = 5; break;
            case '6': onekey = 6; break;
            case '7': onekey = 7; break;
            case '8': onekey = 8; break;
            case '9': onekey = 9; break;
            return onekey;
        }

        console.clearscreen();
        stringstream ss(pass2);
        int thisVar2;
        if( (ss >> thisVar2).fail() ) {       //here you do the conversion to integer whole number...
        //error
        }
        c = thisVar2;    //here you store the value into the variable a...
        cout << setw(11) << c;

        ch2 = _getch();
      }

      else if(onekey == 13) {
        goto FINAL;
      }

      else if(onekey == 189) { //In case we want to subtract again then we repeat the process...
          onekey = '-';
          b = onekey;
          goto LOOP2;
      }

      else if(onekey == 187) {
          onekey = '+';
          b = onekey;
          goto LOOP2;
      }

      else if(onekey == 191) {
          onekey = '/';
          b = onekey;
          goto LOOP2;
      }

      else if(onekey == 219) {
          onekey = '*';
          b = onekey;
          goto LOOP2;
      }

      else if(onekey == 8) {
          console.clearscreen();
          goto PROBANDO;
      }
   } //closes the while statement...

          FINAL:
          console.clearscreen();
          if(b=='+'){
                    n = addizione(a,c);
                    cout << setw(11) << n;
                    a=n;
                    }else if(b=='-'){
                          n=sottrazione(a,c);
                          cout << setw(11) << n;
                          a=n;
                          }else if(b=='/'){
                                n=divisione(a,c);
                                cout << setw(11) << n;
                                a=n;
                                }else if(b=='*'){
                                      n=moltiplicazione(a,c);
                                      cout << setw(11) << n;
                                      a=n;
                                      }
                                      goto LOOP;   /*goes to loop and allows to
                                                   automatically continue operating*/
          system("pause");
          return 0;
          }
The four functions (+,-,*,/) are all written for int types and this is the type you convert your char digits to also. As you are aware int types cannot store fractions. It seems that instead of converting char types to int you will have to convert to double or float type with a function like atof() or write your own.
Hope this helps.
Topic archived. No new replies allowed.