1. ## Do..While Loops

Attached is the instructions.

Here is what I have so far and having trouble:

Code:
```#include <iostream>
using namespace std;

//Write a simple calculator program. Ask the user if s/he wants to add ('+') subtract ('-'), multiply ('*') or divide ('/')
//and then ask for the number. Perform the operation with the given number. Start the "display" at zero. Stop when
//the user enters 'X' (for exit). Use a Do..While loop and "if" statements.
int main()
{
double total = 0, n = 0;
char operation =  ' ';

cout << "Current total is 0" << endl;

do
{
cout << "Enter an operation: + - * / (or enter X to exit): ";
cin >> operation;
if (operation == 'X')
{
break;
}

if ((operation != '+') && (operation != '-') && (operation != '*') && (operation != '/'))
do
{
cout << "Enter an operation: + - * / (or enter X to exit): ";
cin >> operation;
}
while ((operation != '+') && (operation != '-') && (operation != '*') && (operation != '/'));

cout << "Enter a number. " ;
cin >> n;
if (operation == '/')

do
{
cout << "Can not divide by zero!" << endl;
cout << "Current total is " << total << endl;
}
while (n = 0);

else cout << "Current total is ";

if (operation == '+')
total = (total + n);

if (operation == '-')
total = (total - n);

if (operation == '*')
total = (total * n);

if (operation == '/')
total = (total / n);

cout << total << endl;

}
while (operation != 'X');

}```

2. Wow...nice indentation!

What's the problem?

3. I have the program initiating "total" twice when dividing by zero and the 2nd time is nonsense. Also I am getting a "Can not divide by zero" comment for non zero divisors (example 23/3?)

aghhh,

I am missing something.

4. Originally Posted by Titanguy
Code:
```if (operation == '/')

do
{
cout << "Can not divide by zero!" << end
cout << "Current total is " << total << endl;
}
while (n = 0);```
I would change the above to:

Code:
```if (operation == '/' && total == 0)

{
cout << "Can not divide by zero!" << endl;
cout << "Current total is " << total << endl;
}```

5. Code:
`while (n = 0);`
= is for assignment, == is for comparison

6. Start putting braces around your ifs. Then stop adding additional tabs for every if. For each brace, +1 tab ONLY.

warning C6282: Incorrect operator: assignment of constant in Boolean context. Consider using '==' instead

7. Indentation for if's:

Code:
```if (conditional)
{
DoSomething();
if (another_conditional)
{
DoSomethingElse();
}
}```
You don't have to provide braces if the conditional only leads to one statement but I find that always using braces for if's results in much cleaner code.

If you are using MSVC you can usually rely on their default indentation to be correct. There are only two areas that really annoy me in MSVC:

• It does not indent over once before writing the body of a class.
• It indents braces inside switch statements.

IE: MSVC's suggestion
Code:
```class Example
{
public:
Example();
...
};

switch (some_value)
{
case 0:
{
DoSomething();
break;
}
}```
My preference:
Code:
```class Example
{
public:
Example();
...
};

switch (some_value)
{
case 0:
{
DoSomething();
break;
}
}```
Other than those two areas I usually just allow the compiler to figure out the indentation. Of course it has become so automatic for me I'm not sure what the editor in the compiler actually suggests anymore.

8. My preference:
Code:
```class Example
{
public:
Example();
...
};

switch (some_value)
{
case 0:
{
DoSomething();
break;
}
}```