Develop a program that computes the new price of an item.
The program should receive a
character variable colour and a double precision
floating-point variable price from the
user. Discount rate is determined based on the colour of the
discount sticker, as shown in the
following table. An error message should be printed if an
invalid colour has been entered
Answer / tw
I'm still learning, but I made this. The color input isn't
validated for random strings characters (will get a seg
fault). Still need to add the calculations
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void colors() {
int ctr = 0;
double price;
char color[20], tmp[20];
char colorMenu[9][20] = {" ", "WHITE", "BLACK", "RED",
"GREEN", "BLUE", "ORANGE", "YELLOW", "PURPLE"};
int discount[9]= { 0, 15, 20, 25, 30, 35, 40, 45, 50 };
do {
printf("\n\nEnter the discount sticker color -> ");
fgets(tmp, 20, stdin);
sscanf(tmp, "%s", &color);
} while (isalpha(color[1]) == 0);
do {
printf("\n\nEnter the price -> ");
fgets(tmp, 20, stdin);
} while (sscanf(tmp, "%f", &price) == 0);
for (int x = 0; color[x] != '\0'; x++)
color[x] = toupper(color[x]);
for (ctr = 0; (strcmp(color, colorMenu[ctr]) != 0); ctr++);
printf("\nPICK: %d", ctr);
printf("\nYOU CHOSE: %s, DISCOUNT: %d percent off",
colorMenu[ctr], discount[ctr]);
}
int main() {
colors();
return (0);
}
Is This Answer Correct ? | 0 Yes | 0 No |
typedef struct { int i:8; char c:9; float f:20; }st_temp; int getdata(st_temp *stptr) { stptr->i = 99; return stptr->i; } main() { st_temp local; int i; local.c = 'v'; local.i = 9; local.f = 23.65; printf(" %d %c %f",local.i,local.c,local.f); i = getdata(&local); printf("\n %d",i); getch(); } why there there is an error during compiling the above program?
I want tcs placement papers of 2004-2009 , its urgent
why we are using float in C
When should I declare a function?
what is the difference between %d and %*d in c languaga?
Why cd or dvd are round why not square.
What is #include called?
#include<stdio.h> main() {int i=1;j=1; for(;;) {if(i>5) break; else j+=1; printf("\n%d",j) i+=j; } }
when to use : in c program?
What is multidimensional arrays
What is a scope resolution operator in c?
Explain can you assign a different address to an array tag?