0

Could someone help me with my code? My program isn't terminating. After choosing a brand, it should terminate but it loops forever.

#include<stdio.h>
int main (void)
{
    int number,count=0 ;

    while (count<3)
    {
        printf("Menu:\n");
        printf("1.Proace\n");
        printf("2.Yonex\n");
        printf("3.Reebook\n");
        printf("0.Exit\n");
        printf("Enter your selection:");
        scanf("%d",&number);

        switch (number)
        {
        case 0:
            printf("exit.\n");
            break;
        case 1:
            printf("You have selected proace.\n");
            break;
        case 2:
            printf("You have selected yonex.\n");
            break;
        case 3:
            printf("You have selected reebook.\n");
            break;

        default:
            printf("Please try again.\n");
            exit(0);
        }
    }
    return 0;
}
Arnaud
  • 7,259
  • 10
  • 50
  • 71

8 Answers8

1

I guess you are misunderstanding the default case of switch: it is executed only if number is not 0,1,2,3. If you want to exit each time a exit(0) for each case 0 1 2 3

#include<stdio.h>
int main (void)
{
    int number,count=0 ;

    while (count<3)
    {
        printf("Menu:\n");
        printf("1.Proace\n");
        printf("2.Yonex\n");
        printf("3.Reebook\n");
        printf("0.Exit\n");
        printf("Enter your selection:");
        scanf("%d",&number);

        switch (number)
        {
        case 0:
            printf("exit.\n");
            exit(0);
            break;
        case 1:
            printf("You have selected proace.\n");
            exit(0);
            break;
        case 2:
            printf("You have selected yonex.\n");
            exit(0);
            break;
        case 3:
            printf("You have selected reebook.\n");
            exit(0);
            break;

        default:
            printf("Please try again.\n");
        }
    }
    return 0;
}

BTW a better code should be

#include<stdio.h>
int main (void)
{
    int number;
    bool canExit = false;

    while (canExit == false)
    {
       canExit  = true;

        printf("Menu:\n");
        printf("1.Proace\n");
        printf("2.Yonex\n");
        printf("3.Reebook\n");
        printf("0.Exit\n");
        printf("Enter your selection:");
        scanf("%d",&number);

        switch (number)
        {
        case 0:
            printf("exit.\n");
            break;
        case 1:
            printf("You have selected proace.\n");
            break;
        case 2:
            printf("You have selected yonex.\n");
            break;
        case 3:
            printf("You have selected reebook.\n");
            break;

        default:
            printf("Please try again.\n");
            canExit = false;
        }
    }
    return 0;
}
LPs
  • 16,045
  • 8
  • 30
  • 61
0

Replace while (count<3) with a boolean, bool loop=true; followed by while (loop). And then inside the switch when you wish to exit, don't call the exit function but simply set loop=false;.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

Your break in the switch() exits the switch() but does not exit the while().

If you enter 0, default: case won't be executed. Is it really what you wanted ?

SR_
  • 874
  • 13
  • 31
  • i want to exit if i type either 1,2,3,0 number other than that number it will repeat ask again. – Qili Xiang Sep 16 '15 at 07:44
  • Your code exits only if you type something different from { 1; 2 ; 3 ; 0 } (I don't take into account problems with `scanf()`). In addition to other answers that show you another way of writing what you want, I insist on the fact that you should have another look at way the `break`statement works. http://stackoverflow.com/questions/2565659/ is a good start. – SR_ Sep 17 '15 at 06:14
0
int number=4 ;

while (number > 3)

and you can delete count, which I can't see is being used anywhere

Actually, the following structure might be better, since you won't have to put an arbitrary number in the comparison:

int number;
do {
  // the thing
} while(number > 3);

In this case, it determines whether to continue the loop AFTER the first execution rather than before it. So in a do-while it will ALWAYS execute the do ONCE before the condition can break it, even if the condition is false to begin with!

Tor Valamo
  • 33,261
  • 11
  • 73
  • 81
0

I would replace your switch with:

char* brand[] = { "proace", "yonex", "reebook" };
do{
    if(number == 0)
        printf("exit.\n");
    else if(number <= 3)
        printf("You have selected %s.\n", brand[number]);
    else
        printf("Please try again");
}while(number > 3);

Encapsulate it in do-while loop with condition number > 3 and it will loop until user select 0-3. In this case, exit(0) is unnecessary.

Quest
  • 2,764
  • 1
  • 22
  • 44
0

You are not updating the count variable that is why it is not exiting. For exiting the while loop the count should be greater than or equal to 3.

0

As long as you will be giving the input 0, 1, 2 or 3 you will never reach default which has exit() and thus the program will continue to run as we have set while conditional as (count < 3) i.e. 0 < 3 which is always true. So no chance of exiting the program by while conditional. The only chance to terminate the program is from default.

So if you give input anything other than 0, 1, 2 or 3, you will reach default which will call exit() and thus will cause the program to terminate.

rootkea
  • 1,474
  • 2
  • 12
  • 32
-3

Hello you can exit your program by including the header file #include in your header file section.

Then you can use the exit() command

Eg :-

#inlcude<process.h>
#include<studio.h>
into opt;
printf(" enter o ");
scant(" %d",&opt);
switch(opt)
case 0: printf(" exiting the program");
             getch();
             exit();
Jaya Krishna
  • 313
  • 4
  • 14
  • In `gcc`, `exit()` is a built-in function so you don't have to include any header file but then `gcc` will throw a warning about the type declaration of `exit()`. So to make the code compile under any possible C standard compliant compiler, you should include `stdlib.h` which has the declaration of `exit()` – rootkea Oct 03 '15 at 05:32
  • BTW what's this `studio.g`? Did you mean `stdio`? – rootkea Oct 03 '15 at 05:33
  • @rootkea its a typo, I mean its auto correction problem. Yeah should be included to use exit(). – Jaya Krishna Oct 03 '15 at 05:50
  • Why `process.h`? No need of that. – rootkea Oct 03 '15 at 05:53