Reputation: 41
In this code, the computer asks the user what he wants to purchase between 1-7 options. And if the user presses the wrong option up to 3 times -> computer will print this message: "Do you want to leave or make a purchase? Press y(for leave) or n(for stay)". I know another way how to do this, but how can I stop the while loop and print a message?
!!!This is incorrect code(has mistakes), used for example!!!
#include <stdio.h>
int DisplayMenu();
double OrderPrice(int itemNumber);
int main
{
char process = 'y';
int mainOption;
while (process == 'y' || process == 'Y')
{
optionMain = DisplayMenu();
printf("\nYour choice was %d", optionMain);
};
return 0;
}
int DisplayMenu()
{
int option, optionCount;
printf("1 - ..., 2 - ..., 3 - ..., 4 - ..., 5 - ..., 6 - ..., 7 - ...");
scanf("%d", &option);
while (option > 7 || option < 1) {
printf("Make your chose between 1-7:");
scanf(" %d", &option);
optionCount++;
if (optionCount == 2) {
printf("Do you want to leave or make a purchase? Press y(for leave) or n(for stay)");
}
};
return option;
return 0;
}
double OrderPrice(int itemNumber)
{
double price;
switch (itemNumber) {
case 1 :
printf("\nSelection 1.");
price = 1.1;
break;
case 2 :
printf("\nSelection 2.");
price = 2.2;
break;
case 3 :
printf("\nSelection 3.");
price = 3.3;
break;
case 4 :
printf("\nSelection 4.");
price = 4.4;
break;
case 5 :
printf("\nSelection 5.");
price = 5.5;
break;
case 6 :
printf("\nSelection 6");
price = 6.6;
break;
case 7 :
printf("\nSelection 7.");
price = 7.7;
break;
// default:
// printf("Incorrect selection");
// price = 0.00;
}
return price;
}
Upvotes: 0
Views: 305
Reputation: 41
was solved by @tadman: You need to get input at that point, test it, and if it's a stop request, break
optionCount++;
if (optionCount == 2) {
break;
}
Upvotes: 3