Hi guys after fixing my previous bug this one has resulted. What I am trying to do:
- Make getchar() read only one character per input
- Make the while loop not accept numbers
- When the user enters duplicate characters the program should only accept the first character.
code:
// abcmail.c -- This is a program that calculates all fo ABC Mail Order Grocery activities
#define ARTICHOKE_PRICE 2.05
#define BEET_PRICE 1.15
#define CARROT_PRICE 1.19
#include <stdio.h>
#include <ctype.h>
int main (void)
{
// Declaring my Variables
char ch; // Input character
float artichoke_count; // artichoke counter in pounds
float beet_count; // beet counter in pounds
float carrot_count; // carrot counter in pounds
float artichokes_cost; // artichoke_count * price const
float beets_cost;
float carrots_cost;
float total_cost; // Will be the sum of artichoke_cost, beef_cost, carrot_cost
// Initializing my variables
artichoke_count = 0.0;
beet_count = 0.0;
carrot_count = 0.0;
/************************************************* PART 1: Collecting User Input *********************************************/
printf("Enter the letter a for artichoke, b for beet and c for carrot and q to quit: ");
while((ch = getchar()) != 'q')
{
float artichokes; // Will collect artichokes numbers from the user
float beets; // Will collect beets numbers from the user
float carrots; // Will collect carrot numbers from the user
if ('\n' == ch) // What this does is put a new line without affecting the program because the '\n'
continue; // will be regarded as a character and placed in default section
//if ((isdigit(ch)) == -1)
if (isalpha(ch))
{
ch = tolower(ch); // make the input characters to lower case if the user enters uppercase
switch(ch)
{
case 'a':
{
printf("Enter the number of arhitokes you need in pounds: ");
scanf("%f", &artichokes);
artichoke_count += artichokes;
break;
}
case 'b':
{
printf("Enter the number of beets you need in pounds: ");
scanf("%f", &beets);
beet_count += beets;
break;
}
case 'c':
{
printf("Enter the number of carrots you need in pounds: ");
scanf("%f", &carrots);
carrot_count += carrots;
break;
}
default:
//{
// if (n_count <= 1)
printf("\nI am only accepting a for arhitokes, b for beets and c for carrots");
// break;
//}
} // end of switch
}
printf("Enter the letter a for artichoke, b for beet and c for carrot and q to quit: ");
} // end of while loop
printf("You have entered %.2f artichokes, %.2f beets, and %.2f carrots\n", artichoke_count, beet_count, carrot_count); // testing
/******************************************** End of Part 1 ******************************************************************/
/******************************************* Part 2: Calculate the user's costs ********************************************/
artichokes_cost = ARTICHOKE_PRICE * artichoke_count;
beets_cost = BEET_PRICE * beet_count;
carrots_cost = CARROT_PRICE * carrot_count;
// Test line
printf("artichokes costs = %.2f\n", artichokes_cost);
printf("beets cost = %.2f\n", beets_cost);
printf("carrots cost = %.2f\n", carrots_cost);
total_cost = artichokes_cost + beets_cost + carrots_cost;
return 0;
}
-
When I try to enter input with numbers I get the following output:
Enter the letter a for artichoke, b for beet and c for carrot and q to quit: 12
Enter the letter a for artichoke, b for beet and c for carrot and q to quit: Enter the letter a for artichoke, b for beet and c for carrot and q to quit:
-
When I enter duplicate characters at once this is the output:
Enter the letter a for artichoke, b for beet and c for carrot and q to quit: bb
Enter the number of beets you need in pounds: Enter the letter a for artichoke, b for beet and c for carrot and q to quit: Enter the number of beets you need in pounds:
I hope I can get some help on this. Thanks in advance!