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



Develop a program that computes the new price of an item. The program should receive a character v..

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

Post New Answer

More C Interview Questions

Why do we use stdio h and conio h?

0 Answers  


write a program in c language to print your bio-data on the screen by using functions.

6 Answers   College School Exams Tests, IBM,


the constant value in the case label is followed by a a) semicolon b) colon c) braces d) none of the above

0 Answers  


what is the height of tree if leaf node is at level 3. please explain

0 Answers  


How can I write functions that take a variable number of arguments?

0 Answers  






Write a code to generate divisors of an integer?

0 Answers   Ericsson,


define string ?

0 Answers  


WRITE A CODE IN C TO SEARCH A FILE FROM NOTEPAD FILE.

0 Answers   Google, Infosys,


write a program wch produces its own source code aas its output?

1 Answers   IonIdea,


1) There is a singing competition for children going to be conducted at a local club. Parents have been asked to arrive at least an hour before and register their children’s names with the Program Manager. Whenever a participant registers, the Program Manager has to position the name of the person in a list in alphabet order. Write a program to help the Program Manager do this by placing the name in the right place each time the Program Manger enters a name. 2) the Event Manager has to send participants to the stage to perform in the order in which they registered. Write a program that will help the Event Manager know who to call to the stage to perform. The Logic should be in Data Structures

0 Answers   KPIT,


Create a structure to specify data on students given below: Roll number, Name, Department, Course, Year of joining Assume that there are not more than 450 students in the college. 1.write a function to print names of all students who joined in a particular year 2.write a function to print the data of a student whose roll number is given

0 Answers   XYZ,


How do you search data in a data file using random access method?

0 Answers  


Categories