Hello everyone, I've been using this website and it has helped me with it's hundred of solved cases, but I couldn't find one appropriate for this one.
I have created a Menu() function that looks like this:

int Menu()
{
	int i;
	printf("MENU\n\n");
	printf("1 - ...\n");
	printf("2 - ...\n");
	printf("3 - ...\n");
	printf("4 - ...\n");
	printf("5 - ...\n\n");
	printf("Choose: ");
	scanf("%d",&i);
	printf("\n\n");
	return i;
}

And I have been calling it using this on main():

void main()
{
	int op;
	op = Menu();
	switch(op)
	{
	case 1:
		funct1();
		break;
	case 2:
		funct2();
		break;
	case 3:
		funct3();
		break;
	case 4:
		funct4();
		break;
	case 5:
		funct5();
		break;
	default:
		printf("ERROR: Invalid.\n\n");
		break;
	}
}

It works great, no problems at all. I just wanted to know how to, if possible, return to the Menu() after funct1() has ended, for example.

Thank you! :)

Put Menu() inside a loop.

void main()

is a no no. Should always return an int.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Put Menu() inside a loop.

void main()

is a no no. Should always return an int.[/QUOTE]

Like

int main()
{
    while(program not ended)
    {
        menu()....
        switch....
    }
    return 0;
}

So

{
    return 0;
}

is preferable to

void main()
{
}

why?

Edited 3 Years Ago by Reverend Jim: Fixed formatting

>why?
Because when you use void main, your code is no longer guaranteed to compile or run. The C standard specifies two definitions of main, and you should use one of those unless you have a very good reason not to:

/* When expecting no command line arguments */
int main ( void )
{
  /* ... */
}

/* When expecting command line arguments */
int main ( int argc, char *argv[] )
{
  /* ... */
}

Further, there are only three predictable return values from main. Deviating from them means your code may not behave as expected on termination:

/* Return a success code */
return 0;

/* EXIT_SUCCESS defined in <stdlib.h>, return a success code */
return EXIT_SUCCESS;

/* EXIT_FAILURE defined in <stdlib.h>, return a failure code */
return EXIT_FAILURE;

The point of these rules is to make your code portable to every compiler on every machine that supports standard C. If you don't follow them, you lose that flexibility to the point where your code is only portable to the exact version of the compiler and machine you initially wrote your code on.

Well, that gives me one good reason to change my code and start using int main() from now on! :) Thank you guys!

Anyway, the main problem is resolved, and here's the solution for future viewers:

int main()
{
	int end = 0;
	while(end!=1)
	{
		int op;
		system("CLS");
		op = Menu();
		switch(op)
		{
		case 1:
			funct1();
			break;
		case 2:
			funct2();
			break;
		case 3:
			funct3();
			break;
		case 4:
			funct4();
			break;
		case 5: // This is the option to quit the program
			end = 1;
			break;
		default:
			printf("ERRO: Opcao invalida.\n\n");
			system("PAUSE");
			break;
		}
	}
	return 0;
}

Thank you all!

The use of system("CLS"); and system("PAUSE"); tight that code to operating systems that have those calls. Furthermore, system() will open a different process.

[edited]
The use of scanf("%d",&i); opens the possibility to a forever loop in Menu() if the user enters something different than an integer.

The use of system("CLS"); and system("PAUSE"); tight that code to operating systems that have those calls. Furthermore, system() will open a different process.

Yes, it is meant for Windows OS only, as it's a good workaround if portability is not an issue. I didn't get the different process problem, when i run the program it all happens in the same window.

In response to the edit: Yes, it is in fact true, and I actually did it accidentally, but It's not meant to be a too complex program. :) Or is there some simple if test function to check if the entered symbol is an int?

>Or is there some simple if test function to check if the entered symbol is an int?
scanf returns the number of successful conversions. The test itself is as simple as this:

if ( scanf ( "%d", &i ) != 1 ) {
  /* Handle the error */
}

Handling the error is harder because you need to clear out the bogus input and make sure the state of the stream is prepared for another input request. Usually this amounts to flushing the stream entirely and clearing the error state before jumping back to the input request:

for ( ; ; ) {
  /* Display options */

  if ( scanf ( "%d", &i ) == 1 )
    break;
  else {
    int ch;

    clearerr ( stdin );

    do
      ch = getchar();
    while ( ch != '\n' && ch != EOF );

    clearerr ( stdin );
  }
}

I didn't know scanf() could return values, I guess that makes the testing easier! So for that test code to work it would require a no parameter for( ; ; )?

>So for that test code to work it would require a no parameter for( ; ; )? for( ; ; ) is a needed forever loop that in essence will only stop when if ( scanf ( "%d", &i ) == 1 ) is true and break; while(1) could have been used, but there's the added process of while evaluating that 1 which is a little extra step.

That makes sense, well i understood it completely! Thank you all guys! :)

I learn more here than in classes! it's all working perfectly now!

Hi,

I guess you can also use goto function to return to the menu.

menu:

printf  "Your menu";

// upto you to use switch or scanf


// when the work is done for choosen option
// just do: goto menu; and it will return to menu

Hope it helps.

>I guess you can also use goto function to return to the menu.
I know without guessing that if you control the flow of the program you don't need a goto in this case.

goto is frown upon because it could jump anywhere making it hard to read and debug. Search for "spaghetti code", if you want to know further.

Goto is good for batch files, most programmers hate that command.

Can ANYONE find a mistake in this! I cant seem to make it work! (nevermind the french comments...)

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int Imprimer_Menu();
void Execute_Test(int operande1[], int operande2[], int reponse[], int operation, int taille);
void Imprimer_Resultat(int operande1[], int operande2[], int reponse[], int operation, int taille);
int main(){
int OPERATION, TAILLE;
TAILLE=10;
int Op1[TAILLE], Op2[TAILLE], Rep[TAILLE];

    printf(printf("Ce programme vous aidera %c pratiquer vos multiplications et vos divisions\n", 133);
do{

    OPERATION=Imprimer_Menu(); /*Appel de la fonction Imprimer_Menu*/

Execute_Test(Op1, Op2, Rep, OPERATION, TAILLE); /*Appel de la fonction Execute_Test*/

Imprimer_Resultat(Op1, Op2, Rep, OPERATION, TAILLE);  /*Appel de la fonction Imprimer_Resultat*/
    system("PAUSE");}
    while (OPERATION==1 || OPERATION==2);
    return(0);
}

int Imprimer_Menu(){
int choix;
    do {
        printf("\nMenu  1)Multiplication\n      2)Division\n      3)Sortie\n");
    printf("Veuillez choisir une option:");
    scanf("%d", choix);}
                while(choix>3 || choix<1);
                return(choix);}
void Execute_Test(int operande1[], int operande2[], int reponse[], int operation, int taille){
int i;
time_t t;
srand(time(&t));
if (operation==1){

printf("\nR%cpondez aux 10 multiplications suivantes\n", 130);
for(i=0; i<taille; i++) {
operande1[i]=1+rand()%9;
operande2[i]=1+rand()%9;

printf("\n%d X %d=", operande1[i], operande2[i]);
scanf("%d", reponse[i]);}}


else if(operation==2){
printf("\nR%cpondez aux 10 divisions suivantes\n", 130);
for(i=0; i<taille; i++){
operande1[i]=1+rand()%9;
do{
operande2[i]=1+rand()%9;}
while((operande1[i]%operande2[i])!=0);

printf("\n%d / %d=", operande1[i], operande2[i]);
scanf("%d", reponse[i]);}}


else exit(EXIT_FAILURE);
}

void Imprimer_Resultat(int operande1[], int operande2[], int reponse[], int operation, int taille){
int score, i;
score=0;
if (operation==1){
for(i=0; i<taille; i++){
                if(reponse[i]=(operande1[i]*operande2[i])){
printf("\n%d X %d = %d - r%cponse correcte", operande1[i], operande2[i], reponse[i], 130);
score++;
}
else
printf("\n%d X %d = %d - r%cponse incorrect, la bonne r%ponse est %d", operande1[i], operande2[i], reponse[i], 130, 130, operande1[i]*operande2[i]);}
}
else{
for(i=0; i<taille; i++){
                if(reponse[i]=(operande1[i]/operande2[i])){
printf("\n%d / %d = %d - r%cponse correcte", operande1[i], operande2[i], reponse[i], 130);
score++;
}
else
printf("\n%d / %d = %d - r%cponse incorrect, la bonne r%ponse est %d", operande1[i], operande2[i], reponse[i], 130, 130, operande1[i]/operande2[i]);}
if(score>7)
printf("\nF%clicitations!", 130);
else
printf("\nVeuillez demander de l'aide %c votre enseignant(e)!", 133);
printf("\nVotre score est de %d/%d", score, taille);
}
}

Edited 3 Years Ago by Dani: Formatting fixed

This question has already been answered. Start a new discussion instead.