So I just started programming in C and so far so good.
I wanted to make a sample menu program that uses switch and loops for the logic.
Now the problem:
When I want to go back to the menu program I'll need to input a 1 for yes or a 0 for no.
After that input the program stops and I don't know why.
The code:
#include<stdio.h>
#include <stdlib.h>
// start main
int main() {
int option, response;
int optOne(), optTwo(), optThree();
do {
printf("Menu:\nPress 1 for Option One.\nPress 2 for Option Two.\nPress 3 for Option Three.\n");
scanf("%d", &option);
switch(option)
{
case 1:
system("cls");
optOne();
/*!!!-------------After this input my program stops working--------------------!!!*/
printf("Do you want to go back to main menu?[1 for YES/0 for NO]\n");
scanf("%d", &response);
system("cls");
break;
case 2:
system("cls");
optTwo();
/*!!!-------------After this input my program stops working--------------------!!!*/
printf("Do you want to go back to main menu?[1 for YES/0 for NO]\n");
scanf("%d", &response);
system("cls");
break;
case 3:
system("cls");
optThree();
/*!!!-------------After this input my program stops working--------------------!!!*/
printf("Do you want to go back to main menu?[1 for YES/0 for NO]\n");
scanf("%d", &response);
system("cls");
break;
}
} while(response == 1);
} // end of main
void optOne() {
char response;
int counter = 1;
// start of a while loop
while(counter != 0) {
printf("This is Option One.\n");
printf("Do you want to try again:[Y/N]\n");
scanf("%s", &response);
if(response == 'y' || response == 'Y')
{
continue;
}
else if(response == 'n' || response == 'N')
{
system("cls");
counter = 0;
}
else
{
printf("Invalid input\n");
}
}
} // end of optOne function
void optTwo() {
char response;
int counter = 1;
// start of a while loop
while(counter != 0) {
printf("This is Option Two.\n");
printf("Do you want to try again:[Y/N]\n");
scanf("%s", &response);
if(response == 'y' || response == 'Y')
{
continue;
}
else if(response == 'n' || response == 'N')
{
system("cls");
counter = 0;
}
else
{
printf("Invalid input\n");
}
}
} // end of optTwo function
void optThree() {
char response;
int counter = 1;
// start of a while loop
while(counter != 0) {
printf("This is Option Three.\n");
printf("Do you want to try again:[Y/N]\n");
scanf("%s", &response);
if(response == 'y' || response == 'Y')
{
continue;
}
else if(response == 'n' || response == 'N')
{
system("cls");
counter = 0;
}
else
{
printf("Invalid input\n");
}
}
} // end of optThree function
Thanks for the time reading it :]

New Topic/Question
Reply




MultiQuote





|