# Thread: Basic Calculator I made check it out if you want...

1. ## Basic Calculator I made check it out if you want...

Code:
```#include <iostream>
#include <string>

using namespace std;

int main()
{
const string HEADER ( "\t\t\tBasic Calculator Program\n\n" );
const string SUBHEAD ( "\t\t\tEnter in a math problem\n\n" );
const string line ( 80, '*' );
float FirstNumber;
char Sign;
float SecondNumber;
float Total;

cout.precision(4);

cout << line << endl << endl;

while ( true ) {

cout << ">";
cin >> FirstNumber >> Sign >> SecondNumber;

switch ( Sign )
{

case '+':

Total = ( FirstNumber + SecondNumber );

cout << "\n" << FirstNumber << " + " << SecondNumber << " = " << Total << "\n\n";
break;

case '-':

Total = ( FirstNumber - SecondNumber );

cout << "\n" << FirstNumber << " - " << SecondNumber << " = " << Total << "\n\n";
break;

case '*':

Total = ( FirstNumber * SecondNumber );

cout << "\n" << FirstNumber << " * " << SecondNumber << " = " << Total << "\n\n";
break;

case '/':

Total = ( FirstNumber / SecondNumber );

if ( FirstNumber == 0 || SecondNumber == 0 )
{
cout << "\n\"Error you can't / by 0\" \n\n";
continue;

} else {

cout << "\n" << FirstNumber << " / " << SecondNumber << " = " << Total << "\n\n";
break;
}

default:

cout << "\n\"Error you can't do that \"\n\n";
cout << "Try again.\n\n";
continue;
}

}

system("pause");

return(0);
}```
Something I made really quick Because I'm board...... Comment if you want. Any help would be nice but you don't have to waste your time.

2. In the case of a division, you first divide and then check for zero divide, that doesn't make much sense it should be the other way around. Also, you shouldn't check if the first term is equal to zero because that is a perfectly valid division. Zero divided by any term that is non-zero will always be zero.

There's nothing else I see that could be problematic... Well there is the fact that you use "using namespace std" which is evil especially if you start making your own math functions, this could result in name clashing with the standard library. Also, I would advise against using system(), I think the FAQ has an entry on why system() is evil, you could look it up.

3. A few tweaks
Code:
```#include <iostream>
#include <string>

using namespace std;

int main()
{
const string HEADER ( "\t\t\tBasic Calculator Program\n\n" );
const string SUBHEAD ( "\t\t\tEnter in a math problem\n\n" );
const string line ( 80, '*' );
float FirstNumber;
char  op ;
float SecondNumber;
float Total;

cout.precision(4);

cout << line << endl << endl;

while ( true ) {

cout << ">";
cin >> FirstNumber >> op >> SecondNumber;

switch ( op )
{

case '-':

Total = FirstNumber - SecondNumber ;
break;

case '+':

Total = FirstNumber + SecondNumber ;
break;

case '*':
case 'x':
case 'X':

Total = FirstNumber * SecondNumber ;
break;

case '/':

if ( SecondNumber == 0 )
{
cout << "\n\"Error you can't / by 0\" \n\n";
continue;
}

Total = FirstNumber / SecondNumber ;
break;

default:

cout << "\n\"Error you can't do that \"\n\n";
cout << "Try again.\n\n";
continue;
}

cout << "\n" << FirstNumber << " " << op << " " << SecondNumber << " = " << Total << "\n\n";

}

system("pause");
return(0);
}```

Code:
```#include <iostream>
#include <string>

using namespace std;

int main()
{
const string HEADER ( "\t\t\tBasic Calculator Program\n\n" );
const string SUBHEAD ( "\t\t\tEnter in a math problem\n\n" );
const string line ( 80, '*' );
float FirstNumber;
char  sign ;
float SecondNumber;
float Total;

cout.precision(4);

cout << line << endl << endl;

while ( true ) {

cout << ">";
cin >> FirstNumber >> sign >> SecondNumber;
cin.ignore();

switch ( sign )
{

case '-':

Total = FirstNumber - SecondNumber ;
break;

case '+':

Total = FirstNumber + SecondNumber ;
break;

case '*':
case 'x':
case 'X':

Total = FirstNumber * SecondNumber ;
break;

case '/':

if ( SecondNumber == 0 )
{
cout << "\n\"Error you can't / by 0\" \n\n";
continue;
}

Total = FirstNumber / SecondNumber ;
break;

default:

cout << "\n\"Error you can't do that \"\n\n";
cout << "Try again.\n\n";
continue;
}

cout << "\n" << FirstNumber << " " << sign << " " << SecondNumber << " = " << Total << "\n\n";

}

cin.get();
return(0);
}```

5. I like it. Thanks for checkout my program..... Thanks for the help, Both of you

6. How about using an even better solution than cin.get (IMHO)?
http://apps.sourceforge.net/mediawik...=Pause_console

7. I imagine that using namespace std could be made less evil just by putting it within main(). This way you confine the possible plague of ambiguity to one function, and lose no functionality.

8. Not using it at all is even better. Typing std:: is only five characters and there is no possible confusion neither for the compiler or anyone that reads your code.

9. Originally Posted by Desolation
Not using it at all is even better.
We have had this discussion before, and considering that Sutter and Alexandrescu disagree with you (but then I disagree with them on the finer details, but perhaps they merely were not explicit enough in C++ Coding Standards), it is clear that this is a case of style where infinite are the arguments of mages.