I used to program quite a bit in c++ and only just got back into it. Hence, why I am rusty with things like switch statements :P
Just want to say a huge thankyou to all who responded - I managed to fix the issue I was having :)
@MatthewRock
Function CalculatiOptions is basically the main menu of my calculator program. It takes the user input (their choice on whether they want addition, subtraction etc.) and uses this input for the if statements to determine which function needs to be activated in order to calculate the result.
Yes! That is exactly what I needed, so a HUGE thankyou. I am still very new to c++ so I will read the two articles that you posted. I have already flicked through them and they look very informative. You have been a great help :D
@giblit
Firstly thank you for replying to my thread. I am still VERY new to c++, so I do not know what a double is. Is it where you allow decimals? Because if so, I dont think I would need that for the main menu aha :) Surely I need the int answer so that the function can return what the user inputted so that this value can be used with useroption? Really sorry, but i dont really understand your code... :/
@TheIdeasMan
It is good to see that someone else was confused by giblit too!! I was getting worried there ha.
I tried adding and else clause saying WRONG INPUT!! but it seems to come up after the user has done their sums? Also, next I would like an option for the user to go back to the main menu, but I am not sure how to implement that yet. A nod in the right direction regarding this aspect would be great! Thanks - likewise with you!
@crimsonzero2
Is it bad practice to use the if statements as I am at the moment?
Ok, thankyou very much, I shall look into switch statements.
To All:
Here is what I have ended up with:
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
|
#include <iostream>
using namespace std;
//Menu Function
int CalculatioOptions(int answer){
cout << "What would you like to do: \n";
cout << "1. Addition \n";
cout << "2. Subtraction \n";
cout << "3. Division \n";
cout << "4. Multiplication \n";
cin >> answer;
return answer;
}
//Addition Function
class Addition{
public:
void enterNumber(){
int x;
int y;
cout << "Please enter two numbers: \n";
cout << "First Number: ";
cin >> x;
cout << endl;
cout << "Second Number: ";
cin >> y;
cout << endl;
int answer = x + y;
cout << "The answer is: ";
cout << answer;
}
};
class Subtraction{
public:
void enterNumber(){
int x;
int y;
cout << "Please enter two numbers: \n";
cout << "First Number: ";
cin >> x;
cout << endl;
cout << "Second Number: ";
cin >> y;
cout << endl;
int answer = x - y;
cout << "The answer is: ";
cout << answer;
cout << endl;
}
};
class Division{
public:
void enterNumber(){
int x;
int y;
cout << "Please enter two numbers: \n";
cout << "First Number: ";
cin >> x;
cout << endl;
cout << "Second Number: ";
cin >> y;
cout << endl;
int answer = x / y;
cout << "The answer is: ";
cout << answer;
cout << endl;
int remainder = x % y;
cout << "And the remainder is: ";
cout << remainder;
cout << endl;
}
};
class Multiplication{
public:
void enterNumber(){
int x;
int y;
cout << "Please enter two numbers: \n";
cout << "First Number: ";
cin >> x;
cout << endl;
cout << "Second Number: ";
cin >> y;
cout << endl;
int answer = x / y;
cout << "The answer is: ";
cout << answer;
cout << endl;
}
};
int main(){
//Defining Variables
int useroption;
//Defining Classes
Addition Additionclass;
Subtraction SubtractionClass;
Division DivisionClass;
Multiplication MultiplicationClass;
//BEGINNING OF PROGRAM
useroption = CalculatioOptions(useroption);
if(useroption == 1){
Additionclass.enterNumber();
}
if(useroption == 2 ){
SubtractionClass.enterNumber();
}
if(useroption == 3 ){
DivisionClass.enterNumber();
}
if(useroption == 4 ){
MultiplicationClass.enterNumber();
}
return 0;
}
| |
Please do not refrain from telling me what you think of it - such as bad practice, or what I could have made clearer etc. :)