hi. this is my program for bus ticket detail. at the bottom of my program, i want user to input yes or no for loop this program. but before i can input anything, it already told me that 'Please input the correct answer'. it is like i already input something. how to overcome this?

and 1 more question, is it possible to put seat number in this program so when the program loop and same seat number is chose i want to the program to tell that the seat is occupied. thank you very much :)

#include <stdio.h>

int main ()
{
	int type, desti, time, amount;
	char* bus;
	char* dest;
	char yn;
	int time2;
	double price, tprice;
	do { 
	printf ("------------------------------\n");
	printf ("| Speed of Light Bus Company |\n");
	printf ("------------------------------\n\n");
	printf ("------------------------------------------------| \n");
	printf (" |Please select your preferred type of bus   | \n");
	printf (" |1. Single-decker\t\t\t     |\n");
	printf (" |2. Double-decker\t\t\t     |\n");
	printf ("------------------------------------------------| \n\n");
	do { printf (" >> ");
	scanf ("%d", &type);
	if (type == 1)
		printf ("\n >> You've selected single-decker type\n\n");
	else if (type == 2)
		printf ("\n >> You've selected double-decker type\n\n");
	else
		printf ("\n - Please enter the correct number");
	} while (type > 2);
	if (type == 1)
		bus = "Single-decker\0";
	else
		bus = "Double-decker\0";
	printf ("-------------------------------------| \n");
	printf (" |Please choose your destination| \n");
	printf (" |1. Taiping >> RM28.30\t\t|\n");
	printf (" |2. Pulau Pinang >> RM31.20\t|\n");
	printf (" |3. Kuala Lumpur >> RM33.80\t|\n");
	printf (" |4. Alor Setar >> RM36.80\t|\n");
	printf (" |5. Kuantan >> RM16.30\t\t|\n");
	printf (" |6. Seremban >> RM27.40\t|\n");
	printf (" |7. Melaka >> RM39.50\t\t|\n");
	printf (" |8. Johor Bharu >> RM32.10\t|\n");
	printf ("-------------------------------------| \n\n");
	do { printf (" >> ");
	scanf ("%d", &desti);
	if (desti == 1) {
		printf ("\nYou've selected --> Taiping >> RM28.30\n");
		dest = "Taiping\0";
		price = 28.30; }
	else if (desti == 2) {
		printf ("\nYou've selected --> Pulau Pinang >> RM31.20\n");
		dest = "Pulau Pinang\0";
		price = 31.20; }
	else if (desti == 3) {
		printf ("\nYou've selected --> Kuala Lumpur >> RM33.80\n");
		dest = "Kuala Lumpur\0";
		price = 33.80; }
	else if (desti == 4) {
		printf ("\nYou've selected --> Alor Setar >> RM36.80\n");
		dest = "Alor Setar\0";
		price = 36.80; }
	else if (desti == 5) {
		printf ("\nYou've selected --> Kuantan >> RM16.30\n");
		dest = "Kuantan\0";
		price = 16.30; }
	else if (desti == 6) {
		printf ("\nYou've selected --> Seremban >> RM27.40\n");
		dest = "Seremban\0";
		price = 27.40; }
	else if (desti == 7) {
		printf ("\nYou've selected --> Melaka >> RM39.50\n");
		dest = "Melaka\0";
		price = 39.50; }
	else if (desti == 8) {
		printf ("\nYou've selected --> Johor Bharu >> RM32.10\n");
		dest = "Johor Bharu\0";
		price = 32.10; }
	else
		printf ("\n - Please enter the correct number");
	} while ( desti > 8);
	printf ("\nPlease enter how many ticket you want >> ");
	scanf ("%d", &amount);

	tprice = price * amount;

	printf ("--------------------------------------| \n");
	printf (" |Please choose what time you want | \n");
	printf (" |1. 1030\t\t\t   |\n");
	printf (" |2. 1430\t\t\t   |\n");
	printf (" |3. 1800\t\t\t   |\n");
	printf (" |4. 2230\t\t\t   |\n");
	printf ("--------------------------------------| \n\n");
	do { printf (" >> ");
	scanf ("%d", &time);
	if (time == 1)
		time2 = 1030;
	else if (time == 2)
		time2 = 1430;
	else if (time == 3)
		time2 = 1800;
	else if (time == 4)
		time2 = 2230;
	else
		printf ("\n - Please enter the correct number");
	} while ( time > 4);

	printf ("\nThis is your ticket information : \n");
	printf ("Bus type : %s\n", bus);
	printf ("Destination : %s\n", dest);
	printf ("Total price = %0.2f \n", tprice);
	printf ("Time : %d\n", time2);

	printf ("\nDo you want to print another ticket? (Y/N)\n");
	do { printf (" >> ");
	scanf ("%c", &yn);
	
	if ( yn == 'Y' || yn == 'y' || yn == 'N' || yn == 'n' ) {
		break;
	}
	else {
		printf ("\nPlease enter the correct answer"); }

	} while (yn != 'Y' || yn != 'y' || yn != 'N' || yn != 'n');
	} while (yn == 'Y' || yn == 'y');

	
}

Recommended Answers

All 4 Replies

Make a truth table for the logical tests ...

} while (yn != 'Y' || yn != 'y' || yn != 'N' || yn != 'n');

if yn is equal to 'y' you wan't the loop to finish, except 'y' is not 'Y', 'N' or 'n' so that statement equates to true and the loop continues.

} while ((yn != 'Y') && (yn != 'y') && (yn != 'N') && (yn != 'n'));

That is one fixed, your turn to fix the rest ;) ... brackets for clarity.

If you enter the number 12, how many keys do you actually hit? 1, 2, 3? If you actually read the number (12) what's left in the input buffer to be read at the next input?

no. it is not the expression is wrong. but the program enter the input itself. blank input. and it direct tell to input the correct answer before i get to input anything. u know what i mean rite?

i try reading about the buffer thing, but can't understand it. can u explain it to me in simple english.. thank you :)

and about my 2nd question about the seat. anyone can answer that??

no. it is not the expression is wrong. but the program enter the input itself. blank input. and it direct tell to input the correct answer before i get to input anything.

This is a very common problem.I hope this will make things clear. If not, try to Google it yourself.

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.