-3

For some reason anything I try results in numbers after the decimal place being disregarded.

sscanf(argv[5], "%.2lf", &add4); doesn't work

add4 = atof(argv[5]); doesn't work

Any help?
1: program name (aka for the program im trying to solve currently, i need the following parameters. of these, double isnt getting saved properly)
2: string
3: int
4: int
5: double

all but the double are being copied into my arrays fine. I've tried the above two ways of copying the double but both result in numbers after the decimal being .00

Declarations:
char add1[50]; int add2, add3; double add4;

This call adds a new line to a list .csv file:

inv add banana 2 2 2.50

where below, command has already been assigned the string "add".

what would end up listed would be:

"banana, 2, 2, 2.00" at the end of my text file

if(strcmp(command,"add") == 0)
                {
                    strcpy(add1, argv[2]);
                    add2 = atoi(argv[3]);
                    add3 = atoi(argv[4]);
                    add4 = atof(argv[5]);

                    FILE *fp3 = fopen("replica.csv", "w");
                    for(j=0;j<i;j++)
                    {
                        fprintf (fp3, "%s,%d,%d,%.2lf\n", item[j], quantity[j], limit[j], cost[j]);
                    }   

                    fprintf(fp3, "%s,%d,%d,%.2lf\n", add1, add2, add3, add4);

                    fclose(fp1);
                    fclose(fp3);
                    remove("inventory.csv");
                    rename("replica.csv", "inventory.csv"); 
                }
user3251142
  • 175
  • 7
  • 16
  • 1
    can you add more code and an example of call with command line args? – C.B. Mar 11 '14 at 14:07
  • Can you please elaborate on the "doesn't work" part? Perhaps edit your question to include a more complete example (preferably a so-called [SSCCE](http://sscce.org/))? And you *have* at least five command line arguments, with the fifth being the number? Please edit your question to show how you invoke your program as well. – Some programmer dude Mar 11 '14 at 14:08
  • Are you saying that you have *four* command line arguments, and want to get the value from the *fifth*? Remember that the indexes for `argv` are `1` for the first argument, `2` for the second, etc. – Some programmer dude Mar 11 '14 at 14:12
  • my bad, the first is actually the name of the program i want to run, while the argv[]s onward are just parameters id need for that program – user3251142 Mar 11 '14 at 14:12
  • 2
    Please show the *complete* and *actual* command line, and a *complete* code example. – Some programmer dude Mar 11 '14 at 14:15
  • On which platform are you (e.g. Windows, Linux OSX)? What terminal program are you using? Because unless you have a space before or after the decimal point in the number, then it seems the terminal program cuts your command line at the decimap point. – Some programmer dude Mar 11 '14 at 14:22
  • Show declaration (and allocation) of `add4` – chux - Reinstate Monica Mar 11 '14 at 14:23
  • im using command prompt with windows 7, and no spaces – user3251142 Mar 11 '14 at 14:24
  • One thing you can try, print the value of `argv[5]`, like `printf("argv[5] = '%s'\n", argv[5]);`. Then you can see exactly what is passed to you. Also print `argc` to see how many arguments are passed to you, if it's not `6` when you pass five arguments, there's something wrong. – Some programmer dude Mar 11 '14 at 14:25
  • for some odd reason nothing gets printed. it seems like printf isnt working at all – user3251142 Mar 11 '14 at 14:27
  • You didn't forget the newline? Also, please show the declarations of the `addX` variables, especially `add1`. – Some programmer dude Mar 11 '14 at 14:30
  • wow, i just made the dumbest mistake of all time. there is no problem anymore. >. – user3251142 Mar 11 '14 at 14:32
  • Care to elaborate? What was the mistake? Could it be something that other can learn from? – Some programmer dude Mar 11 '14 at 15:14
  • it was due to running my executable which i had changed to a different name, instead of the actual saved file.. – user3251142 Mar 12 '14 at 19:43

2 Answers2

1

Enable more warnings and check the return value of sscanf.

% clang test.c
test.c:5:28: warning: invalid conversion specifier '.' [-Wformat-invalid-specifier]
        int n = sscanf(argv[1], "%.2lf", &x);
                                 ~^
1 warning generated.

atof works for me. Make sure you're including stdlib. You could also try stdtod(argv[5], NULL).

Kevin
  • 53,822
  • 15
  • 101
  • 132
0

Try:

sscanf(argv[5], "%lf", &add4);
Mark J. Bobak
  • 13,720
  • 6
  • 39
  • 67