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