Jump to content

C Programing, While loop not working.

Go to solution Solved by danejesse1,

In your switch you don't have a break statement. Also, scanf("%d", rerun) needs a &rerun since rerun is an int, not a pointer to an int.

Edit: it looks like you're missing a break statement in your first switch, too.

switch(rerun) {
	case 1:
		k=0;
		break; // Missing this break statement	
	case 2:
		k=1;
		system("pause");  
}

Hi,

 

Thanks for helping me out, if you're wondering why I'm not posting on stack overflow, it's because I have a cool down for some reason.

 

Here's my code:

Spoiler

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

void BookingSystem()
{

	int nineNaboo=0;
	int nineTattoine=0;
	int sevenEndor=0;
	int nineEndor=0;
	int sevenNaboo=0;
	int sevenTatooine=0;
	int ninej=0;
	int seventable=0;
	int sevenj=0;
	int ninetable=0;
	int k=0;
	int time=0;
	int rerun;


	while (k==0)
{
		printf("\nWelcome to the booking System\nMeals are served at 9pm and 7pm, please enter what time you would like to book for.\n\n");
    	scanf("%d", &time);
			
	if (time==7)
	{
       	printf("\nYou have selected to book your slot at 7PM\n");
        k=7;
	}
	else if (time==9)
	{
		printf("\nYou have selected to book your slot at 9PM\n");
		
	while (ninej==0)
{
	
		printf("Please enter what table you would like to book\nPlease enter: \n\n'1' for Endor \t'2' for 'Naboo'\t'3' for Tattoine");
		scanf("%d", &ninetable);
		switch(ninetable)
{
				case 1:
						if (nineEndor==1)
						{
							printf("Sorry, this table has already been booked, please selected another table\n");
							ninej==0;
					  	}

						if (nineEndor==0)
						{
						printf("You have selected to book your table at Endor, your table has been booked for 9PM");
						nineEndor = 1;
						ninej=1;
	  					}

				case 2:
						if (nineNaboo==1)
						{
							printf("Sorry, this table has already been booked, please selected another table\n");
							ninej=0;
						}
						if (nineNaboo==0)
						{
						printf("You have selected to book your table at Naboo, your table has been booked for 9PM");
						nineNaboo = 1;
						ninej=1;
						break;
						}

				case 3:
						if (nineTattoine==1)
						{
							printf("Sorry, this table has already been booked, please selected another table\n");
							ninej=0;
						}
						if (nineTattoine==0)
						{
						printf("You have selected to book your table at Tattoine, your table has now been booked for 9PM");
						nineTattoine = 1;
						ninej=1;
						}
}
}
}




	else
{
		printf("\nYou have selected an incorrect time, please try again.\n\n");
		k=0;
}

	printf("\n\nIf you would like to book another table, please say select '1' for Yes, and '2' for No.");
	scanf("%d", rerun);
	switch(rerun)
{

		case 1:
				k=0;
				
		case 2:
				k=1;
				system("pause");  
}
}
}
int main()
{
	BookingSystem();
}

 

 

Okay, as you can probably see. I'm trying to get my program to loop back to the beginning whenever K is turned into something other than 0. Any loops I try result in it either not working or a crash. I think I'm missing something really essential from a while loop, and I can't seem to think what it is.

 

Thanks for helping me out, and sorry if I explained this poorly.

Link to comment
Share on other sites

Link to post
Share on other sites

In your switch you don't have a break statement. Also, scanf("%d", rerun) needs a &rerun since rerun is an int, not a pointer to an int.

Edit: it looks like you're missing a break statement in your first switch, too.

switch(rerun) {
	case 1:
		k=0;
		break; // Missing this break statement	
	case 2:
		k=1;
		system("pause");  
}
Edited by danejesse1
Link to comment
Share on other sites

Link to post
Share on other sites

looks like break statement is missing.

Intel Core i3 2100 @ 3.10GHz - Intel Stock Cooler - Zotac Geforce GT 610 2GB Synergy Edition

Intel DH61WW - Corsair® Value Select 4GBx1 DDR3 1600 MHz - Antec BP-300P PSU

WD Green 1TB - Seagate 2.5" HDD 1TB - Seagate Barracuda 500GB - Antec X1 E.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, danejesse1 said:

In your switch you don't have a break statement. Also, scanf("%d", rerun) needs a &rerun since rerun is an int, not a pointer to an int.

 


switch(rerun) {
	case 1:
		k=0;
		break; // Missing this break statement	
	case 2:
		k=1;
		system("pause");  
}

Thank you!
 

I was literally searching my code for at least 30 mins trying to figure it out. I can't believe I couldn't see that stupid mistake!!

 

Thanks a lot, you've saved my sanity xD

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×