mardi 22 septembre 2015

Prompt error message if user choose 1 before 2 in a menu in C

I have a code written in C where I need the user to choose between 4 different choices in a menu. My code looks like this:

while(t!=0)
{
printf("-------------------MENU---------------------------\n");
printf("1. Randomize array.\n");
printf("2. Sort array in ascending order.\n");
printf("3. Print min- and max value.\n");
printf("4. Search for a number in the array.\n");
scanf("%d",&t);
    switch(t)
    {
        case 1:
        randomize(array);
        print(array);
        break;

        case 2:
        array[ARRAYSIZE]=bubblesort(array,ARRAYSIZE);
        break;

        case 3:
        array[ARRAYSIZE]=max(array,ARRAYSIZE);
        break;

        case 4:
        search(array,ARRAYSIZE);
        break;

        default:
        printf("Wrong input. Please try again!");
    }   
} 

I want the user to choose 1 before 2,3 and 4. I've tried using a if-statement in a while-loop like this:

while(t!=1)
{
if(!array[ARRAYSIZE])
{
 printf("You must choose 1 first!");
}
}

But even then if I choose 1 first the I still get error message 'You must choose 1 first', i guess the if-statement still reads the array[ARRAYSIZE] as empty?

I was thinking of perhaps returning some sort of boolean flag from the function randomize() which tells an if-statement in main() if choice 1 has been executed. I'm really stuck here. Do anyone have any suggestions?

Aucun commentaire:

Enregistrer un commentaire