Using isalpha()

This is a discussion on Using isalpha() within the C Programming forums, part of the General Programming Boards category; I have tried & tried & tried to figure out what's wrong with my code but I can't . I's ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    46

    Question Using isalpha()

    I have tried & tried & tried to figure out what's wrong with my code but I can't . I's supposed to give an error if the user enters anything else but an integer . So something like : 34j , should be comming up as an error.
    Thanks for any help you can give !

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    #define MAXLINE 25  
    
    int check(char *line);
    char line[MAXLINE];
    int error,n,digit,flag;
    int i = 0;
    int flag = 0;
    
    
    void main(){ 
    	
    	do{
    		printf("Input a posetive integer: ");
    		if (fgets(line,MAXLINE,stdin) != NULL){
    		 digit = check(&line);
    		 error = ((sscanf(line,"/t%d/t",&n) != 1 || n <= 0) && (digit == 1));  
    		}
    		if (error)
    			printf("\nERROR: Do it again .\n");
    		else 
    			printf("There was no error!");
    	}while (error);
       
    }
    
    int check(char *line){ 
    	while((i < MAXLINE) && (flag == 0)){
    		if (isalpha((int)line[i]))
    	      flag = 1;
    	    ++i;
    	}
    	if (flag = 0)
    		return 0;
    	else
    		return 1;
    }

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    Common problem, a swap of the assignment and the testing operator...

    Code:
      	if (flag = 0)
     		return 0;
     	else
     		return 1;
    notice you're assigning flag to 0, not testing if it EQUALS zero

    Simple mistake that we all make every now and then, just change the = to a == and you'll be set to go

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    Thanks! Ive done that , but while debugging after i've entered the values 5t , t5, t, 5
    the 5 still gives an error while it shouldn't ??

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    You're using a lot of global variables in this program that are causing you more of a headache then anything, heh.

    What's happening is

    1) not resetting the i to 0
    2) not resetting the flag to 0
    3) you should only test (i < strlen(line)) in order to only test the inputted data.

    Code:
     int check(char *line){ 
     	i=0;
     	flag=0;
     	while((i < strlen(line)) && (flag == 0))
     	{
     		if (isalpha((int)line[i]))
     		  flag = 1;
     		++i;
     	}
     	if (flag == 0)
     		return 0;
     	else
     		return 1;
     }

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    Thank you soooo much it works now, it was such a pain in the butt this program lol . Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindrome, problem with isalpha
    By Kyeong in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 01:28 PM
  2. Problem with isalpha
    By kron_19792000 in forum C Programming
    Replies: 3
    Last Post: 09-26-2005, 01:13 AM
  3. isalpha || ispunct
    By linuxdude in forum C Programming
    Replies: 3
    Last Post: 05-21-2004, 10:43 PM
  4. isalpha
    By mackol in forum C Programming
    Replies: 9
    Last Post: 11-29-2002, 02:55 PM
  5. Floating Point isalpha();
    By UnclePunker in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:00 PM

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