Thread: Really Basic code, help.

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    4

    Really Basic code, help.

    Hey i'm just practicing my knowledge of C so i've tried to make this crude calculator where you enter two numbers, type whether you want to multiply, add, subtract or divide and it returns the answer. My code is here:

    Code:
    /* SIMPLE CALCULATOR */
    
    
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    
    
    int  main(int argc, char* argv[])
    {
        int x, y, validInput, answer;
        char multiplication, division, addition, subtraction;
        
        validInput = (argc == 7);
        validInput = validInput && sscanf(argv[1], "%d", &x);  /*This is where values for x and y are inserted*/
        validInput = validInput && sscanf(argv[2], "%d", &y);
        
        if(!validInput)
        {
            printf("Insert any positive values for x and y and run again.\n"); /*exit if no numbers inserted*/
            return(EXIT_FAILURE);
        }
        
        
        validInput = validInput && sscanf(argv[3], "%c", &multiplication);
        validInput = validInput && sscanf(argv[4], "%c", &division);
        validInput = validInput && sscanf(argv[5], "%c", &addition);
        validInput = validInput && sscanf(argv[6], "%c", &subtraction);
        
        
        if(x && y <= 0)
        {
            printf("Insert positive numbers");
            return(EXIT_FAILURE);
        }
        
        printf("Do you want to use division, multiplication, addition or subtraction?\n");
        
        if (scanf("%c",&multiplication))
        {
            answer = x * y;
            return(answer);
        }
        
        if (scanf("%c",&division))
        {
            answer = x / y;
            return(answer);
        }
        
        if (scanf("%c",&addition))
        {
            answer = x + y;
            return(answer);
        }
        
        if (scanf("%c",&subtraction))
        {
            answer = x - y;
            return(answer);
        }
    
    
        return 0;
    However when I compile, it just comes up with the "Insert two positive values for x and y" message even after putting two numbers in. I get the feeling I need to assign memory for this, but im unsure as how to do this.

    I'm also unsure whether validInput = (argc == 7); is correct either. Is 7 correct? I don't know what number to put here, in a previous example there were two values for the valid inputer and it had argc == 3.

    Any help would be great thanks.

    *EDIT* Also as well, should I be using if, else, if, else... etc? I've just got a line of if's... Got a feeling that isnt correct either.
    Last edited by Ryan Gregg; 11-07-2014 at 01:13 PM. Reason: added more info

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Ryan Gregg View Post
    Hey i'm just practicing my knowledge of C so i've tried to make this crude calculator where you enter two numbers, type whether you want to multiply, add, subtract or divide and it returns the answer. My code is here:

    However when I compile, it just comes up with the "Insert two positive values for x and y" message even after putting two numbers in. I get the feeling I need to assign memory for this, but im unsure as how to do this.

    I'm also unsure whether validInput = (argc == 7); is correct either. Is 7 correct? I don't know what number to put here, in a previous example there were two values for the valid inputer and it had argc == 3.

    Any help would be great thanks.

    *EDIT* Also as well, should I be using if, else, if, else... etc? I've just got a line of if's... Got a feeling that isnt correct either.
    Most of this can't be answered because we don't know how your program should be called. Some possibilities
    Code:
    $ calculator 3 + 4
    $ calculator 3 4 -
    $ calculator 3 4 addition=no subtraction=no multiplication=yes division=no
    ...
    The last one looks weird, but makes of bit of sense if you think argc should be 7.

    So first, study this: FAQ > Accessing command line parameters/arguments - Cprogramming.com. Get a basic feel for working with command line arguments. Note, argc is the number of things passed on the command line, so the correct value depends on how you want your program to be run.

    Thus, after you understand how to use them in general, you will need to figure out how you want your program to be run.

    As for the list of if statements, it depends. You use a sequence of if's when you have several conditions that may all be true: e.g. you want the user to be able to specify more than one operation, to see the sum, difference, product and quotient of 3 and 4 all in one run. You use an if-else if sequence if the conditions should be mutually exclusive: you just want one mathematical operation per run. You'll have to determine how you want your program to behave. Often times, I prefer switch/case statements instead of if-else.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Oh, and this line
    Code:
    if(x && y <= 0)
    Doesn't do what you think. Consult a C operator precedence chart(C Operator Precedence Table). You'll notice that <= has higher precedence than &&. Thus, the above line is equivalent to
    Code:
    if(x && (y <= 0))
    In plain English: If x is non-zero and y is less than or equal to zero.... You need to check these independently: if (x <= 0 && y <= 0)

    Also, note that scanf (and fscanf and sscanf) return the number of this successfully scanned, or EOF on end of input or error. EOF is guaranteed to be negative (non-zero). If you ask for 1, a return value of 1 is successful, however if there were problems, you could get a 0 or EOF. Thus, your validInput && scanf... statements are not correct, they will report validInput as true if scanf returns EOF. Instead, try
    Code:
    validInput = validInput && (scanf(...) != 1);  // replace 1 with the correct number of things you are trying to scan with your format specifier
    Also, you should consider checkint validInput after each step, so you can report specifically what the user did wrong: numbers must be positive, $ is not a valid operator, only one operator allowed, only two numbers allowed, etc.

    Note, input validation is a giant pain in the butt to get correct. For basic programs like this that need some tolerance, but do not need to be as robust as commercial products, I recommend just reading a line with fgets (remember to remove the newline if there: FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com) and then use sscanf or strtol/strtod functions to parse out numbers.

    Maybe other errors, but this should keep you plenty busy.

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    4
    Thanks a lot for replying, it helped a lot. I'm not sure what this part means though:


    Code:
    $ calculator 3 + 4
    $ calculator 3 4 -
    $ calculator 3 4 addition=no subtraction=no multiplication=yes division=no
    ...
    


    Where about would I put that in the code?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Ryan Gregg View Post
    Thanks a lot for replying, it helped a lot. I'm not sure what this part means though:


    Code:
    $ calculator 3 + 4
    $ calculator 3 4 -
    $ calculator 3 4 addition=no subtraction=no multiplication=yes division=no
    ...
    


    Where about would I put that in the code?
    The use of a "$" means to use it on a command line; Unix/Linux tends to use "$" as the default command prompt.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to write basic code
    By tingting in forum C Programming
    Replies: 13
    Last Post: 07-22-2009, 04:16 PM
  2. Visual Basic Code Help
    By Slinger137 in forum Windows Programming
    Replies: 9
    Last Post: 01-04-2009, 10:26 AM
  3. Basic C-code problem
    By matanzalle in forum C Programming
    Replies: 2
    Last Post: 04-16-2008, 07:41 AM
  4. basic code help
    By matt37664 in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2006, 01:40 PM
  5. Basic Code
    By MadCow257 in forum Game Programming
    Replies: 6
    Last Post: 03-01-2005, 05:28 PM

Tags for this Thread