0

Guys i'm making a ticket program in c but there's little problem. I have problem with structs and printing structs correctly. Problem is: Program print block in time option everytime.as you can see time:20.00a or something How can i fix that? There is a codes:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
void blink();
void bloksira(int a);
void pay();
struct ticketdetails
{
    char match[50];
    char tribunename[5];
    char time[5];
    char blok[1];
    int seatnumber[20];
    int ticketfee;
}ticket1;
int main()
{
    char choose[100],tribune[10],yesno[2];
    int  matchnumber;
    system("color B");
    blink();
    _getch();
    menu:printf("**********\nTICKET APP\n**********\n");
    printf("Choose your option:\n");
    printf("1-)Match\n2-)Concert\n3-)Events\n");
    scanf("%s",choose);
    if (strcmp(choose, "Match")==0 || strcmp(choose, "match")==0 || strcmp(choose, "MATCH")==0 || strcmp(choose, "1") == 0) 
    {
        printf("You chose match option. Select your match with number: \n");
        printf("1-)Fenerbahce-Galatasaray(20.00)\n2-)Besiktas-Trabzonspor(18.00)\n3-)Sivasspor-Kayserispor(13.30)\n");
        choseoption:scanf("%d",&matchnumber);
        switch (matchnumber)
        {
            case 1: printf("You chose Fenerbahce-Galatasaray match.\n");
                    strcpy(ticket1.match,"Fenerbahce-Galatasaray");
                    strcpy(ticket1.time, "20.00");
                    printf("Choose your tribune\nEast=100TL\nWest=100TL\nSouth=75TL\nNorth=75TL\n");
                    scanf("%s", tribune);
                    strcpy(ticket1.tribunename,tribune);
                    if (strcmp(tribune, "East") == 0 || strcmp(tribune, "east") == 0 || strcmp(tribune, "EAST") == 0) {
                        bloksira(100);
                    }
                    if (strcmp(tribune, "West") == 0 || strcmp(tribune, "west") == 0 || strcmp(tribune, "WEST") == 0) {
                        bloksira(100);
                    }
                    if (strcmp(tribune, "South") == 0 || strcmp(tribune, "south") == 0 || strcmp(tribune, "SOUTH") == 0) {
                        bloksira(75);
                    }
                    if (strcmp(tribune, "North") == 0 || strcmp(tribune, "north") == 0 || strcmp(tribune, "NORTH") == 0) {
                        bloksira(75);
                    }
                    printf("\nWould you like to continue? (Y/N):");
                    scanf("%s", yesno);
                    if (strcmp(yesno,"Y")==0||strcmp(yesno,"y")==0)
                        goto menu;
                    else
                        goto end;
                    break;
                        
            case 2: printf("You chose Besiktas-Trabzonspor match.\n");
                    strcpy(ticket1.match, "Besiktas-Trabzonspor");
                    strcpy(ticket1.time, "18.00");
                    printf("Choose your tribune\nEast=100TL\nWest=100TL\nSouth=75TL\nNorth=75TL\n");
                    scanf("%s", tribune);
                    strcpy(ticket1.tribunename, tribune);
                    if (strcmp(tribune, "East") == 0 || strcmp(tribune, "east") == 0 || strcmp(tribune, "EAST") == 0) {
                        bloksira(100);
                    }
                    if (strcmp(tribune, "West") == 0 || strcmp(tribune, "west") == 0 || strcmp(tribune, "WEST") == 0) {
                        bloksira(100);
                    }
                    if (strcmp(tribune, "South") == 0 || strcmp(tribune, "south") == 0 || strcmp(tribune, "SOUTH") == 0) {
                        bloksira(75);
                    }
                    if (strcmp(tribune, "North") == 0 || strcmp(tribune, "north") == 0 || strcmp(tribune, "NORTH") == 0) {
                        bloksira(75);
                    }
                    printf("\nWould you like to continue? (Y/N):");
                    scanf("%s", yesno);
                    if (strcmp(yesno, "Y") == 0 || strcmp(yesno, "y") == 0)
                        goto menu;
                    else
                        goto end;
                    break;
            case 3: printf("You chose Sivasspor-Kayserispor match.\n");
                    strcpy(ticket1.match, "Sivasspor-Kayserispor");
                    strcpy(ticket1.time, "13.30");
                    printf("Choose your tribune\nEast=100TL\nWest=100TL\nSouth=75TL\nNorth=75TL\n");
                    scanf("%s", tribune);
                    strcpy(ticket1.tribunename, tribune);
                    if (strcmp(tribune, "East") == 0 || strcmp(tribune, "east") == 0 || strcmp(tribune, "EAST") == 0) {
                        bloksira(100);
                    }
                    if (strcmp(tribune, "West") == 0 || strcmp(tribune, "west") == 0 || strcmp(tribune, "WEST") == 0) {
                        bloksira(100);
                    }
                    if (strcmp(tribune, "South") == 0 || strcmp(tribune, "south") == 0 || strcmp(tribune, "SOUTH") == 0) {
                        bloksira(75);
                    }
                    if (strcmp(tribune, "North") == 0 || strcmp(tribune, "north") == 0 || strcmp(tribune, "NORTH") == 0) {
                        bloksira(75);
                    }
                    printf("\nWould you like to continue? (Y/N):");
                    scanf("%s", yesno);
                    if (strcmp(yesno, "Y") == 0 || strcmp(yesno, "y") == 0)
                        goto menu;
                    else
                        goto end;
                    break;
            default:printf("Please select correct option:"); goto choseoption;
        }

    
    }
    end:
    return 0;
}
void bloksira(int a) {
    char blok[2];
    int number,ticketpiece,totalprice;
    printf("How many tickets do you want to buy?:");
    scanf("%d", &ticketpiece);
    printf("Choose your block:\n");
    printf("A\nB\nC\n");
    scanf("%s", blok);
    strcpy(ticket1.blok, blok);
    printf("Your block chose is %s\n", _strupr(blok));
    srand(time(0));//Bazen aynı sayıları veriyor.
    for(int i=0;i<ticketpiece;i++){
    number = 1+rand() % 50;
    ticket1.seatnumber[i] = number;
    printf("Your seat chose is %s block %d number\n", _strupr(blok), number);
}
    
    totalprice = ticketpiece * a;
    printf("Total price:%d\n", totalprice);
    ticket1.ticketfee = totalprice;
    pay();
    printf("\nTICKET DETAILS\n");
    printf("Match:%s\n", ticket1.match);
    printf("Time:%s\n", ticket1.time);
    printf("Tribune:%s\n", _strupr(ticket1.tribunename));
    printf("Block:%s\n", _strupr(ticket1.blok));
    printf("Your seats:");
    for (int i = 0; i < ticketpiece; i++) {
        printf("%s%d ",blok, ticket1.seatnumber[i]);
    }
    printf("\nTicket fee:%d", ticket1.ticketfee);
}
void pay() {
    char payoption[5];
    printf("Would you like to pay with credit card or cash? (Card/Cash): ");
    scanf("%s", payoption);
    if (strcmp(payoption, "Card") == 0 || strcmp(payoption, "card") == 0 || strcmp(payoption, "CARD") == 0) {
        char yesno[1];
        int cardnumber, validdate;
        printf("Please enter your card number : \n ");
        scanf("%d",&cardnumber);
        printf("Please enter your card valid date : \n ");
        scanf("%d",&validdate);
        printf("Payment successful...");

     }
    if (strcmp(payoption, "Cash") == 0 || strcmp(payoption, "cash") == 0 || strcmp(payoption, "CASH") == 0) {
        printf("Payment successful...");
        
    }
}
void blink() {
    int i;
    for (i = 0; i < 1; i++) {
        Sleep(500);
        printf("WELCOME");
        Sleep(500);
        system("cls");
        Sleep(500);
        printf("TO");
        Sleep(500);
        system("cls");
        Sleep(500);
        printf("TICKET");
        Sleep(500);
        system("cls");
        Sleep(500);
        printf("APP :)");
        Sleep(500);
        system("cls");
        Sleep(500);
        printf("Press any key to continue...\n");
    }
}


I expected there's no block name in time. Like Time:20.00 Time:18.00

  • `char blok[1];` can't hold a string. You need room for the string contents and the null terminator. This only has room for the null, not the contents. – Barmar Feb 03 '23 at 23:05
  • `char time[5]` should be `char time[6]`. – Barmar Feb 03 '23 at 23:07
  • Thanks for comment. It worked. I have also another problem with srand() function. Sometimes it makes same number and i don't wanna same numbers because these are seats and seats can't be same. – Halil Furkan Feb 03 '23 at 23:30
  • you should only call `srand()` once, when the program starts, not every time you call the function. – Barmar Feb 03 '23 at 23:31
  • Program is still making same numbers like (10 20 21 45 10) not (10 10 10 10 10). I tried srand in the beginning of main function but it didn't work. – Halil Furkan Feb 03 '23 at 23:39
  • I don't understand. `(10 20 21 45 10)` is different numbers, but `(10 10 10 10 10)` is the same numbers. – Barmar Feb 03 '23 at 23:41
  • We aren't going to solve the problem in comments. Post another question with a [mre] demonstrating just the problem with `srand()`. – Barmar Feb 03 '23 at 23:43
  • there are 2 10 in (10 20 21 45 10) .I don't wanna number repeat one more time. – Halil Furkan Feb 04 '23 at 00:59
  • The way to prevent repeats in a sequence of random numbers is to make an array of all the numbers, then shuffle it randomly and take them in order. – Barmar Feb 04 '23 at 01:38
  • See https://stackoverflow.com/questions/8930401/generating-list-of-random-numbers-without-duplication-using-c – Barmar Feb 04 '23 at 03:43

1 Answers1

0

I think to print the time correctly, you can modify the time variable in the ticketdetails structure to hold a string representation of the time in 24-hour format for example "20:00". Then you can use the strftime function to format the time for display.