Just a heads up, this is for a class assignment, but I'm only asking for help with one part of it. Basically we need to make a cash register type program, with the ability to add an item, compute change, print a total, reset it, and also some extra functions for converting the total to binary and hex. The main restriction however is that scanf is only allowed to read using %c, so no reading things as numbers or a whole line. This is kinda where my problem comes in, as I'm not sure how C actually does this. Here's the code I'm using for my main menu:
Code:
void main()
{
float sum = 0;
float change = 0;
char command = '0';
int runCommand = TRUE;
printMenu();
while (command!='6')
{
scanf(" %c", &command);
if(command=='1') {
sum += enterItem();
printMenu();
}
else if(command=='2') {
printf("%s%.2f\n\n", "The total cost is: $", sum);
printMenu();
}
else if(command=='3') {
printf("%s%.2f\n\n", "The total cost is: $", sum);
printMenu();
}
else if(command=='4') {
change = computeChange(sum);
printf("%s%5.2f\n\n", "The total change is: $", change);
printMenu();
}
else if(command=='5') {
sum = 0;
printf("%s\n\n", "The cash register has been reset.");
printMenu();
}
else if(command=='6') {
break;
}
else {
printf("Incorrect option.\n");
printMenu();
}
}
return;
}
So far it seems to be working fine when only one character is entered, but when the user enters multiple characters it reprints the menu multiple times and goes through doing the right command, or printing the error message, for each character. My question is this: how can I make it print an error message and the menu once if the user enters multiple characters as one input? I've got a bunch of other problems I need to figure out, but with this out of the way I think I can figure them out. Thanks for any help.