Problems with simple program

I am currently learning C++ on my own and the best way I learn is hands-on. This lead me to attempt to create a simple calculator capable of addition, subtraction, multiplication and division. I cannot find the error I made that isn't allowing me to build this. The error occurs towards the end of the program. If you could explain what I did wrong, it would be greatly appreciated.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````#include using namespace std; int main() { float a, pass; cout << "What is the four digit passcode to access the calculator? \n"; cin >> pass; while (pass not_eq 1001){ cout << "Invalid entry. Re-enter Passcode. \n"; cin >> pass;} if(pass==1001){ cout << "What would you like to do? Type 1 for addition, 2 for subtraction, 3 for \nmultiplication, or 4 for division. \n"; cin >> a; if(a==1){cout << "Enter a number to add. \n"; float b, c, sum; cin >> b; cout << "Enter a second number. \n"; cin >> c; sum = b + c; cout << "The sum is " << sum << endl;} if(a==2){cout << "Enter a number to subtract from. \n"; float d, e, diff; cin >> d; cout << "Enter the second number. \n"; cin >> e; diff = d - e; cout << "The difference is " << diff << endl;} if(a==3){cout << "Enter a number to multiply. \n"; float f, g, prodct; cin >> f; cout << "Enter a second number. \n"; cin >> g; prodct = f * g; cout << "The product is " << prodct << endl;} if(a==4){cout << "Enter a number to divide from. \n"; float h, i, quotent; cin >> h; cout << "Enter the second number. \n"; cin >> i; quotent = h / i; cout << "The quotient is " << quotent << "." << endl;}} return 0;``````

I don't know if it's just a copy and paste error on the forum, but you're missing a closing } after the return 0; line.

I don't think you really need the if (pass==1001) condition, since you've already made sure they entered the correct passcode in the while loop before it.

You don't check to make sure they enter a valid selection from the 1,2,3 or 4 menu. I'd also put in a check in the division section to make sure you don't try to divide by zero, since that will cause a crash.
I think you should try moving the last closing } after "return 0;"
You left off a curly bracket to close int main(). Other than that I was able to compile and run it.

You also don't check to see if the user inputs a valid int (1-4) into the variable a to determine the operation.

And one last thing, dividing 2 floats will not result in what you expect most of the time. Try dividing things like 1/3 and you'll see what I mean.
wildblue, you were correct that i did not need that if conditioning statement. That was able to cut out a line of code as well as one of the curly braces which could make the program easier to read. I have been attempting to use do while statements to be able to stop from dividing by 0. Before it wouldn't compile which might have been due to the missing brace at the end of the operation.
RadWayne, i divided 1/3 and it came up as .333333 which is the correct answer (a repeating decimal). I have not yet experienced the error you speak of from dividing floats. I have also tried to use a do while to confirm validation of using a correct number for an operation, but like I said previously, it wouldn't work (most likely due to the missing brace after return 0).

I would like to thank you all for your help. If I have any more questions they will probably be under the same topic or a similarly named topic.
Topic archived. No new replies allowed.