Thread: Using isdigit and atoi

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    61

    Using isdigit and atoi

    My program calculates the first n numbers of the fibonacci series. For error checking, I've prevented the program from trying to calculate a negative number, but I'm having trouble with character inputs.

    Here is my code to detect character inputs:
    Code:
        if(isdigit(argv[2]) !=0){
            number = atoi(argv[2]);
            printf("%d Fibonacci numbers\n",number);
        
        }
        
        else{
            printf("You must input a positive integer); 
        }
    I read online that isdigit's return value for a true condition is a non-zero number. When my program reaches this point, it crashes. When I remove these lines of code from my program it runs perfectly.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You'll have to post all of your code. Although it may be crashing in the code you've posted, the error is elsewhere (unless argv[2] is null or non-existant).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    Code:
     #include <stdio.h>
     #include <stdlib.h>
     
     #define MAXCHAR 256
    
     
     
     void main( int argc, char *argv[])
     {
     int fibonacci(int n);
     
     FILE *out=NULL;
     int count;
     char check[MAXCHAR];
     char filename[MAXCHAR];
     int number;
     int fib[MAXCHAR];
     int flag;
     
    if(argv[1] == NULL){                                    
        printf("This program calculates a series of Fibonacci numbers\n");            
    
        
     }
     
    if(argv[1] !=NULL){
        strcpy(check,argv[1]);            
    }
    
    if(argv[1] !=NULL){
        if(strcmp(check, "/?") == 0 || argc < 3 || atoi(argv[2]) < 0){        
            printf("This program calculates a series of Fibonacci numbers\n");        
    
            flag = 1;                                                                
            
            }
        strcpy(filename, argv[1]);            
    }
    
     if(argc >= 3 && flag != 1) {         
            out = fopen(filename, "w");            
     }
    
        
    
        printf("Test 1\n");
    if(argc >= 3 && flag != 1){    
    
        
        if(isdigit(argv[2]) !=0){                        
            number = atoi(argv[2]);                    
            printf("The first %d Fibonacci numbers\n",number);
        
        }
        
        else{
            printf("This program calculates a series of Fibonacci numbers\n");
    
            flag=1;                                                        
        }
        
    }    
        
    if(argc >=3 && flag != 1){    
        for(count = 0; count < number; count++){
            printf("%d ",fibonacci(count));
            }
        }
    if(argc >=3 && flag != 1){
        fprintf(out,"The first %d Fibonacci numbers\n",number);    
        for(count = 0; count < number; count++){
            fprintf(out,"%d ",fibonacci(count));
            }
        }
     printf("\nTest 2\n");
     
    
    if(out !=NULL){    
        fclose(out);
        }
     }

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by november1992 View Post
    Code:
        if(isdigit(argv[2]) !=0){
            number = atoi(argv[2]);
    isdigit() and atoi() take different types of parameter. The first takes an int; the second takes a string.
    You passing argv[2] to both functions. At least one of the calls is wrong.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by qny View Post
    isdigit() and atoi() take different types of parameter. The first takes an int; the second takes a string.
    You passing argv[2] to both functions. At least one of the calls is wrong.
    I missed that. Well spotted. It didn't occur to me that the OP was trying to apply isdigit to the entire string.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    Could I use strlen to figure out how long the input is and then use isdigit to check the first element?
    Last edited by november1992; 10-05-2012 at 03:26 PM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    EDIT: Woah, I guess I got carried away at work for longer than I thought...opened this thread a while ago, several people already covered this.

    This is certainly a problem:
    Code:
    isdigit(argv[2])
    isdigit takes an int, argv[2] is a char * (remember, argv itself is an array of char * or a char **). Perhaps you want
    Code:
    isdigit(argv[2][0])

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    Cool, it works now! Thanks.
    Although, I'm curious why my idea didn't work. I used strcpy to put argv[2] into a temp and then use isdigit on the first element. If isdigit returned 0, it printed "Only positive integers can be used" and then exited the for loop. But it skipped the rest of the code.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Post the exact code that didn't work, and what input you gave, that caused this problem. I don't see argv[2] being copied into a temp var anywhere.

  10. #10
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    For the record, main should be declared as returning int (and it should also return 0 or EXIT_SUCCESS to indicate success).

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    I came up with the idea after I posted the original code. But i realized I included parts of the code that skipped into the if statement. It works now.


    I write void main because that's what my professor taught my class to use. What's wrong with using it?
    Last edited by november1992; 10-05-2012 at 04:22 PM.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by november1992 View Post
    I write void main because that's what my professor taught my class to use. What's wrong with using it?
    Read this: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. isdigit()
    By a.mlw.walker in forum C Programming
    Replies: 10
    Last Post: 04-14-2009, 11:43 AM
  2. isdigit
    By quasigreat in forum C Programming
    Replies: 2
    Last Post: 06-27-2008, 02:57 PM
  3. isdigit
    By AmazingRando in forum C Programming
    Replies: 3
    Last Post: 09-21-2003, 03:14 PM
  4. isdigit()
    By stargazer66 in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2003, 07:02 PM
  5. isdigit ()
    By mackol in forum C++ Programming
    Replies: 8
    Last Post: 01-18-2003, 11:03 PM