Hi,
First up, I must have been a bit discombobulated this morning when I was talking about the test-expressions in while loops. I said that they were opposite to those in a do while, which is wrong (no one noticed?). I had it right to start with, then edited it to make it wrong - My really Bad.
Ok, if you are to use cout, then use endl instead of \n.
|
cout << "What would you like to use?" << endl;
|
Do the toupper function once, then use the result in a switch statement, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
//before main(), put these declarations for your functions.
void ShowMenu() //I have shown these functions
//not returning anything i.e void,
//which is ok for now
void UseCalculator(); //but generally functions should return some
void PlayGuessingGame(); //sort of status, so you can do error checking
void EnterText();
//in the main function do this instead
ShowMenu() ;
cin >> MenuOption;
MenuOption = toupper(MenuOption);
switch (MenuOption)
case 'A':
//call function to do this option eg UseCalculator();
case 'B':
//call function to do this option eg PlayGuessingGame();
case 'C':
//call function to do this option eg EnterText();
//put the definition of all your functions after main()
|
You seem to have a thing for loops: Even as you code stands the while's should have been if statements. The while loops you have are infinite and would continuously print text.
You should also have other functions ( such as ProcessOperator(symbol) that takes code out of your loop, making it easier to understand.
Also
|
while ( symbol != '+' && symbol != '-' && symbol != '/' && symbol != '*' && symbol != 'c' && symbol != 'C' && symbol != 0 && cin.fail())
|
You still have &&'s when they should be ||'s.
Instead of having a big long test-expression, write a function bool IsOperator(symbol) . Then you can do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
//before main
bool IsOperator(char symbol);
while (IsOperator(symbol) == false){
ProcessOperator(symbol);
}
//after the end of the main() function ie after main's closing brace
//put all the other function definitions here
bool IsOperator(char symbol){
if ( symbol != '+' || symbol != '-' || symbol != '/' || symbol != '*' )
return true;
else return false;
}
|
You could do a similar thing for IsQuit and IsClearInput even though they would only be 1 line of code, it would make things easier to maintain later.
I don't see where ansnum is declared, and all these values read in with cin should be floats or doubles because of the multiplication and division. At the moment it does integer multiplication and division, which is not what you need.
So declare a bunch of variables as doubles to hold the values you read in. C++ allows "on the fly declarations" whereby you can declare a new variable anywhere. Don't declare new things inside loops though. In C, all the declaration go together near the top of the file, or for local variables, at the beginning of the function.
Finally, I have been telling this multiple times:
Don't do this:
Newbies do this because thats what they see in textbooks.
If you are only cin and cout and string ,Do this instead:
1 2 3
|
using std::cin;
using std::cout;
using std::string;
|
It makes it a lot easier on the compiler/ optimiser because it doesn't bring in all the other stuff that is in std:: (and there is lots of it) It won't make any difference to your program either way, it's a matter of style and it's what professional programmers do.
Let us know how you go with all this. Don't worry everyone has to start somewhere, and you have a good attitude I think :)
TheIdeasMan