# switch cases and do while

• 07-27-2003
exoillusion
switch cases and do while
Hi! I'm new to C program and my prof gave me this problem :( Can anybody help me out.
Ok, she wants an output like the one below
--------------------------------
[2] Subtraction
[3] Multiplication
[4] Divison

Enter your choice: __
-2nd page/another screen

Enter Number 1 :
Enter Number 2 :
The Sum is _

Try Again? [y][n]
--------------------------------

i should use do while and switch case
and here's what i made

--------------------------------
#include<stdio.h>
#define p printf
#define g gotoxy
#define s scanf

main()

{
int num1,num2,total,ch;
clrscr();

g(25,6);p("[2] Subtraction");
g(25,7);p("[3] Multiplication");
g(25,8);p("[4] Division");

do
{
g(25,11);ch=getche();
getche();
}

while(ch!='1' && ch!='2' && ch!='3' && ch!='4');

switch(ch)
{
break;

case '2': ch=num1-num2;
oper=subtraction;
break;

case '3': ch=num1*num2;
oper=multiplication;
break;

case '4': ch=num1/num2;
oper=division;
break;
}

clrscr();

g(30,3);p("%s",oper);

g(15,5);p("Number 1 : ");
s("%d",num1);

g(15,6);p("Number 2 : ");
s("%d",num2);

g(15,7);p("Total is %d",ch);

getch();
return 0;
}
-----------------------------------

But it doesn't work. What could be wrong in my program?To anyone who replies, thanks in advance!
• 07-27-2003
confuted
SALEM MISSED IT!! :)

>>main()

Change this to int main() and add a return 0; at the end of the function.

Your formatting is horrible. Code is a lot easier to read when it is formatted correctly - each statement gets its own line, etc. I like to put braces on their own line as well.

When you post a problem, tell us specifically what doesn't work.

And, finally, last but not least,
Code:

`#include <conio.h>`
• 07-27-2003
nold
unique code
I have to say your code is unique...unlike much I have seen before. Guess everyone has there own style.:)
• 07-28-2003
Hammer
>>s("%d",num1);
Should be:
>>scanf("%d", &num1);
• 07-28-2003
exoillusion
The - #define - works. I don't have a problem with that.
Here's what don't work

After entering the no. of the chosen operation eg Enter Your Choice : 1

It then goes to the next page with the chosen Operation displayed already

Since user chose 1 The screen should be like this

Enter Number 1:
Enter Number 2:

And then it gets the sum of the two numbers (since user chose addition)

It'd be great if anybody can fix the code for me. I know im very lame with C...Thanks again!
• 07-28-2003
Hammer
• 07-28-2003
exoillusion
• 07-28-2003
Hammer
Quote:

Originally posted by exoillusion
here's a program

http://www.ei-corp.net/files/PRELIM01.C

And? How does this relate to your maths program?

>>The - #define - works. I don't have a problem with that.
But you missed the amphersand, that was my point (&)