我用 C 语言编写,为什么我的代码没有运行?我在 ss 上画了错误

i'm writing in c, why is my code not running? I drawn on the ss the error

提问人:Bruno Duarte 提问时间:11/17/2023 最后编辑:Ted LyngmoBruno Duarte 更新时间:11/17/2023 访问量:40

问:

肖特

我不明白为什么我的终端会出现这条消息:

请输入要计算不带任何空格的百分比的第一个变量: 莫兰戈斯 PS C:\Users\TechnoStoreReparacoe\Desktop>

这是完整的代码:

#include <stdio.h>
#include <string.h>

int main (){
    
    char name1[25] ;
    char name2[25] ;
    char name3[25];
    char name4[25];
    char name5[25];

    int value1 ;
    int value2 ;
    int value3 ;
    int value4 ;
    int value5 ;
    
    
    printf ("Hello mortal, this program allows you to calculate percentages of five parts that are the only components of the whole.\n");
    
    printf ("\nPlease enter your 1st variable that you want to calculate the percentage of without any spaces:\n");
    fgets (name1, 25, stdin);
    printf ("\nPlease enter the value of %s:\n", name1[25]);
    scanf ("%i", &value1);
    
    printf ("\nPlease enter your 2nd variable that you want to calculate the percentage of without any spaces:\n");
    fgets (name2, 25, stdin);
    printf ("\nPlease enter the value of %s:\n", name2[25]);
    scanf ("%i", &value1);

    
    printf ("\nPlease enter your 3rd variable that you want to calculate the percentage of without any spaces:\n");
    fgets (name3, 25, stdin);
    printf ("\nPlease enter the value of %s:\n", name3[25]);
    scanf ("%i", &value1);


    printf ("\nPlease enter your 4th variable that you want to calculate the percentage of without any spaces:\n");
    fgets (name4, 25, stdin);
    printf ("\nPlease enter the value of %s:\n", name4[25]);
    scanf ("%i", &value1);


    
    printf ("\nPlease enter your 5th variable that you want to calculate the percentage of without any spaces:\n");
    fgets (name5, 25, stdin);
    printf ("\nPlease enter the value of %s:\n", name5[25]);
    scanf ("%i", &value1);


    return 0;
}

我正在制作一个概率查找程序,非常简单。 用户输入变量名称和相应的数值。 这是我的第一个程序,我不明白为什么代码运行不好

c 字符串 变量 终端 错误报告

评论

1赞 Ted Lyngmo 11/17/2023
首先,解决程序中的即时问题。它们列在这里: godbolt.org/z/GzM9Txr6P - 如果在修复所有当前问题后问题仍然存在,请更新问题。

答:

0赞 chux - Reinstate Monica #1

printf() 需要一个 char *,而不是一个 char

name1[25]中是数组的第 25 个(从 0 开始)元素。这是在数组之外,无法读取。printf()name1[]charname[]

"%s"需要匹配的指针,即 .char *

char name1[25] ;
// printf ("\nPlease enter the value of %s:\n", name1[25]);
printf ("\nPlease enter the value of %s:\n", name1);