Hi, do you know why my output does not working properly..
Actually my output should looping,but it straightly ending the program..

#include<conio.h>
#include<stdio.h>

int main ()
{
 int choice = 0 ;


    while ((choice>=1)&&(choice<=5))

{
        printf ("\nPlease Choose No 1 , 2 , 3 , 4 , 5\n");
        printf ("Enter your choice");
        scanf ("%d", &choice);
}
    switch (choice)
    {
        case 1 :
        printf ("RM32");
        break ;

        case 2 :
        printf ("RM23");

        case 3 :
        printf ("RM43");
        break ;

        case 4 :
        printf ("RM44");
        break;

        default :
        printf ("Program will exit now..");


    }
    getch ();
    return 0 ;

}

Recommended Answers

All 4 Replies

Hi,
I am unable to actually understand the desired output that you wish to receive from the above program. I would just like to point it out to you that your while loop doesn't enclose the switch statement as well.

Thereby your code would just take input and print nothing . Until you enter a choice value above 5 or below 1. Which will execute the default: label and exit the program.

Initially choice = 0, and in the first iteration itself while condition is false. Now here you can use a do while. Also put the switch case inside the while loop as pointed by Sky Diploma

One more thing, in the default case put an exit() statement so that the user cannot enter more choices. Usually , what you are supposed to do is something like this:
Add 1 more case as follows and use default if a user enters a wrong value

case 5 :
printf ("Exitting");
exit(0);
 
default :
printf ("Invalid choice : Enter a valid choice");

Now your program becomes a little more interactive than before. :)

Thanks you for your explanation and suggestion..:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.