switch cases and do while

This is a discussion on switch cases and do while within the C Programming forums, part of the General Programming Boards category; Hi! I'm new to C program and my prof gave me this problem Can anybody help me out. Ok, she ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    3

    Arrow 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
    --------------------------------
    Main Menu
    [1] Addition
    [2] Subtraction
    [3] Multiplication
    [4] Divison

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

    Addition
    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()

    {
    char oper,addition,subtraction,multiplication,division;
    int num1,num2,total,ch;
    clrscr();

    g(33,3);p("MAIN MENU");
    g(25,5);p("[1] Addition");
    g(25,6);p("[2] Subtraction");
    g(25,7);p("[3] Multiplication");
    g(25,8);p("[4] Division");
    g(25,10);p("Enter Your Choice");

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

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

    switch(ch)
    {
    case '1': oper=addition;
    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!

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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>
    Away.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    26

    unique code

    I have to say your code is unique...unlike much I have seen before. Guess everyone has there own style.
    "Borland Rocks!"

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>s("%d",num1);
    Should be:
    >>scanf("%d", &num1);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    3
    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

    Addition

    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!

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read this
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    3

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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 (&)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21