hello. i have a problem in selecting destination and time. when i select any number in destination the program always tell me that i choose number 1 (taiping). and the time it always show 1030 even i input number that is not 1. please tell me where i'm wrong here. thank you :)

#include <stdio.h>

int main ()
{
	int type, desti, time, amount, time2;
	char* bus[99];
	char* dest[99];
	double price, tprice;
	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");
	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\n");
	if (type = 1)
		bus[99] = "Single-decker";
	else
		bus[99] = "Double-decker";
	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");
	printf (" >> ");
	scanf ("%d", &desti);
	if (desti = 1) {
		printf ("\nYou've selected --> Taiping >> RM28.30\n");
		dest[99] = "Taiping\0";
		price = 28.30; }
	else if (desti = 2) {
		printf ("\nYou've selected --> Pulau Pinang >> RM31.20\n");
		dest[99] = "Pulau%%Pinang\0";
		price = 31.20; }
	else if (desti = 3) {
		printf ("\nYou've selected --> Kuala Lumpur >> RM33.80\n");
		dest[99] = "Kuala%%Lumpur\0";
		price = 33.80; }
	else if (desti = 4) {
		printf ("\nYou've selected --> Alor Setar >> RM36.80\n");
		dest[99] = "Alor%%Setar\0";
		price = 36.80; }
	else if (desti = 5) {
		printf ("\nYou've selected --> Kuantan >> RM16.30\n");
		dest[99] = "Kuantan\0";
		price = 16.30; }
	else if (desti = 6) {
		printf ("\nYou've selected --> Seremban >> RM27.40\n");
		dest[99] = "Seremban\0";
		price = 27.40; }
	else if (desti = 7) {
		printf ("\nYou've selected --> Melaka >> RM39.50\n");
		dest[99] = "Melaka\0";
		price = 39.50; }
	else if (desti = 8) {
		printf ("\nYou've selected --> Johor Bharu >> RM32.10\n");
		dest[99] = "Johor%%Bharu\0";
		price = 32.10; }
	else
		printf ("\nPlease enter the correct number : ");
	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");
	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 ("\nPlease enter the correct number : ");

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

Recommended Answers

All 8 Replies

line 19: use == (comparison) operator instead of = (assignment) operator

The = operator assigns while the == operator tests for equality. You're using the assignment operator for equality, and C's implicit equality rules apply. Here's an example of your code:

if (type = 1)
    bus[99] = "Single-decker";
else
    bus[99] = "Double-decker";

Here's how C is really interpreting it:

type = 1;

if (type != 0)
    bus[99] = "Single-decker";
else
    bus[99] = "Double-decker";

As you can see, that comparison will always be true because you ensure it with an assignment to 1 prior to making to comparison. The else clause will never be executed.

thank you. and i still have a problem. after input all number and after the output is out, there's an error appear that tell me debug error. why is that happening?

another question.. i would like to put a seat number in this program. and i want to loop it. then i want the program to tell me that if a seat is taken (eg seat number 1) then the program will tell that the seat is occupied. is that possible?

My crystal ball says that it's due to always accessing the 99th index in arrays that only have room for 99 items (ie. the last valid index is 98).

i changed to this. still error :(

#include <stdio.h>

int main ()
{
	int type, desti, time, amount;
	char* bus[55];
	char* dest[55];
	int time2;
	double price, tprice;
	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");
	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\n");
	if (type = 1)
		bus[55] = "Single-decker\0";
	else
		bus[55] = "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");
	printf (" >> ");
	scanf ("%d", &desti);
	if (desti == 1) {
		printf ("\nYou've selected --> Taiping >> RM28.30\n");
		dest[55] = "Taiping\0";
		price = 28.30; }
	else if (desti == 2) {
		printf ("\nYou've selected --> Pulau Pinang >> RM31.20\n");
		dest[55] = "Pulau Pinang\0";
		price = 31.20; }
	else if (desti == 3) {
		printf ("\nYou've selected --> Kuala Lumpur >> RM33.80\n");
		dest[55] = "Kuala Lumpur\0";
		price = 33.80; }
	else if (desti == 4) {
		printf ("\nYou've selected --> Alor Setar >> RM36.80\n");
		dest[55] = "Alor Setar\0";
		price = 36.80; }
	else if (desti == 5) {
		printf ("\nYou've selected --> Kuantan >> RM16.30\n");
		dest[55] = "Kuantan\0";
		price = 16.30; }
	else if (desti == 6) {
		printf ("\nYou've selected --> Seremban >> RM27.40\n");
		dest[55] = "Seremban\0";
		price = 27.40; }
	else if (desti == 7) {
		printf ("\nYou've selected --> Melaka >> RM39.50\n");
		dest[55] = "Melaka\0";
		price = 39.50; }
	else if (desti == 8) {
		printf ("\nYou've selected --> Johor Bharu >> RM32.10\n");
		dest[55] = "Johor Bharu\0";
		price = 32.10; }
	else
		printf ("\nPlease enter the correct number : ");
	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");
	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 ("\nPlease enter the correct number : ");

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

You didn't fix the problem of accessing an array out of range. dest[55] is not a valid index, for example. It's also silly to declare an array and then only use one element. I think you need to study up a bit on arrays and how they work in C.

just use

char* bus
char* dest
.../*and later*/
bus="single-decker"
dest="taiping"

thank you. it works great. i'm sorry but i'm not really good in english. when i read online about array.. i understand nothing =.='' i'm sorry.

well about my question earlier.. i would like to put a seat number in this program. and i want to loop it. then i want the program to tell me that if a seat is taken (eg seat number 1) then the program will tell that the seat is occupied. is that possible?

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.