/* My first program in C++ (calculator)
Working on a " Ans " ( if invalid operation bloack "Ans" mode !
*/
#include <iostream>
#include <limits>
usingnamespace 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;
}
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.
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.
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!