can someone please check my code because everytime i enter y it keeps prompting
ID: 3664220 • Letter: C
Question
can someone please check my code because everytime i enter y it keeps prompting me the message please enter your choice (y, n or q) instead of saying great choice goodbye for y
#include <stdio.h>
char GetInput(void){
/* while this is true.
It asks user to input a choice of y, n, or q.
If statement checks whether input is y, n, or q.
If user inputs a y, n, or q, then it would break out of while loop;
Otherwise it would continue to ask the user to print one of the three letters */
while(1){
char input;
printf("Please enter your choice (y for yes, n for no, q for quit) ");
scanf(" %c", &input);
if (input=='y' || input=='n' || input=='q'){
return input;
//break;
}
}
}
/* while this is true.
It asks user if they want an upgrade, keeps printing this message unless they input yes.
Once they enter yes the while loop breaks */
void SellUpgrade(int q_limit, int mbps, int mbps_increase, int price, int price_increase){
while(1){
printf("You are eligible for a limited time offer. You can get %d megabits per second for the low promotional rate of $%d a month. Act now while supplies last. ", mbps, price);
char x=GetInput(); // x is now getInput
int count=0; //sets initial count to 0
if(x=='y'){ //if user inputs y, then it prints the message
printf("Great choice. Goodbye. ", x);
break;
}
if(x=='n' || x=='q'){ // if user inputs n or q it continues to print the service upgrade message
return;
}
if (x=='q'){
count++; // increases count
if(count>q_limit){
return;
}
}
}
}
void PayBill(void){
printf("We already have your credit card information and have charged you.Thanks for being a loyal customer! ");
}
int main (){
int choice; // user has option to choose 1-4
printf("Welcome to Cablecom! Please make a choice from the following options: "); //opening menu
printf("1) Change your password ");
printf("2) Cancel your service ");
printf("3) Schedule a service call ");
printf("4) Pay your bill ");
scanf("%d", &choice);
if (choice!=1 && choice!=2 && choice!=3 && choice!=4){ // if choice is not between 1 and 4
printf("Please enter a number between 1 and 4. "); // prints the statement if choice is not between 1 and 4
scanf("%d", &choice); //user inputs a choice and feeds it into choice
}
else if (choice==1){
SellUpgrade(2, 10, 1, 50, 1);
}
else if (choice==2){
SellUpgrade(5, 100, 10, 80, 20);
}
else if (choice==3){
SellUpgrade(5, 40, 5, 60, 10);
}
else if (choice==4){
PayBill();
}
return 0;
}
Explanation / Answer
I have fixed the issues with your code and marked the changes I have made in bold.I have explained the changes but still if you have any confusion then comment on the answer.
#include <stdio.h>
char GetInput(void){
/* while this is true.
It asks user to input a choice of y, n, or q.
If statement checks whether input is y, n, or q.
If user inputs a y, n, or q, then it would break out of while loop;
Otherwise it would continue to ask the user to print one of the three letters */
while (1){
char input;
printf("Please enter your choice (y for yes, n for no, q for quit) ");
scanf(" %c", &input);
if (input == 'y' || input == 'n' || input == 'q'){
return input;
//break;
}
}
}
/* while this is true.
It asks user if they want an upgrade, keeps printing this message unless they input yes.
Once they enter yes the while loop breaks */
void SellUpgrade(int q_limit, int mbps, int mbps_increase, int price, int price_increase){
//moved count out of the loop otherwise everytime you press q and go into while it will become 0,that is why it was //not coming out of loop after pressing q twice in option 1.
int count = 0; //sets initial count to 0
while (1){
printf("You are eligible for a limited time offer. You can get %d megabits per second for the low promotional rate of $%d a month. Act now while supplies last. ", mbps, price);
char x = GetInput(); // x is now getInput
if (x == 'y'){ //if user inputs y, then it prints the message
printf("Great choice. Goodbye. ", x);
break;
}
//moved this if condition up because we should first check for count of q input and then process it in next if.
if (x == 'q'){
count++; // increases count
if (count >= q_limit){
return;
}
}
if (x == 'n' || x == 'q'){// if user inputs n or q it continues to print the service upgrade message
mbps += mbps_increase;
price += price_increase;
//replaced the return statement with continue because we don't want to break out of the while loop we wan't //to take next input with new service upgrade message with new values of mbps and price.
continue;
}
}
}
void PayBill(void){
printf("We already have your credit card information and have charged you.Thanks for being a loyal customer! ");
}
int main(){
int choice; // user has option to choose 1-4
printf("Welcome to Cablecom! Please make a choice from the following options: "); //opening menu
printf("1) Change your password ");
printf("2) Cancel your service ");
printf("3) Schedule a service call ");
printf("4) Pay your bill ");
scanf("%d", &choice);
if (choice != 1 && choice != 2 && choice != 3 && choice != 4){ // if choice is not between 1 and 4
printf("Please enter a number between 1 and 4. "); // prints the statement if choice is not between 1 and 4
scanf("%d", &choice); //user inputs a choice and feeds it into choice
}
else if (choice == 1){
SellUpgrade(2, 10, 1, 50, 1);
}
else if (choice == 2){
SellUpgrade(5, 100, 10, 80, 20);
}
else if (choice == 3){
SellUpgrade(5, 40, 5, 60, 10);
}
else if (choice == 4){
PayBill();
}
return 0;
}
Sample output for option 1 with q pressed twice
Welcome to Cablecom! Please make a choice from the following options:
1) Change your password
2) Cancel your service
3) Schedule a service call
4) Pay your bill
1
You are eligible for a limited time offer. You can get 10 megabits per second fo
r the low promotional rate of $50 a month. Act now while supplies last.
Please enter your choice (y for yes, n for no, q for quit)
q
You are eligible for a limited time offer. You can get 11 megabits per second fo
r the low promotional rate of $51 a month. Act now while supplies last.
Please enter your choice (y for yes, n for no, q for quit)
q
Press any key to continue . . .
sample output for option 1 with input y
Welcome to Cablecom! Please make a choice from the following options:
1) Change your password
2) Cancel your service
3) Schedule a service call
4) Pay your bill
1
You are eligible for a limited time offer. You can get 10 megabits per second fo
r the low promotional rate of $50 a month. Act now while supplies last.
Please enter your choice (y for yes, n for no, q for quit)
y
Great choice. Goodbye.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.