Using Do-While loop statement for a selection Menu

Joined
May 1, 2023
Messages
7
Reaction score
0
Hello All,
am stuck using a recursive menu selection. It keeps iterating and do not know what I am missing from my code. Can anybody help.
I have tried putting the function inside the switch statements and it also is not getting me the results.

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <ctype.h>

void getTriangle();
void getRectangle();
void getSquare();
char displayMenu();

int main(void)
{
char input;

//display program
input = displayMenu();

//display shape + decision

do {
switch (input)
{
case 'A':
getSquare();
break;
case 'B':
getTriangle();
break;
case 'C':
getRectangle();
break;
case 'D':

default:
printf("Terminate Program!\n");
break;

}
} while (input != 'D');



return (0);

}
char displayMenu()
{
char userInput;
printf("A. Square\n");
printf("B. Triangle\n");
printf("C. Rectangle\n");
printf("D. Exit\n");
printf("Enter your selection: ");
scanf("%c", &userInput);

return toupper(userInput);
}

void getTriangle()
{
printf(" /\\ \n");
printf(" / \\ \n");
printf(" / \\ \n");
}

void getRectangle()
{
printf(" ************\n");
printf(" * *\n");
printf(" * *\n");
printf(" ************\n\n");
}

void getSquare()
{
printf(" *********\n");
printf(" * *\n");
printf(" * *\n");
printf(" *********\n\n");
}
 
Joined
Sep 4, 2022
Messages
128
Reaction score
16
C++:
do{
 switch(inputMenu()){



 }
}

your error is around the input 'char' var, you have to call you decision fork [ inputMenu() ] every loop to achieve.
 
Joined
May 1, 2023
Messages
7
Reaction score
0
Thank you FResher for the tip!

I got it to run better; however, i still cannot terminate the program and the selection menu doubles.

int main(void)
{

//display program
char displayMenu();

//display shape + decision

do {

switch (displayMenu())
{
case 'A':
getSquare();
break;
case 'B':
getTriangle();
break;
case 'C':
getRectangle();
break;
case 'D':

default:
printf("Terminate Program!\n");
break;

}
} while (displayMenu() != 'D');
 
Joined
Sep 4, 2022
Messages
128
Reaction score
16
the "do...while" statement makes the evaluation of a value after one first loop, why don't you go through a simple "while", it's easy :

C:
while (get_answer() != 'D'){

clean_screen() ; // reset the display
build_display() ; // prepare and return the display to come
get_answer() ; // wait for the answer
launch_action() ; // execute the request answer, or quit.

}

/* it's one I made few years ago, I use global vars to load each parts at every functions, it's a common loop for "console mode" */
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top