Thread: Command Line Arguements

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    6

    Command Line Arguements

    Hi, This is my first posting to this forum. I have been reading it for a couple of weeks and have learned a great deal. I have just started learnging C in college and am currently taking a class. I am stuck on the current problem that we have. Well, I am stuck on part of the problem. I searched the board and could not find an answer. Here it is. I am writing a program that figures gas consumption at a pay at a pump terminal. Part of the program is to enter the current gas price as a command line arguement when the program is initialised. I have to prompt the user for the gas price if the user does not put it in. I have written code but that part of the program seems to be ignored. The rest of the program works perfectly except for this part about what happens if the user does not put in the price. Here is what I have:

    <code>
    int main(int argc, char *argv[])
    {
    int a;
    if (argc < 0)
    {
    printf("Please input the current gas price per gallon: ");
    scanf("%s", argv[a])
    }


    return 0;
    }
    </code>


    This isn't all of the code for the program. I am at work and can't access it right now. If you need the whole program, I will post it tonight.

    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    6

    Sorry about the Code Tags

    I thought I had the code tags right. sorry

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    use [ instead of < for the code tags
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >if (argc < 0)
    Generally, argc will be 1 if there are no arguments. This test should be:
    Code:
    if ( argc != 2 )
    >scanf("%s", argv[a])
    I can only assume you meant to read the price into a. As it is you are causing all kinds of undefined behavior by accessing argv with an indeterminate subscript. Your code should look something like this for a school project:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( int argc, char **argv )
    {
      float price;
    
      if ( argc != 2 ) {
        printf ( "Please input the current gas price per gallon: " );
        scanf ( "%f", &price );
      }
      else
        price = atof ( argv[1] );
    
      printf ( "%f\n", price );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    6

    Thanks

    Prelude,

    Thanks. I was totaly off.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command line arguements in functions.
    By SlyMaelstrom in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2005, 07:41 AM
  2. Comparing command line arguements to a string.
    By SlyMaelstrom in forum C++ Programming
    Replies: 14
    Last Post: 10-30-2005, 04:56 AM
  3. command line arguements
    By screamer903 in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 07:59 AM
  4. Arguements for gnome?
    By mart_man00 in forum Tech Board
    Replies: 12
    Last Post: 07-30-2003, 04:46 PM
  5. Command Line Arguements
    By scaven in forum C Programming
    Replies: 2
    Last Post: 04-13-2003, 05:36 PM