TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: FRANK MASINGILL
from: CLIFF RHODES
date: 1997-08-23 10:27:00
subject: Mathplay.Cpp

--> Frank Masingill wrote to All  <--
FM>        Any and all comments truly welcome - no matter how harsh:
Frank, I have a number of comments, but I hope you won't take them as 
being harsh. My first is that your code is really C (with a BASIC 
flavor), not C++. Aside from the use (unnecessary here!) of cin and 
cout, it is C. Plus it doesn't use OOP. It is strictly procedural.
FM>#include 
FM>#include 
FM>#include 
FM>#include 
FM>#include               // for exit statement
FM>#define YES  1
FM>#define NO   0
These defines are not necessary, you don't really use them.
FM>int num1, num2, guess, ans, i, c, proceed=YES;
These globals are questionable. i and c certainly have no business 
being global. You don't even use proceed, so it shouldn't be anywhere.
FM>addCalc();
FM>minusCalc();
FM>multCalc();
FM>divCalc();
FM>int menu();
FM>main()
FM>{
FM>    menu();
change this to
       while(menu())
          ;
then stick your 'goodbye' code here.
FM>    return 0;
FM>}
FM>int menu()
FM>{
FM>   c=30;
FM>   clrscr();
FM>   gotoxy(c,10);
FM>   textcolor(14);
FM>   cputs("Would you like to practice a little ");
Frank, notice how many times your code uses these three statements in 
a row? That should suggest to you that maybe a function could simplify 
things:
void prompt(int x, int y, int c, const char *p)
{
   gotoxy(x, y);
   textcolor(c);
   cputs(p);
}
Then the above 3 lines would be
prompt(c, 10, 14, "Would you like to practice a little?");
FM>   c=35;
FM>   gotoxy(c,11);
FM>   textcolor(9);
FM>   cputs("1. Addition?");
prompt(c, 11, 9, "1. Addition?");
etc.
FM>   gotoxy(c,12);
FM>   textcolor(10);
FM>   cputs("2. Subtraction?");
FM>   gotoxy(c,13);
FM>   textcolor(13);
FM>   cputs("3. Multiplicaton?");
FM>   gotoxy(c,14);
FM>   textcolor(11);
FM>   cputs("4.Division?");
FM>   gotoxy(c,15);
FM>   textcolor(11);
FM>   cputs("5. Quit?");
FM>   do
FM>   {
FM>        gotoxy(c,17);
FM>        textcolor(15);
FM>        cputs("Enter your choice: ");
FM>        scanf("%d", &i);
FM>        switch(i)
FM>  {
FM>             case 1: addCalc();  clrscr(); menu(); break;
This is dangerous. You are calling menu() from inside itself! So, you 
have a recursive function here. What you want to do is have menu() 
return i, and let the while() I suggested in main() handle 
repetitions.
The clrscr() is redundant, because it is one of the first things 
menu() does.
FM>             case 2: minusCalc(); clrscr(); menu();  break;
FM>             case 3: multCalc(); clrscr(); menu(); break;
FM>             case 4: divCalc(); clrscr(); menu();  break;
Same as above for all these cases.
FM>             case 5:
FM>                 clrscr();
FM>                 gotoxy(c,12);
FM>                 textcolor(11);
FM>     cputs("Thanks for playing!");
FM>                 exit(0);
Move this 'goodbye' code to main() after the while(menu()); statement.
You don't need the exit() here if you do that. case 5: just needs to 
break; 
FM>        }                  // end switch statement
FM>   }while (i  5);
Ooops! you mean while(i  5);
FM>   return i;
Make this return (i == 5) ? 0 : 1;
So, if i is 5 the while() in main() will end because 0 is returned. 
Otherwise menu() will be called again.
FM>}
FM> 
FM>addCalc()
FM>{
FM>      clrscr();
FM>      c=20;
FM>      gotoxy(c, 10);
FM>      textcolor(2);
FM>      cputs("ADDITION PROBLEM:");
FM>      gotoxy(c, 12);
FM>      cputs("Type in the first number (Addend)? ");
FM>      cin >> num1;
FM>   gotoxy(c, 13);
FM>      cputs("Type in the second number (Addend)? ");
FM>      cin >> num2;
FM>      ans = num1 + num2;
FM>      gotoxy(c, 14);
FM>      cputs("What do you think is the answer (called the SUM)? ");
FM>      cin >> guess;
FM>      gotoxy(c, 15);
FM>      cprintf("The answer is ");
FM>      textcolor(14);
FM>      cprintf("%d", ans);
FM>      textcolor(2);
FM>      gotoxy(c, 16);
FM>   cprintf("You guessed %s", (ans == guess) ? "right!" : "wrong.");
FM>      cout << endl;
FM>   gotoxy(c, 18);
FM>   cprintf("Would you like to try another (Y/N)? ");
FM>   do {
FM>   guess = (0 == (guess = getch())) ? -getch() : guess;
FM>   guess = toupper(guess);
FM>   }  while(guess != 'Y' && guess != 'N');
FM>   textcolor(9);
FM>   gotoxy(c,22);
FM>   cprintf("Press any key");
FM>   getch();
FM>   return 0;
FM>}
FM> 
FM>minusCalc()
FM>{
FM>   clrscr();
FM>   c=20;
FM>   gotoxy(c, 10);
FM>   textcolor(2);
FM>   cputs("SUBTRACTION PROBLEM:");
FM>   gotoxy(c, 12);
FM>   cputs("Type in the Minuend (number to subract FROM: ");
FM>   cin >> num1;
FM>   gotoxy(c, 13);
FM>   cputs("Now, type in the Subtrahend (number to subract: ");
FM>   cin >> num2;
FM>   ans = num1 - num2;
FM>   gotoxy(c, 14);
FM>   cputs("What do you think is the answer (REMAINDER)? ");
FM>   cin >> guess;
FM>   gotoxy(c, 15);
FM>   cprintf("The answer is ");
FM>   textcolor(14);
FM>   cprintf("%d", ans);
FM>   textcolor(2);
FM>   gotoxy(c, 16);
FM>   cprintf("You guessed %s", (ans == guess) ? "right!" : "wrong.");
FM>   cout << endl;
FM>   gotoxy(c, 18);
FM>   cprintf("Would you like to try another (Y/N)? ");
FM>   do {
FM>   guess = (0 == (guess = getch())) ? -getch() : guess;
FM>   guess = toupper(guess);
FM>   }  while(guess != 'Y' && guess != 'N');
FM>   textcolor(9);
FM>   gotoxy(c,22);
FM>   cprintf("Press any key");
FM>   getch();
FM>   return 0;
FM>}
Look very carefully at your calculation routines. Notice that the are 
VERY similar. In fact, you could use just one calc function with an 
argument saying whether to add, subtract, etc to implement all four in 
one function.
void calc(int type)
{
...
}
Why don't you make some of the changes I have suggested and repost the 
code. If you don't understand what I have said, or don't agree, or 
have a better idea, post what you've got.
I think if we work on improving this incrementally, you might learn a 
lot.
Cliff Rhodes
cliff.rhodes@juge.com
crhodes@flash.net
X CMPQwk 1.42 1692 X"Fortune sides with him who dares." - Virgil
--- Maximus/2 3.01
---------------
* Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.