My basic C++ calculator always multiplies

It's weird.
I just made a basic calculator in c++ , but even if I specify an other option it still multiplies the numbers.
Does anyone know why it does that? 

Thank you

edit: how do I paste code properly in the text box?

 <code>

#include <iostream>

using namespace std;

int aa, ab, ac, ad;

int main()
{

cout << "\nA BASIC CALCULATOR\nBy Mae B \n\n";

while(true)
{

do
{

cout << "\nWhat would you like to do?\n" << "1. add\n" <<
"2. subtract\n" <<
"3. divide\n" <<
"4. multiply\n" <<
"5. exit\n\n" <<
": ";
cin >> aa;

}
while (aa > 5 || aa < 1); // check if the user submitted a correct value, if not the question will be repeated

// check if the user wants to quit the program, if so, break out of the loop
if (aa == 5)
break;

cout << "\nWhat is the first number?\n\n" <<
": ";
cin >> ab;

cout << "\nWhat is the second number?\n\n" <<
": ";
cin >> ac;

// depending on the method the user specified before the result gets calculated
switch (aa)
{

case 1:
ad = ab + ac;
case 2:
ad = ab - ac;
case 3:
ad = ab / ac;
case 4:
ad = ab * ac;

}

cout << "\n\nThe final result of your specified calculation is: " << ad << "\n\n\n\n";

}

}</code>

Looks like your switch statement isn't implemented correctly.

Remember that with switch statements, you need a "break;" at the end of each case, otherwise the code will continue executing the next statement. Since your last case (case 4) is multiplication, that will always be the last thing executed, hence you will always be getting the multiplied results.

Your code should look like this:

switch (aa) {

   case 1:

        ad = ab + ac;

        break;

    case 2:

        ad = ab - ac;

        break;

    case 3:

        ad = ab / ac;

        break;

    case 4:

        ad = ab * ac;

        break;

}

Syntax may not be 100% accurate, (I'm pretty rusty on c++), but that should be the gist of it.

I can't see how a compiler would compile this. There are many errors.

You are using global variables. (because those variables are defined outside of a function or scope.) This is bad practice for many reasons; though, the primary reason is namespace pollution. Basically, programmers/compilers can get confused if a program uses a variable by the same name within another scope.

Your variable names are not good. I had a hard time figuring out what each variable did. I had to go through and re-name all of the variables to understandable and descriptive names.

The way you are using "case" is completely incorrect. It needs to be encapsulated by a "switch statement."

Also, when the user selects "5" the program does not exit. It still asks for the first and second number.

I think that this is alright if you have under a days worth of C++ experience underneath your belt, but you should really look up some tutorials. Those will give you the basic guidelines of what you need to do. Also, ditch your compiler. Use VS2010 or MINGW64 or something.

Anyways, I've fixed your code: http://pastebin.com/N4qW3daF

Here's the website that I used to learn all about C++: http://www.cplusplus.com/doc/tutorial/

Mh weird, I use Codeblocks with the integrated compiler, it should do well right?

There was a switch statement already, but it didn't seem like it because the code got wonky when I pasted it in here. There just weren't any break statements after each case, forgot to implement those.

As far as I know, the program works correctly after I implemented the break statements, but if more of the code is wrong, that's what you say, why does it compile correctly? I'm using the latest version of Codeblocks.. Weird.

Thank you very much! I can't believe I forgot to implement the break statements :P

Try setting break points using Code::Blocks to debug your code next time. Stepping through each line will help the learning curve when you hit a snag.

I like to tell those who are new to programming to work on the problem on their own for a solid 30 minutes with just documentation and the debugger before they can turn to someone or the internet for help. This will help build your troubleshooting skills a little bit when it comes to coding.

Code::Blocks Debugging: http://wiki.codeblocks.org/index.php?title=Debugging_with_Code::Blocks

Sorry, I was a bit tired when I wrote that. (I stayed up all night.)

[Removed, it's irrelevant now.]

However, there are a few more "errors." The compiler may or may not warn you about them. You should always include a return statement at the end of your main loop. This is what tells the OS/IDE that your program has closed successfully or unsuccessfully.

Damn, I just looked over your code and figured it out. For some strange reason, when you posted this the "switch" statement was moved to the line above it. The same happened to your if(aa == 5) break;

Your program is okay. Just add the breaks. Sorry about the confusion. It's good to know that code isn't pasted in here properly. I would use PasteBin next time. I didn't even know about this.

Here's what your code looked like to me:

http://pastebin.com/aaMEw48E

See how messed up it is from your original code? I just ignored the comments and filled in what I thought was missing.