gpt4 book ai didi

c - 输出未正确显示

转载 作者:行者123 更新时间:2023-11-30 21:27:21 26 4
gpt4 key购买 nike

我是 C 编程的初学者,我编写了以下代码,但它在输出时产生错误。代码是-:

#include <stdio.h>
#include <conio.h>

void main()
{
int n=0;
int i,k;
char name;
printf("\t\t Program to find the overtime of 10 workers\n\n");
for(i=0;i<=10;i++){
printf("Enter the name of worker");
scanf("%char",&name);
printf("Enter the number of hours worked");
scanf("%d",&n);
if(n>10){
k=n-10;
printf("He is eligible for overtime for overtime of rs %d\n",k*12);
}
else{
k=0;
printf("He is not eligible to any overtime in rs %d \n",k);
}
}
getchar();
}

第一次显示正确的结果,但是当我第二次输入工作人员的姓名时,它给出了错误的输出。我无法检测到错误。

最佳答案

  1. 名称应该是字符数组==> char name[15];
  2. 第二个可见错误是 for循环:当您查找 10 个工作条目时,您的数组循环将从 i=0 运行到 i=10,即 11 个条目。所以,for(i=0;i<10;i++) ==> i<10而不是i<=10 .
  3. 您的scanf声明==> scanf("%s",name);不是%char这是错误的。

关于c - 输出未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52200267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com