I just recently (yesterday infact) started to learn c++ and am finding it pretty entertaining; However I have had no programming experience before this so my progress has likely been quite slow by most standards.
After working at some tutorials I started writing a sort of calculator program. The problem is there are a couple of things I'm really stumped with that repeated googling can't seem to solve. Could anyone help?
Here's the code first of all:
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
|
//stuff
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
/* This is the initial selection stuff
that lets people choose the kind of
mathmatics they want to do... hopefully*/
int selection;
double x;
double y;
float add, subtract, mult, div;
{
cout<<"Please make a selection.\n";
cout<< "1. To add\n";
cout<< "2. To subtract\n";
cout<< "3. To multiply\n";
cout<< "4. To divide\n";
cout<< "0. to exit\n";
cin>> selection;
cin.get();
}
/////////////////////// The add section
if (selection == 1);
{
cout<< "Please enter the two numbers you want to add followed by the enter key." << endl;
cin >> x >> y;
add = x + y;
cout<< "Your answer is " << add << ".\n";
return 0;
}
////////////////////// The subtract bit.
if (selection == 2)
{
cout<< "Please enter a number followed by the number you wish to subtract from it." << endl;
cin >> x >> y;
subtract = x - y;
cout<< "Your answer is " << subtract << ".\n";
}
////////////////////// Multiplication
if (selection == 3)
{
cout<< "Please enter the two numbers you wish to multiply." << endl;
cin >> x >> y;
mult = x * y;
cout<< "Your answer is " << mult << ".\n";
}
///////////////////// Divison
if (selection == 4)
{
cout<< "Please enter the two numbers divide" << endl;
cin >> x >> y;
div = x / y;
cout<< "Your answer is " << div << ".\n";
}
//////////////////// The exit selection
if (selection == 0)
{
cout<< "\nThankyou! - Matt.\n";
return 0;
}
}
|
It's almost fully functional. I somehow broke the exit option (I'm sure it worked at one point...), I'm still trying to work that one out, but it isn't my main problem.
What I REALLY want to know is how to make each arithmetic section loop back to the main menu to allow another selection! I've spent quite a while trying the various loop statements but it's seems I don't have a clue. Something I suspected all along...
I'm pretty sure my codes fairly ridiculous aswell but I wrote it only way I know how at the moment. Hope no one gets a headache!
Any help at all on my loop problem, my exit code or just the way I've written the thing would be greatly appreciated!
Thanks.