神王豪斯(重拾基础期) 2023-12-24 12:22 采纳率: 0%
浏览 20
已结题

VS2022没有报错但是无法编译

img


我希望实现先输入两位学生的信息 比较过后输出成绩更高的学生的所有信息 以上就是我的代码 输完之后系统没有报错 却一直无法编译 显示发生生成错误

  • 写回答

2条回答 默认 最新

  • 译泽在努力 2023-12-24 13:46
    关注

    img


    经本人验证程序可以运行,运行结果如上图所示,不过建议程序改写成如下形式:

    #include <stdio.h>
    struct student
    {
        char name[20];
        float score;
        int num;
    }stu1,stu2;
    int compareScore(struct student stu1,struct student stu2)
    {
        if(stu1.score > stu2.score)
        {
            printf("%s %2.2f %d\n",stu1.name,stu1.score,stu1.num);
        }else if(stu1.score < stu2.score) {
            printf("%s %2.2f %d\n",stu2.name,stu2.score,stu2.num);
        }else{
            printf("%s %2.2f %d\n",stu1.name,stu1.score,stu1.num);
            printf("%s %2.2f %d\n",stu2.name,stu2.score,stu2.num);
        }
    }
    int main()
    {
        scanf("%s%f%d",stu1.name,&stu1.score,&stu1.num);
        scanf("%s%f%d",stu2.name,&stu2.score,&stu2.num);
        compareScore(stu1, stu2); 
        return 0;
    }
    

    当然这种传参的方式有着浪费内存空间的缺点,所以可以使用指针传参,写法如下:

    #include <stdio.h>
    #define _CRT_SECURE_NO_WARNINGS
    struct student
    {
        char name[20];
        float score;
        int num;
    }stu1,stu2;
    int compareScore(int *p, int *q)
    {
        if(stu1.score > stu2.score)
        {
            printf("%s %2.2f %d\n",stu1.name,stu1.score,stu1.num);
        }else if(stu1.score < stu2.score) {
            printf("%s %2.2f %d\n",stu2.name,stu2.score,stu2.num);
        }else{
            printf("%s %2.2f %d\n",stu1.name,stu1.score,stu1.num);
            printf("%s %2.2f %d\n",stu2.name,stu2.score,stu2.num);
        }
    }
    int main()
    {
        scanf("%s%f%d",stu1.name,&stu1.score,&stu1.num);
        scanf("%s%f%d",stu2.name,&stu2.score,&stu2.num);
        int *p = &stu1;
        int *p1 = &stu2;
        compareScore(p,p1);
        return 0;
    }
    

    最后希望我的答案对你有所帮助,如果有不懂的地方欢迎私信与我,我会帮你解答,或者关注我的博客,内有C语言,C++等笔记,希望对你的学习有所帮助,谢谢。

    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 7月18日
  • 创建了问题 12月24日

悬赏问题

  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥50 如何将脑的图像投影到颅骨上
  • ¥15 提问一个关于vscode相关的环境配置问题,就是输入中文但是显示不出来,代码在idea可以显示中文,但在vscode不行,不知道怎么配置环境
  • ¥15 netcore使用PuppeteerSharp截图
  • ¥20 这张图页脚具体代码该怎么写?
  • ¥20 WPF MVVM模式 handycontrol 框架, hc:SearchBar 控件 Text="{Binding NavMenusKeyWords}" 绑定取不到值
  • ¥15 需要手写数字信号处理Dsp三个简单题 不用太复杂
  • ¥15 数字信号处理考试111
  • ¥15 allegro17.2生成bom表是空白的
  • ¥15 请问一下怎么打通CAN通讯