" Ans " for cmd calculator

Sup peps !

I made my first calculator two day's ago " in fact its my first program " yet i still ave 1 problem and i do not know why its not working!

I wish to ave the option to use last awnser gave by the calculator for the next operation
For example:

_____(Ans)
1 + 1 = 2 + 1 = 3

I try'd to make it by using if, else, return but i failed
Let's ave a look to the source code if you want!



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
/*                          My first program in C++ (calculator)
                    Working on a  " Ans " ( if invalid operation bloack "Ans" mode ! 
*/

#include <iostream>
#include <limits>
using namespace std;

int main ()
{
     char response;
     int select;
     double a, b, y;
     do
     {
         system ("cls"); // its here just because i can ...
         cout << "Select the first number you want to calculate\n" << endl; cout << "///////////////////////////////////////////////\n";
         cout << "Choisie le premier nombre que tu veut calculer\n " << endl; cout << "______________________________________________\n\n";
         if ( y == response )
         {
              cin >> response;
         }
         else 
         {
              cin >> a;
         }
         cout << "______________________________________________\n\n";
         cout << "What you want to do ...\n "  << endl; cout << "///////////////////////////////////////////////\n";
         cout << "Que veut tu faire ...\n " << endl; cout << "______________________________________________\n\n";
         cout << "1. Add // Additionner\n" << endl;
         cout << "2. Substract // Soustraire\n" << endl;
         cout << "3. Multiply // Multiplier\n" << endl;
         cout << "4. Divide // Diviser\n" << endl;
         cout << "5. Pourcentage\n" << endl;
         cout << "______________________________________________\n\n";
         if ( y == response )
         {
              cin >> select;
         }
         else
         {
             cin >> select;
         }
         cout << "______________________________________________\n\n";
         cout << "Select the second number you want to calculate\n"  << endl; cout << "///////////////////////////////////////////////\n";
         cout << "Choisie le deusieme nombre que tu veut calculer\n " << endl; cout << "______________________________________________\n\n";
         cin >> b;
         cout << "______________________________________________\n\n";
         cout << "And the awnser is ...\n"; cout << "///////////////////////////////////////////////\n";
         cout << "Et la reponce est ...\n"; cout << "______________________________________________\n\n";
         switch(select)
         {
                       case 1: cout << a + b << endl; break;
                       case 2: cout << a - b << endl; break;
                       case 3: cout << a * b << endl; break;
                       case 4: cout << a / b << endl; break;
                       case 5: cout << a *100 /b << endl; break;
                       case 6: if ( y == response ) return select; break;
                       default: cout <<"Invalid operation!\n";
         }
         cout << "______________________________________________\n\n";
         cout << "Press y to continue or n to exit.\n\n"; cout << "///////////////////////////////////////////////\n\n";
         cout << "Peser sur y pour continuer ou sur n pour quiter\n";
         cin >> response;
         }
    while ( response == 'y');
    std::cout << "Press ENTER to continue...";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

  return 0;

}


i know im close somehow but ....
Last edited on
1. integer/integer = integer. integers only represent whole numbers, not fractions, su 1/3=0. if you want to have 0.333, use floats or doubles.

2. your first problem is that you don't store your results anywhere. you only print them. You could store them in a and from line 47 jump to line 18 (and remove the outer loop) but that functionality isn't very comfortable without a gui or more complex parsing.
if you want to have 0.333, use floats or doubles.


i never used those yet ... as i told before its my first prog ... i still ave a lot to learn, i already started about floats a bit but its not fully clear atm + i started this program to take a break of my books lol i just started to write <sstring> basics.

but that functionality isn't very comfortable without a gui or more complex parsing.


so what else i could do ???

The fix for problem 1 is actually quite simple. Just change the type of a and b by switching out int for double. It's that simple!

For using the answer again, you can add an extra option at the end: instead of pressing 'y', you could press 'a' or 'b' to be able to use that answer for argument a or b (you'll need two if statements around lines 29 to 31 and 15 to 17, as well as a switch statement at line 48). It's not that hard!

Votre anglais n'est pas mal.

-Albatross
The fix for problem 1 is actually quite simple. Just change the type of a and b by switching out int for double. It's that simple!

gosh i feel so retard on this one ... how could i miss that ,...lol

merci albatross ton francais est pas ml non plus ;)
Lol. I remember making the same mistake not too long ago. :P
Topic archived. No new replies allowed.