C中的结构有问题,我该如何修复它?[duplicate]

bzzcjhmw  于 2023-02-07  发布在  其他
关注(0)|答案(1)|浏览(100)
    • 此问题在此处已有答案**:

How should character arrays be used as strings?(4个答案)
3天前关闭。
伙计们,我正在用c语言做一个票务程序,但是有一个小问题。我在结构体和正确打印结构体方面有问题。问题是:程序打印块在时间选项每次。as you can see time:20.00a or something我怎么能解决这个问题?有一个代码:

#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");
    }
}

我以为没有时间块名称。例如时间:20.00时间:18.00

wfsdck30

wfsdck301#

我认为要正确地打印时间,可以修改TicketDetails结构中的time变量,以24小时格式保存时间的字符串表示,例如“20:00”,然后可以使用strftime函数格式化时间以供显示。

相关问题