You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言影院票务代码输出异常求助:指针使用问题排查

UniMAP CINEMA票务系统指针问题排查与修复

我开发的UniMAP CINEMA票务系统C语言代码无法正确打印电影名、座位号、场次时间及折扣价格,调整指针的*和&符号后,输出仍为乱码或(null)等异常值。以下是我的代码及两种错误输出,请求帮忙排查指针相关问题,实现正常输出需求:

#include <stdio.h>
float getUserDetail(char*, float*);
float getDiscount(char, float, float, float*);
float getDetails(char*, float*, float*);
float FPrint(char*, float*, float*, float*);

int main()
{
    char name, CH, Std, movie;
    float age, RM, Disc, seat, Time;
    printf("\t\t\t\tWELCOME TO UniMAP CINEMA!!!");
    do      
    {
        getUserDetail(&name, &age);
        getDiscount(Std, age, RM, &Disc);
        getDetails(&movie, &seat, &Time);
        printf("\nDo you like to continue? If yes, press 'Y':");
        scanf("%s",&CH);
    }
    while(CH=='Y'||CH=='y');
    FPrint(&movie, &seat, &Time, &Disc);
    printf("\n\t\t\tThank you");
    return 0;
}
float getUserDetail(char*name, float*age)
{
    printf("\nName: ");
    scanf("%s", name);
    printf("Age: ");
    scanf("%f",age);
    return (*age);
}
float getDiscount(char Std,float age, float RM, float*Disc)
{
    RM == 15;
    if (age < 13){
        *Disc == ((30/100)*RM); 
        return (*Disc); }
    else if (age < 19){
        printf("Are you a student?(Y/N):");
        scanf("%s", &Std);
            {
            if(Std == 'Y'|| Std == 'y'){
                *Disc == ((50/100)*RM);
                return (*Disc); }
            else
                *Disc == RM;
                return (*Disc); }}
    else
        *Disc == RM;
        return (*Disc);
}
float getDetails(char*movie, float*seat, float*Time)
{
    printf("Name of the movie: ");
    scanf("%s", &movie);
    printf("Number of the seat: ");
    scanf("%f", &seat); 
    printf("Time of the movie: ");
    scanf("%f", &Time);
}
float FPrint(char*movie, float*seat, float*Time, float*Disc)
{
    printf("\n\t\t\tBooking Successful");
    printf("\n\t\tMovie Name:%s", &movie);
    printf("\n\t\tSeat Number:%f", &seat);
    printf("\n\t\tTime:%f", &Time);
    printf("\n\t\tDiscounted Price:RM%s", &Disc);
}

原输出:

Name: kaus
Age: 21
Name of the movie: qwerty
Number of the seat: 21
Time of the movie: 2.15

Do you like to continue? If yes, press 'Y':n

                        Booking Successful
                Movie Name:§■e
                Seat Number:0.000000
                Time:0.000000
                Discounted Price:RM
■e
                        Thank you
--------------------------------
Process exited after 14.19 seconds with return value 0
Press any key to continue . . .

调整指针符号后的输出:

Name: kaus
Age: 21
Name of the movie: qwerty
Number of the seat: 21
Time of the movie: 2.15

Do you like to continue? If yes, press 'Y':n

                        Booking Successful
                Movie Name:(null)
                Seat Number:0.000000
                Time:0.000000
                Discounted Price:RM(null)
                        Thank you
--------------------------------
Process exited after 12.51 seconds with return value 0
Press any key to continue . . .

核心问题排查与修复

1. 字符串存储错误:单个char无法存储字符串

name、movie被声明为单个char变量,但scanf("%s")需要读取字符串到字符数组中,单个char会导致内存溢出,后续输出乱码。
修复:将变量改为字符数组:

char name[50], CH, Std, movie[50]; // 数组大小可根据需求调整

2. 指针使用混乱:多余的&和错误的解引用

  • getDetails函数中,movie是数组名(本质是指针),scanf("%s", &movie)多了&,应该直接传movie;seat和Time已经是float*类型,scanf("%f", &seat)多了&,直接传seat即可。
  • FPrint函数中,printf("%s", &movie)多了&,直接用movie;float类型的seat、Time需要直接使用指针指向的值(解引用*seat、*Time),Disc是float*,输出时要用%f格式符并解引用*Disc,不能用%s。

修复后的getDetails和FPrint:

void getDetails(char*movie, float*seat, float*Time)
{
    printf("Name of the movie: ");
    scanf("%s", movie);
    printf("Number of the seat: ");
    scanf("%f", seat); 
    printf("Time of the movie: ");
    scanf("%f", Time);
}

void FPrint(char*movie, float*seat, float*Time, float*Disc)
{
    printf("\n\t\t\tBooking Successful");
    printf("\n\t\tMovie Name:%s", movie);
    printf("\n\t\tSeat Number:%f", *seat);
    printf("\n\t\tTime:%f", *Time);
    printf("\n\t\tDiscounted Price:RM%f", *Disc);
}

3. 赋值运算符错误:用==代替=

代码中多处用比较运算符==代替赋值运算符=,比如RM == 15;、*Disc == ((30/100)*RM);,导致变量无法正确赋值。
修复:将所有错误的==改为=,同时注意整数除法问题(30/100结果为0,需用30.0/100或0.3):

RM = 15;
*Disc = (0.3 * RM);

4. 变量未初始化与输入错误

  • main中RM未初始化,传递给getDiscount时是垃圾值,直接在getDiscount中赋值RM=15即可。
  • scanf("%s",&CH)读取字符时用%s会导致溢出,应该用%c,并加空格跳过换行符:scanf(" %c", &CH);

5. 函数返回值不匹配

getDetails和FPrint声明为float但没有返回值,改为void更合理。


修复后的完整代码

#include <stdio.h>
float getUserDetail(char*, float*);
float getDiscount(char, float, float*);
void getDetails(char*, float*, float*);
void FPrint(char*, float*, float*, float*);

int main()
{
    char name[50], CH, Std, movie[50];
    float age, Disc, seat, Time;
    printf("\t\t\t\tWELCOME TO UniMAP CINEMA!!!");
    do      
    {
        getUserDetail(name, &age);
        getDiscount(Std, age, &Disc);
        getDetails(movie, &seat, &Time);
        printf("\nDo you like to continue? If yes, press 'Y':");
        scanf(" %c", &CH);
    }
    while(CH=='Y'||CH=='y');
    FPrint(movie, &seat, &Time, &Disc);
    printf("\n\t\t\tThank you");
    return 0;
}

float getUserDetail(char*name, float*age)
{
    printf("\nName: ");
    scanf("%s", name);
    printf("Age: ");
    scanf("%f", age);
    return (*age);
}

float getDiscount(char Std, float age, float*Disc)
{
    float RM = 15; // 直接初始化RM
    if (age < 13){
        *Disc = (0.3 * RM); 
        return (*Disc); 
    }
    else if (age < 19){
        printf("Are you a student?(Y/N):");
        scanf(" %c", &Std); // 用%c读取字符
        if(Std == 'Y'|| Std == 'y'){
            *Disc = (0.5 * RM);
            return (*Disc); 
        }
        else{
            *Disc = RM;
            return (*Disc); 
        }
    }
    else{
        *Disc = RM;
        return (*Disc); 
    }
}

void getDetails(char*movie, float*seat, float*Time)
{
    printf("Name of the movie: ");
    scanf("%s", movie);
    printf("Number of the seat: ");
    scanf("%f", seat); 
    printf("Time of the movie: ");
    scanf("%f", Time);
}

void FPrint(char*movie, float*seat, float*Time, float*Disc)
{
    printf("\n\t\t\tBooking Successful");
    printf("\n\t\tMovie Name:%s", movie);
    printf("\n\t\tSeat Number:%f", *seat);
    printf("\n\t\tTime:%f", *Time);
    printf("\n\t\tDiscounted Price:RM%.2f", *Disc); // 保留两位小数更合理
}

修复后效果

运行代码后,输入信息将正确存储并打印,示例输出:

WELCOME TO UniMAP CINEMA!!!
Name: kaus
Age: 21
Name of the movie: qwerty
Number of the seat: 21
Time of the movie: 2.15

Do you like to continue? If yes, press 'Y':n

                Booking Successful
        Movie Name:qwerty
        Seat Number:21.000000
        Time:2.150000
        Discounted Price:RM15.00
                Thank you

内容的提问来源于stack exchange,提问作者KAUSHAL SINGH AL CHAMKOR SINGH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 09:20:42