Command Line argument help, basic program

This is a discussion on Command Line argument help, basic program within the C Programming forums, part of the General Programming Boards category; hello, i have only recently started learning C and i still havent really got to grips with the basics... I ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    13

    Exclamation Command Line argument help, basic program

    hello, i have only recently started learning C and i still havent really got to grips with the basics...

    I am writing a program to determine the roots of a quadratic equation, using command line arguments. The program compiles... but doesnt do anything. I think the problem is that the program doesn't know what the variables a, b and c are. I need them to be the command line arguments (?) i.e you type "program_name a b c" ...but i'm stuck on how to make this work.

    any pointers in the right direction would be greatly appreciated:

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    int  main(int argc, char *argv[])
    {
      float a, b, c, droot, root1, root2; 
      double discriminant = (pow(b,2) - (4 * a * c));
    
      if( argc != 3 )
        {  printf("3 co-efficients were not entered. Please enter them in the order a, b, c.\n");
           exit(1);
        }
      else if( discriminant < 0 )
        {
          printf("There are 0 roots.\n");
        }
      else if( discriminant == 0 )
        {
          printf("There is 1 root.\n");
          droot = (-b) / (2*a);
          printf("Root1=%f\n", droot);
        }
      else if( discriminant > 0 )
        {
          printf("There are 2 roots.\n");
          root1 = (-b + sqrt(discriminant)) / 2 * a;
          root2 = (-b - sqrt(discriminant)) / 2 * a;
          printf("Root1=%f Root2=%f.\n", root1, root2);
        }		
      return(0);
    
    }
    Thanks!

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    Don't you...uh....need to set a, b, and c? Oh, and by the way, you need to do that before you use them in any calculations. And if you're entering three arguments on the command line, argc would have to be 4 to be valid. The program name is always argv[0].

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    Code:
    In your code:
    int  main(int argc, char *argv[])
    {
      float a, b, c, droot, root1, root2; 
      double discriminant = (pow(b,2) - (4 * a * c));   /////////// this is incorrect. a, b and c  contain junk here.
    
      if( argc != 3 )       //// This should probably be < 3..........
        {  printf("3 co-efficients were not entered. Please enter them in the order a, b, c.\n");
           exit(1);
        }
      /////////// else if( discriminant < 0 )    ///////////////// Change this from else if to just if
    ////////// this is where you start your math............
       if( discriminant < 0 )
        {
          printf("There are 0 roots.\n");
        }
    After you determine that there are enough parameters passed from the command line you must now convert the command line argv[][] into numbers.

    argv contains the values you are passing into your program as character strings. argv[0] contains the program name, argv[1] is what you want for a, argv[2] is b and so on.

    You can change the cstrings to a double with atof().

    Once you do the conversion then you can do the rest of your program.

    Jim

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    okay ill get round to changing that in a bit! thanks for the help, ill probab;ly be using these forums quite a bit from now...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with basic encryption program.
    By mmongoose in forum C++ Programming
    Replies: 5
    Last Post: 08-27-2005, 05:41 AM
  2. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  3. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-13-2002, 12:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21