Thread: Having 2 user inputs for calculator type thing

  1. #1
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877

    Having 2 user inputs for calculator type thing

    Hello all, first post (well second really but the first never materialized on the board).

    I want to have calculations take place inside a switch statement that calls the appropriate function. The menu portion of the program works well, but I can't figure out how to let the user actually input 2 different numbers to the functions. My questions are:

    1. If I use scanf to assign values to the variables, what determines end of input to each variable? (ie.. scanf("%d%d", &a, &b) what is the end of a, what is the end of b?)

    2. Should I assign the variables to user input inside the switch, or try to do it in the functions below?

    3. Is there something I haven't thought to ask that will screw me over? I'm really new to this.

    Code:
    #include<stdio.h>
    int add(int b, int a);
    int mult(int b, int a);
    
    main()
    {
    int preass1, preass2, a, c; /*want preass1, and 2 to both be user input, stands for preassigned*/ 
    
    printf("Press [1] for Addition, or Multiplication");
    
    while ((c=getchar()) !=EOF) {
            
        if (c=='0' || c<='9' && c>='3'){
            printf("didn't enter menu");
        } /*for when you can't get into the main menu of calculator*/
        
        else if (c=='1') {
            printf("Hit 1 for addition \nHit 2 for multiply\n");
        scanf("%d", &a);
            switch (a) {
                case 1:
                    printf("%d", add(preass1,preass2));
                break;
                case 2:
                printf("%d", mult(preass1,preass2)); 
                break;
                default:
                printf("Ruh Roh\n");
           } /*end switch, end of menu for calculator*/
        } /*end else if*/
    } /*end while*/
    return 0;
    }
    int add(int a, int b)
      {
          a=5; /*should I assign user input to variables here, or above?*/
          b=10; 
          return a + b; 
      }
    int mult(int a, int b)
    {
        a=5;
        b=10;
        return a * b;
    }
    p.s. This really was a test of multilayer menu, but I want to add functionality if I can. Thanks for any help!

    edit: Changed a variable before posting and didn't change all the conditions testing it.
    Last edited by Alpo; 04-09-2014 at 11:37 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Alpo View Post
    1. If I use scanf to assign values to the variables, what determines end of input to each variable? (ie.. scanf("%d%d", &a, &b) what is the end of a, what is the end of b?)
    Reading the scanf documentation would help you. In short, anything that is not part of an integer (punctuation, space, etc) stops the processing of one number. However, unless you specify certain characters between the two numbers (e.g. "%d$%d" meaning the user must input a $ between the two numbers), the user simply must enter some sort of white space (space, tab, enter, etc). That is what most users do anyway, use a space, tab or newline/enter to separate inputs. Note, I'm not a huge fan of scanf for user input, since it is not very fault-tolerant, and users screw up input, by mistake and on purpose. I find it's usually best to read a whole line as a string with fgets, and use sscanf (the first 's' for scanning a string) or strtol/strtod functions (even better since they have more features and better error handling than sscanf) to parse out numeric data.
    Quote Originally Posted by Alpo View Post
    2. Should I assign the variables to user input inside the switch, or try to do it in the functions below?
    Take a look at a function tutorial, like the one on this site. Notice how they do it. Note that your add and mult functions as you currently have them will overwrite the values passed in with 5 and 10 every time, always returning 15 and 50 respectively, regardless of user input.
    Quote Originally Posted by Alpo View Post
    3. Is there something I haven't thought to ask that will screw me over? I'm really new to this.
    Most likely. These might not all screw you over, but are good things for you to fix anyway

    • Keep your indentation and formatting consistent so your code is easy to read/follow.
    • If you have to explain what an abbreviated variable name means, it's probably a bad variable name. preass is not a common abbreviation for preassigned. Just spell it out: preassigned1, preassigned2. It's only a few more characters. Never sacrifice code clarity or readability for laziness. Note, preassigned sounds like a bad name since they are not actually preassigned as far as I can tell. What's wrong with simply num1 and num2? Or operand1 and operand2?
    • You should always list the return type explicitly. Yes, it defaults to int, but it's better to clearly state your intention: int main().
    • Your default case should have a break; at the end, just to be safe. You could run into trouble if you add a new case after the default, in which case anytime you run the default case, it would fall through to the next case. Always put a break; and if you intend for code to fall through to the next case, omit the break and put a comment like // fall through so you know you did not leave it out by accident.
    • Check the return value of your input functions (scanf, fgets if you switch to using that, etc). If they failed, and you try to use the data, who knows what will happen.
    • Your error checking for the outer menu options is not very good. You only check if it's a digit of 0 or 3-9. What happens if I enter 'A' or '&' ? You should display some sort of message telling me what I did wrong. What if I enter '2'? What is that supposed to do?


    Possibly more, but that should give you enough to think about for a while.

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Thanks, Anduril, I think you covered everything I wanted/ could have wanted.

    You are right, I should have added an else statement, stupid of me. I've been playing around and leaving the break off the default does seem really useful as it just re-enters at the beginning of loop.
    Last edited by Alpo; 04-09-2014 at 03:03 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Alpo View Post
    I've been playing around and leaving the break off the default does seem really useful as it just re-enters at the beginning of loop.
    The break on the default case only breaks you out of the switch statement. Whether or not it's there has no bearing on the loop, but it could impact the switch statement. If you find that adding a break statement below default case affects the loop, then you placed it outside of the switch statement by mistake. That mistake would be a lot harder to make if your code were indented properly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-29-2011, 01:09 AM
  2. Calculator Program To Allow User 3 Inputs
    By Interista in forum C Programming
    Replies: 14
    Last Post: 10-19-2011, 11:11 AM
  3. How to handle inputs if the user inputs the wrong thing
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 09-22-2010, 04:28 AM
  4. User inputs two characters and...
    By dre in forum C Programming
    Replies: 6
    Last Post: 09-18-2009, 01:51 PM
  5. program is checking for ent when user inputs out?
    By Blizzarddog in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2003, 01:16 PM

Tags for this Thread