1

the issue I have is if there are not enough arguments supplied from the command line I want the program to request all the values instead. For some reason, it works when I supply 2 out of the 3 arguments but I want it to work if 1 or none of the arguments are supplied.

My Code

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

    float areaOfcircle(float radius_circle)
    {
        float area_circle;
        area_circle = M_PI * radius_circle * radius_circle;

        return area_circle;
    }
    void resitance_current(float length, float area_circle, float voltage, float* resistance, float* current)
    {
        float resistivity;
        resistivity = 1.782*pow(10, -8);
        *resistance = ((resistivity*length) / area_circle);
        *current = (voltage / *resistance);
    }

    void radius_check(float radius)
    {
        if (radius <= 0)
        {
            printf("Radius cant be less than or equal to 0");
            exit(1);
        }
    }
    void voltage_check(float voltage)
    {

        if (voltage <= 0)
        {
            printf("Voltage cant be less than or equal to 0");
            exit(1);
        }
    }
    void length_check(float length)
    {
        if (length <= 0)
        {
            printf("Length cant be less than or equal to 0");
            exit(1);
        }
    }

    void validation(float radius, float voltage, float length)
{
    radius_check(radius);
    voltage_check(voltage);
    length_check(length);
}
    int main(int argc, char *argv[])
    {
        float radius, voltage, length, current, resistance;
        float length_u, length_l;
        //dumpargs(argc, argv);
        radius = atof(argv[1]);
        voltage = atof(argv[2]);
        length = atof(argv[3]);
        if (argc != 4)
        {
            printf("Not enough arguments supplied\n");
            printf("Enter the radius of wire : ");
            scanf("%f", &radius);
            radius_check(radius);
            printf("Enter the Voltage of circuit : ");
            scanf("%f", &voltage);
            voltage_check(voltage);
            printf("Enter the Length of Wire : ");
            scanf("%f", &length);
            length_check(length);
        }
        validation(radius, voltage, length);
        resitance_current(length, areaOfcircle(radius), voltage, &resistance, &current);
        printf("Resistance = %f , Current = %f\n", resistance, current);
        printf("\nEnter the Upper Length of Wire : ");
        scanf("%f", &length_u);
        printf("\nEnter the Lower Length of Wire : ");
        scanf("%f", &length_l);
        if ((length_l < 0) || (length_l >= length_u))
        {
            printf("\nImpossible for Lower Length < 0 or to be larger then Length Upper");
            exit(1);
        }
        else
        {
            for(length_l = length_l; length_l<=length_u; length_l++)
            {

                resitance_current(length, areaOfcircle(radius), voltage, &resistance, &current);
                printf("\nLength = %0.3f Resistance = %0.3f , Current = %0.3f", length, resistance, current);
                length = (length_l + 1);

            }
        }
        return 0;
    }

As you can see from the code "if (argc != 4)" then the values are requested again and over written if they were supplied via command line. im trying to find a sulution to this so if the program on its own was run from CMD the the values would be requested but if all values were supplied the code would work its way through. Other than the argc iss the code opperates as reqired.

Thanks for any help in advance

  • One problem will be knowing *which* argument(s) are missing from the command line. You have no choice but to ask for all of them, unless the input is something like `myprog v=1.5` etc. – Weather Vane Jan 14 '19 at 17:36
  • Only 3 additional arguments are entered via command line along with the program name so 4 in total. They have to be entered by the user in the correct order as a specification so radius first voltage second and length third. As many can be supplied but the code will only read the 3 after the program name. But what I want is if any of those 3 are not supplied the code should ask for them all. Weather Vane – Brian Strugnell Jan 14 '19 at 17:40
  • You've already blown up if no arguments are provided. You must test `argc` before you call any of `radius = atof(argv[1]); voltage = atof(argv[2]); length = atof(argv[3]);` – Jonathan Leffler Jan 14 '19 at 17:40
  • How do you test argc? @JonathanLeffler – Brian Strugnell Jan 14 '19 at 17:41
  • 1
    If you can enforce order of the args you can use a `switch` with fall through. [`getopt`](https://www.gnu.org/software/libc/manual/html_node/Getopt.html) is a also an option. – 001 Jan 14 '19 at 17:41
  • `if (argc != 4)` of course — like you do when it is too late to be helpful. – Jonathan Leffler Jan 14 '19 at 17:41
  • You should also check that each of the `scanf()` calls succeeds — and that each of the `atof()` calls succeeds. It's easier to test `strtod()` than `atof()`, as it happens. You should finish outputs with a newline (instead of, or as well as, starting them with a newline). You should report errors on `stderr` and not `stdout` — that's what it is for. – Jonathan Leffler Jan 14 '19 at 17:44
  • You're consistent, so at one level it doesn't matter, but `resitance_current` should probably be `resistance_current`. Also, the dictionary definition of "cant" — _hypocritical or sanctimonious talk_ — is quite different from "can't" — contraction of "cannot". – Jonathan Leffler Jan 14 '19 at 17:48
  • FWIW (which is probably approximately zero), I prefer to insist on the correct number of arguments rather than ignore extras. That is, if the command should be invoked with 3 arguments (plus command name), then invoking with 4 arguments or more should generate a usage message. In my code, I'd generate a usage message for any number of arguments other than 4 — I don't want programs to try going interactive on me. However, the output is then terser than in this example as it is probably intended for use in shell scripts, etc. – Jonathan Leffler Jan 14 '19 at 17:56

1 Answers1

3

You access out of bound if arguments are passed less than 4.

Just modify the code as below. That is first check the number of arguments passed then access the argv.

    if (argc != 4) // or if (argc < 4)
    {
        printf("Not enough arguments supplied\n");
        printf("Enter the radius of wire : ");
        scanf("%f", &radius);
        printf("Enter the Voltage of circuit : ");
        scanf("%f", &voltage);
        printf("Enter the Length of Wire : ");
        scanf("%f", &length);
    }
    else {

        radius = atof(argv[1]);
        voltage = atof(argv[2]);
        length = atof(argv[3]);
    }
    radius_check(radius);
    voltage_check(voltage);
    length_check(length);

Also you need to check the return value of scanf, Read this on how to check scanf return value.

kiran Biradar
  • 12,700
  • 3
  • 19
  • 44