currently, i had write a program for testing my skill purpose.. but i got hang over this two part.
# include <stdio.h>
# include <stdlib.h>
int main ()
{
int destination, departure, i = 0, j, destination1, departure1;
int ticket;
float charges = 0, charges1, charges2, payment, payment1 = 0, remain = 0, change = 0, destination_km, departure_km;
printf("[1] Bank Negara [7] Serdang ");
printf("\n[2] Kuala Lumpur [8] Kajang ");
printf("\n[3] KL Sentral [9] Bangi ");
printf("\n[4] Mid Valley [10] Nilai ");
printf("\n[5] Salak Selatan [11] Seremban ");
printf("\n[6] Bandar Tun Razak ");
printf("\n\nPlease Choose ");
do
{
i++;
printf("\nDestination : ");
scanf("%d",&destination);
printf("\n----------------------------------------\n");
printf("\nPlease collect your ticket\n");
for (j = 0; j < i; j++)
{
destination1 = destination;
departure1 = departure;
charges2 = charges1;
switch (departure1)
{
case 1:
printf("\nBank Negara ->");
break;
case 2:
printf("\nKuala Lumpur ->");
break;
case 3:
printf("\nKL Sentral ->");
break;
case 4:
printf("\nMid Valley ->");
break;
case 5:
printf("\nSalak Selatan ->");
break;
case 6:
printf("\nBandar Tun Razak ->");
break;
case 7:
printf("\nSerdang ->");
break;
case 8:
printf("\nKajang ->");
break;
case 9:
printf("\nBangi ->");
break;
case 10:
printf("\nNilai ->");
break;
case 11:
printf("\nSeremban ->");
break;
}
switch (destination1)
{
case 1:
printf("Bank Negara ");
break;
case 2:
printf("Kuala Lumpur ");
break;
case 3:
printf("KL Sentral ");
break;
case 4:
printf("Mid Valley ");
break;
case 5:
printf("Salak Selatan ");
break;
case 6:
printf("Bandar Tun Razak ");
break;
case 7:
printf("Serdang ");
break;
case 8:
printf("Kajang ");
break;
case 9:
printf("Bangi ");
break;
case 10:
printf("Nilai ");
break;
case 11:
printf("Seremban ");
break;
}
from the sourcecode above.. at the (add ticket) part, it supposed to be asking user to enter character Y/y or N/n. but i couldn't make it.. as user enter N/n, it still looping.. besides, the below part.. the formatted output is wish to show the ticket detail.. as like dis..
[1] Bank Negara [8] Nilai
[2] Bandar Tun Razak [9] Salak Selantan
[3] Bangi ...
[4] Serdang ...
...
.... . . .
Please Choose
Your destination : 4
Your Departure : 2
Your charges : RM 1.15
Add Ticket : Y
Your destination : 4
Your Departure : 8
Your charges : RM 3.40
Add Ticket : N
Please enter RM 3.40 : 2.00
Please enter the remainder 1.40 : 2.00
Please collect your ticket ->
Bandar Tun Razak -> Serdang RM1.15
Nilai -> Serdang RM2.25
You remainder -> RM0.60
i had think hard.. and i have no idea on how to make the formatted output for the last part.. and also the character to add ticket. can anyone help to edit the code? and also give me some advice on why its not functioning.
while(ticket != 0) ?
After input ticket will be Y y N n or some rubbish.
I don't know when do you want to stop looping, but I suppose when input is N or n
So write while(tolower(ticket) != 'n')
Why is ticket an integer? It should be char. I'm not familiar with stdio. Are you reading an int or char for input?
thx.. for the input reading for ticket i had found out the solution.. now the problems left is only the below part... how am i going to do the looping to show that what the destination and departure of the user entered if there are many.. like this part..
Please collect your ticket ->
Bandar Tun Razak -> Serdang RM1.15
Nilai -> Serdang RM2.25
You remainder -> RM0.60
i know that right after the "please collect your ticket ->" is a loop.. but i not sure how to gain the value from the above loop so that the program can show this part during the end.. all i done is the row is correct.. but the content is same.. like this..
Please collect your ticket ->
Bandar Tun Razak -> Serdang RM1.15
Bandar Tun Razak -> Serdang RM1.15
You remainder -> RM0.60
can anyone tell me what the parts or how to make it shown like the first part..
/*this program created by:
Group Name :
Group Members :
*/
//header declarations
# include <stdio.h>
# include <stdlib.h>
# include <ctype.h>
//main function
int main ()
{
//local declarations
int destination, departure, i = 0, j, destination1, departure1;
char b, a;
bool ticket;//boolean is used due to the answer is only yes or no.
char str [80];//string is used for the data validation
float charges = 0, charges1, charges2, payment, payment1 = 0, remain = 0, change = 0, destination_km, departure_km;
//statements
printf("[1] Bank Negara [7] Serdang ");
printf("\n[2] Kuala Lumpur [8] Kajang ");
printf("\n[3] KL Sentral [9] Bangi ");
printf("\n[4] Mid Valley [10] Nilai ");
printf("\n[5] Salak Selatan [11] Seremban ");
printf("\n[6] Bandar Tun Razak ");
printf("\n\nPlease Choose\n ");
do//do-while loops, perform when users want to add tickets
{
i++;
printf("\nDestination : ");
fflush(stdin);//flush stream
fgets(str,80,stdin);//gets the formatted string
destination = atoi (str);//convert alphabet to integer
while (destination > 11)//while loop for data validation if user entered an error
{
printf("Invalid Options. Please choose again.");
printf("\nDestination : ");
fflush(stdin);//flush stream
fgets(str,80,stdin);//gets the formatted string
destination = atoi (str);//convert alphabet to integer
}
//switch case for destination distance
switch (destination)
{
case 1:
destination_km = 0;
break;
case 2:
destination_km = 1;
break;
case 3:
destination_km = 1.5;
break;
case 4:
destination_km = 2.3;
break;
case 5:
destination_km = 7.8;
break;
case 6:
destination_km = 13.2;
break;
case 7:
destination_km = 15.5;
break;
case 8:
destination_km = 16.1;
break;
case 9:
destination_km = 18;
break;
case 10:
destination_km = 20;
break;
case 11:
destination_km = 23.2;
break;
}
printf("Departure : ");
fflush(stdin);//flush stream
fgets (str,80,stdin);//gets the formatted string
departure = atoi (str);//convert alphabet to integer
while (departure > 11)//while loop for data validation if user entered an error
{
printf("Invalid Option. Please choose again.");
printf("\nDeparture : ");
fflush(stdin);//flush stream
fgets (str,80,stdin);//gets the formatted string
departure = atoi (str);//convert alphabet to integer
}
//switch case for departure distance
switch (departure)
{
case 1:
departure_km = 0;
break;
case 2:
departure_km = 1;
break;
case 3:
departure_km = 1.5;
break;
case 4:
departure_km = 2.3;
break;
case 5:
departure_km = 7.8;
break;
case 6:
departure_km = 13.2;
break;
case 7:
departure_km = 15.5;
break;
case 8:
departure_km = 16.1;
break;
case 9:
departure_km = 18;
break;
case 10:
departure_km = 20;
break;
case 11:
departure_km = 23.2;
break;
}
printf("\nAdd tickets (Y/N): ");
fflush(stdin);//flush stream
a = getchar();//get character from stdin
b = toupper (a);//convert lowercase to uppercase
switch (b)
{
case 'Y':
ticket = true;//if ticket value is Y, means the loop will continue
break;
case 'N':
ticket = false;//if ticket value is N, the loop will terminate
break;
default:
printf("\nAdd tickets (Y/N): ");
fflush(stdin);//flush stream
a = getchar();//get character from stdin
b = toupper (a);//convert lowercase to uppercase
break;
}
} while ((ticket != false));//termination of loop when ticket == false is detected
printf("\n\nPlease enter RM %3.2f: ",charges);
fflush(stdin);//flush stream
fgets (str,80,stdin);//gets the formatted string
payment = atoi (str);//convert alphabet to integer
while (payment < charges)//while loop used if user enter payment less than charges
{
remain = charges - payment;//if user paid lesser than charges, user still require to pay remaining values.
printf("Please enter the remainder RM %3.2f: ", remain);
fflush(stdin);//flush stream
fgets (str,80,stdin);//gets the formatted string
payment1 = atoi (str);//convert alphabet to integer
payment+=payment1;//adjustment after user had paid.
}
if (payment1 > remain )//if user pay excess, there will be some change for the user.
{
change = payment1 - remain;
}
else
{
change = payment - charges;
}
printf("\n----------------------------------------\n");
printf("\nPlease collect your ticket ->\n");//to inform the user to take the tickets.
for (j = 0; j < i; j++)
{
destination1 = destination;
departure1 = departure;
charges2 = charges1;
switch (departure1)
{
case 1:
printf("\nBank Negara ->");
break;
case 2:
printf("\nKuala Lumpur ->");
break;
case 3:
printf("\nKL Sentral ->");
break;
case 4:
printf("\nMid Valley ->");
break;
case 5:
printf("\nSalak Selatan ->");
break;
case 6:
printf("\nBandar Tun Razak ->");
break;
case 7:
printf("\nSerdang ->");
break;
case 8:
printf("\nKajang ->");
break;
case 9:
printf("\nBangi ->");
break;
case 10:
printf("\nNilai ->");
break;
case 11:
printf("\nSeremban ->");
break;
}
switch (destination1)
{
case 1:
printf(" Bank Negara ");
break;
case 2:
printf(" Kuala Lumpur ");
break;
case 3:
printf(" KL Sentral ");
break;
case 4:
printf(" Mid Valley ");
break;
case 5:
printf(" Salak Selatan ");
break;
case 6:
printf(" Bandar Tun Razak ");
break;
case 7:
printf(" Serdang ");
break;
case 8:
printf(" Kajang ");
break;
case 9:
printf(" Bangi ");
break;
case 10:
printf(" Nilai ");
break;
case 11:
printf(" Seremban ");
break;
}