command line program, something wrong in the argv

This is a discussion on command line program, something wrong in the argv within the C Programming forums, part of the General Programming Boards category; OS win98SE compiler dev c++ problem : please check at my modulus sign at check int() function, and also if ...

  1. #1
    Unregistered
    Guest

    command line program, something wrong in the argv

    OS win98SE
    compiler dev c++
    problem : please check at my modulus sign at check int() function, and also if can, please let me know what happen while i'm passing array, if you're generouse enough, please re write this code using pointer instead of array, thank you

    /*
    Program name : chkinp.exe

    Programmer :
    Description : A program which checking number of arguments that are passed
    from the command-line. The program itself will determine
    what kind of argument that has been passed by the user, whether
    a number, a palindrome, a word

    Date : 09/26/2001
    */


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

    #define puts printf


    int check_int(char argv[]);
    int check_float(char argv[]);
    int check_alpha(char argv[]);

    int main(int argc, char **argv)
    {
    int i,c_word, c_palind, c_numb; /*declaring integer type for counter*/

    for(i=1;i<argc;i++)
    {
    argv[i];

    if(isdigit(argv[i])&&ispunct(argv[i]))
    c_numb+=check_float(argv[i]);

    else if(isdigit(argv[i]))
    c_numb+=check_int(argv[i]);
    }

    for(i=1;i<argc;i++)
    {
    argv[i];
    if(isalpha(argv[i]))
    c_word+=check_alpha(argv[i]);
    }


    puts(" There were %d argument(s) \n ", argc-1 ); /* total number of arguments */
    puts(" %d word(s) \n ", c_word ); /* total number of words */
    //puts(" %d palindrom(s)\n ", c_palin); /* total number of palindromes */
    puts(" %d number(s) \n ", c_numb ); /* total number of numbers */

    return 0;
    }

    int check_float(char argv[])
    {
    float a = 0.0;
    a = atof(argv);
    printf("%f",a);

    if((a%2.0)==0.0)
    puts("The number %f is a real number that is even\n",a);
    else
    puts("The number %f is a real number that is odd\n",a);
    return 1;
    }

    int check_int(char argv[])
    {
    int b = 0;
    b = atoi(argv);
    if((b%2)==0)
    puts("The number %d is a integer number that is even\n",a);
    else
    puts("The number %d is a integer number that is odd\n",a);
    return 1;

    }

    int check_alpha(char argv[])
    {
    puts("%s\n", argv);
    return 1;
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Umm, I can't rewrite this, but I can go through it line by line...
    Code:
    // I suggest not using this macro, as there is a library
    //  function puts.
    #define puts printf 
    
    int main(int argc, char **argv) 
    { 
     int i,c_word, c_palind, c_numb; /*declaring integer type for counter*/ 
    
     for(i=1;i<argc;i++) 
     { 
      // isdigit and ispunct operate on ints, not strings.
      // so first off, argv[i] is a char *, not an int.  You should try
      // isdigit(*argv[i]), so you can pass it a char, (which
      // will work as an int)
      // Second, since these are for comparing characters, 
      // you'll never find a char that's both a digit and a punct.
    
      // isdigit (*argv[i]) && ispunct (*argv[i])
      if(isdigit(argv[i])&&ispunct(argv[i])) 
      // So this function will never be called.
      c_numb+=check_float(argv[i]); 
    
      // isdigit (*argv[i])
      else if(isdigit(argv[i])) 
      c_numb+=check_int(argv[i]); 
     } 
     // I don't see the reason in using 2 for loops that do the
     // same conditions like this.
     for(i=1;i<argc;i++) 
     { 
      // isalpha(*argv[i])
      if(isalpha(argv[i])) 
      c_word+=check_alpha(argv[i]); 
     } 
    
    
     puts(" There were %d argument(s) \n ", argc-1 ); /* total number of arguments */ 
     puts(" %d word(s) \n ", c_word ); /* total number of words */ 
    //puts(" %d palindrom(s)\n ", c_palin); /* total number of palindromes */ 
     puts(" %d number(s) \n ", c_numb ); /* total number of numbers */ 
    
     return 0; 
    } 
    
    int check_float(char argv[]) 
    { 
     float a = 0.0; 
     a = atof(argv); 
     printf("%f",a); 
    
     // The floats may be stored with round off errors so 
     // that this is not accurate...
     if((a%2.0)==0.0) 
      puts("The number %f is a real number that is even\n",a); 
     else 
      puts("The number %f is a real number that is odd\n",a); 
     return 1; 
    } 
    
    int check_int(char argv[]) 
    { 
     int b = 0; 
     b = atoi(argv); 
     if((b%2)==0) 
     // You're passing a, but you need to pass b.  That's why this
     // function won't work.
      puts("The number %d is a integer number that is even\n",a); 
     else 
      puts("The number %d is a integer number that is odd\n",a); 
     return 1; 
    } 
    
    int check_alpha(char argv[]) 
    { 
     puts("%s\n", argv); 
     return 1; 
    }
    The only thing I'm not ure about is the isdigit(), ispunct() stuff, since you're using C++, those may operate differently, but as it is in C, those will only check the first character, and you have to pass them an int or a char, not a char *.
    Last edited by QuestionC; 09-26-2001 at 09:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  3. Calendar Program. What am I doing wrong?
    By Sektor in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2004, 10:39 PM
  4. Any suggestions on whats wrong with this C Program?
    By thephreak6 in forum C Programming
    Replies: 3
    Last Post: 10-19-2002, 07:50 PM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2002, 02:54 PM

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