0

I am just trying to run a program that add two numbers and outputs as a float.

For example: $Me ./mc + 1 2.3

should produce $Me 3.3 or 3.3000

Here is what I have:

#include <stdio.h>

int main( int ac, char* args[] )
{
    float sum=0;
    if ((strcmp(args[1],"+")) == 0)
    {
        sum=atof(args[2])+atof(args[3]);
        printf("%f\n", sum);
    }
    else
        printf("exit");
    return 0;
}
shayster01
  • 214
  • 3
  • 17
  • 3
    The conventional names for the two parameters of `main ` are `argc` and `argv`. Using those names makes your code easier to read. – Keith Thompson Jan 12 '15 at 02:15
  • You didn't say what the problem is. – Keith Thompson Jan 12 '15 at 02:24
  • @KeithThompson: The `argc` and `argv` thing was mentioned in a comment to the OP's [earlier question](http://stackoverflow.com/questions/27884998/help-with-basic-programming#comment44171344_27884998) relating to this program... – PM 2Ring Jan 12 '15 at 06:31

2 Answers2

6

You should include stdlib.h. Otherwise the implicit definition of atof will return int.

~$ man atof
      1 ATOF(3)                                                         Linux Programmer's Manual                                                         ATO      1 F(3)
      2
      3
      4
      5 NAME
      6        atof - convert a string to a double
      7
      8 SYNOPSIS
      9        #include <stdlib.h>
     10
     11        double atof(const char *nptr);

To avoid such problems, as @Keith Thompson mentioned:

You should compile with warnings enabled so the compiler can tell you about problems like this. For gcc, use -Wall -Wextra, and possibly -std=... -pedantic.

Logan Ding
  • 1,761
  • 1
  • 12
  • 23
0

You should use strtod instead

#include <stdio.h>
#include <stdlib.h>

void usage(const char *const program)
{
    fprintf(stderr, "usage: %s x y [where x and y are numbers.]\n", program);
    return -1;
}

void conversionFailed(const char *const program)
{
    fprintf(stderr, "arguments should be numbers\n");
    return usage(program);
}

int main( int ac, char* args[] )
{
    float sum;
    char *endptr;

    if (ac < 4)
        return usage(argv[0]);
    if (strcmp(argv[1], "+") == 0)
    {
        sum = strtod(args[2], &endptr);
        if (*endptr != '\0')
            return conversionFailed(argv[0]);
        sum += strtod(args[3], &endptr);
        if (*endptr != '\0')
            return conversionFailed(argv[0]);
        printf("%f\n", sum);
    }
    return 0;
}

strtod() is preferred because it allows input validity checking.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • He's not checking for sign. He is checking for an operator ... in this case "+" presumably because he wants to handle other operators in the future (eg, "*", "-", "/" etc). – Apprentice Queue Jan 12 '15 at 02:20
  • I didn't notice that because I thought the program invokation was `./mc 1 2.3` without the `+` sign, sorry... That's why I fixed the answer, better than deleting it, and also because it shows the usage of the preferred at least for me, `strtod()`. But **THE OP SHOULD PICK `Xu Ding`'s ANSWER** because he is the one solving the issue. – Iharob Al Asimi Jan 12 '15 at 02:21
  • Using `strtod` gives better error checking, but that's not the problem. The problem is the missing `#include `. See the other answer. – Keith Thompson Jan 12 '15 at 02:25
  • @KeithThompson Yes a already commented that... see my previous comment. – Iharob Al Asimi Jan 12 '15 at 02:26