Thread: If/else instead of switch/case

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    1

    If/else instead of switch/case

    Hello everyone,
    I just started learning C and trying to create a program in C that will have a starting menu for a calculator.
    The problem is that I could do that with a switch/case but the assignment requires me to use if/else.

    Here's what I have so far:

    Code:
    #include <stdio.h>
    
    
    int calc();
    int menu();
    
    
    
    
    int main()
    {
        printf("Hello. Welcome to the program.\n");
        printf("Press RETURN key to continue...\n");
        getchar();
        menu();
    
    
    }
    void menu()
    {
        int choice;
        int var1;
        int var2;
        int var3;
        int var4;
        int A;
        int B;
        int C;
        int D;
    
    
        printf("Menu \n");
        printf("1. Addition.\n");
        printf("2. Multiplication.\n");
        printf("3. Square root. \n");
        printf("4. Exit")
        scanf("%d", &choice);
    How should I go from now using if/else to have those 4 choices?I would really appreciate y'all's help. It's driving me crazy and I have a test on the same day of the deadline of this assignment.

    Thanks!!

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    This should help you work it out
    If Statements in C - Cprogramming.com

    Basically, check to see if your variable is equal to 1, 2, 3, or 4
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Reminds me of a scene in a movie.
    The hot coed student comes up to the professor and says, "Prof, I'll do anything to pass this course."
    Prof: "Anything?"
    Student: "yes, annnyyythinnnng."
    Prof: "okay, then go home and read the book and study."

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Touché! ;^)

  5. #5
    Registered User
    Join Date
    Jun 2014
    Posts
    79
    Quote Originally Posted by sal92 View Post
    How should I go from now using if/else to have those 4 choices?
    I think a simple and effective way can be something like this:

    Code:
    if( choice == 1 )
        printf( "\n\t%d + %d = %d\n\n", n1, n2, n1+n2 );
    else if( choice == 2 )
        printf( "\n\t%d x %d = %d\n\n", n1, n2, n1*n2 );
    else if( choice == 3 )
        printf( "\n\tsquare root of %d = %f\n\n", n1, sqrt(n1) );
    else if( choice == 4 )
        return 0;
    Please, notice that choice has to be "==" (comparison) to the chosen number, not just "=" (assignment). Oh, and don't forget to include <math.h>, as sqrt() requires that library.

    Can I say that I would add one more check, just to see if the user didn't type a valid choice?

  6. #6
    Registered User
    Join Date
    Jun 2014
    Posts
    79

    Try this, if you like

    A few months ago, I put together a class for my personal use that creates menu objects. Maybe someone can find it useful as well? It's attached to this message.
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch case
    By Tiemer in forum C Programming
    Replies: 4
    Last Post: 04-30-2011, 02:04 PM
  2. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  3. Some help with switch case bug...
    By hykyit in forum C Programming
    Replies: 5
    Last Post: 05-18-2005, 03:04 AM
  4. switch, case help
    By campermama in forum C++ Programming
    Replies: 7
    Last Post: 06-16-2004, 09:49 AM
  5. switch case
    By threahdead in forum C Programming
    Replies: 5
    Last Post: 09-30-2002, 02:37 PM

Tags for this Thread