我用c写的,为什么我的代码没有运行?我在 ss 上画出了错误

问题描述 投票:0回答:1

sshot

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

请输入您要计算百分比的第一个变量,不带任何空格: 莫兰戈斯 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 string variables terminal error-reporting
1个回答
0
投票

printf()
期望得到
char *
,而不是
char

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

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

char name1[25] ;
// printf ("\nPlease enter the value of %s:\n", name1[25]);
printf ("\nPlease enter the value of %s:\n", name1);
© www.soinside.com 2019 - 2024. All rights reserved.