C programming Upon starting, and immediately following any complete action, the
ID: 3934738 • Letter: C
Question
C programming Upon starting, and immediately following any complete action, the program displays a menu, described as follows: Program prints "Make a selection: (a)dd, (r)emove, (t)otal, or (q)uit. " a. After the user makes a selection, the next text varies. If the user selects (a): i. The program prints "Enter a Price/Pound: ". The user must enter the price per pound as a float. ii. The program prints "Enter a Weight: ". The user must enter the weight as a float. iii. The program prints a single newline character and returs to the beginning of the menu. b. If the user selects (r): i. The program prints "Enter a Weight: ". The user must enter the weight as a float. ii. The program prints a single newline character and returs to the beginning of the menu. c. If the user selects (t): i. The program prints "The current total is: $X.", where X is replaced by the current total as a float with exactly two digits after the decimal place. Truncate any extra digits rather than rounding. ii. The program prints a single newline character and returs to the beginning of the menu. d. If the user selects (q), the program quits immediately. - The program prints "Invalid entry. " when the user enters an invalid weight or price/pound. Invalid cases include negative entries and entries including characters other than numbers and periods. The program then returns to the point where the user was asked to enter the value. - The program prints "Invalid entry, try again. " when the user enters something invalid into the menu. The program then returns to the beginning of the menu. - The program allows for an arbitrary number of transactions, rather than 500 max. You must still allow for 500 cart size.
Explanation / Answer
// C code
#include <stdio.h>
#include <string.h>
int main()
{
char choice;
float price, weight;
float total = 0;
while(1)
{
printf(" Make a selection: (a)dd, (r)emove, (t)otal, or (q)uit: ");
scanf(" %c" ,&choice);
if(choice == 'a')
{
printf(" Enter a price/pound: ");
scanf("%f",&price);
while(1)
{
if(price < 0)
{
printf("Invalid entry ");
printf("Enter a price/pound: ");
scanf("%f",&price);
}
else
break;
}
printf(" Enter a weight: ");
scanf("%f",&weight);
while(1)
{
if(weight < 0)
{
printf("Invalid entry ");
printf("Enter a weight: ");
scanf("%f",&weight);
}
else
break;
}
total = total + price*weight;
}
else if(choice == 'r')
{
printf(" Enter a weight: ");
scanf("%f",&weight);
while(1)
{
if(weight < 0)
{
printf("Invalid entry ");
printf("Enter a weight: ");
scanf("%f",&weight);
}
else
break;
}
total = total - 2.0*weight;
}
else if(choice == 't')
{
printf(" Total: %f ",total );
}
else if(choice == 'q')
{
break;
}
else
{
printf("Invalid entry, try again. ");
}
}
return 0;
}
/*
output:
Make a selection: (a)dd, (r)emove, (t)otal, or (q)uit: a
Enter a price/pound: -3
Invalid entry
Enter a price/pound: 3
Enter a weight: 4
Make a selection: (a)dd, (r)emove, (t)otal, or (q)uit: 1
Invalid entry, try again.
Make a selection: (a)dd, (r)emove, (t)otal, or (q)uit: a
Enter a price/pound: 3
Enter a weight: 4
Make a selection: (a)dd, (r)emove, (t)otal, or (q)uit: t
Total: 24.000000
Make a selection: (a)dd, (r)emove, (t)otal, or (q)uit: a
Enter a price/pound: 3
Enter a weight: 4
Make a selection: (a)dd, (r)emove, (t)otal, or (q)uit: t
Total: 36.000000
Make a selection: (a)dd, (r)emove, (t)otal, or (q)uit: r
Enter a weight: 2
Make a selection: (a)dd, (r)emove, (t)otal, or (q)uit: t
Total: 32.000000
Make a selection: (a)dd, (r)emove, (t)otal, or (q)uit: q
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.