Thread: Why wont isalpha() work in my program?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    23

    Why wont isalpha() work in my program?

    I wrote this program for my class that will display how many toilets are in a community and how much water is saved by switching to a low-flush toilet, but when i check to make sure the user doesn't enter an alphabet character for the population it isn't working.

    If you could tell me how i am using this function incorrectly it would be greatly appreciated.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    void main(void)
    {
    
    	int low_flush = 2; //liters per flush
    	int old_flush = 15; //liters per flush
    	int flush_dif = 0;
    	int total_people = 0;
    	float toilets = 0;
    	int toilet_flush = 14; //times a day
    	int cost_to_install = 150; //dollars
    	int cost_of_toilets = 0;
    	int water_saved = 0; //Amount of water saved
    
    	printf("\nThis program will tell you how much your community will");
    	printf("\nsave by switching to the low-flush model toilets.");
    
    	printf("\n\nHow many people are in your community?: ");
    	scanf("%d", &total_people);
    
    	if(isalpha(total_people))
    	{
    		printf("\nYou entered a non-numeric value. Please try again\n");
    	}
    	else
    	{
    		if(total_people > 0)
    		{
    			toilets = total_people / 3;
    
    			if((total_people % 3) != 0)
    				toilets += 1;
    
    			cost_of_toilets =  cost_to_install * toilets;
    			flush_dif = old_flush - low_flush;
    			water_saved = flush_dif * toilets;
    
    			if(total_people <=3)
    			{
    				printf("\nThere is %.0f toilet in your community.", toilets);
    			}
    			else
    				printf("\nThere are %.0f toilets in your community.", toilets);
    				printf("\nWith the low flush models your community will save %d liters", water_saved);
    				printf("\nof water per flush.\n\n");
    		}
    		else
    		{
    			printf("\nIt is impossible to have this amount of people in a community.");
    			printf("\nPlease try again.");
    			printf("\n\n");
    		}
    	}
    
    	return;
    
    }
    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    When you do scanf("%d", &total_people), you're explicitly asking for a number. If the user enters something that isn't a number (and an integral one at that), then total_people will not be touched by scanf(). What that means is that there is absolutely no use for isalpha() here. In fact, isalpha() should only be used when you have a character that you need to check (such as returned by getc() or through scanf("%c")). It's not a general purpose "did this input contain alphabetic characters?"; rather, it's more like, "is this particular character an alphabetic character?"

    If scanf() fails to read a number (e.g. you typed in a letter), it will return 0, which in this case means it performed no conversions, i.e. it didn't process anything. You're in a bad spot in this case, because the next time you try to read, that letter will still be there. If you do scanf("%d") again, it will fail, and leave the letter there for the next input function. And so on. I suggest reading about scanf()—especially its return value—in your documentation.

    Reading text input in C is tricky business. Generally speaking the best combination of robustness and ease-of-use is to read an entire line with fgets() and then pick it apart (with sscanf(), strtol(), etc). This isn't perfect, but it's a decent way to start.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    A quick and dirty example with scanf() and checking input:
    Code:
    #include <stdio.h>
    
    int main(void){
    
    	int number;
    
    	printf("Enter a number: ");
    	
    	while(scanf("%d", &number) != 1){ //scanf returns number of successful inputs
    		printf("Incorrect input. Enter a number: ");
    		getchar(); //get rid of trailing newline
    	}
    
    	printf("You entered %d", number);
    
    	return(0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy Program, Wont Work With Large N
    By breenmachine in forum C Programming
    Replies: 10
    Last Post: 07-05-2007, 11:27 AM
  2. program wont work, why?
    By chasingxsuns in forum C Programming
    Replies: 11
    Last Post: 02-02-2006, 04:38 PM
  3. program from book wont work
    By cemock in forum C Programming
    Replies: 2
    Last Post: 03-06-2003, 09:58 AM
  4. gets wont work
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-31-2002, 02:31 PM
  5. Heeeelpppppp Please! Program Wont Work!
    By MagiZedd in forum Windows Programming
    Replies: 3
    Last Post: 11-18-2001, 12:18 AM