输出未正确显示

时间:2018-09-06 08:57:03

标签: c

我是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();
}

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

4 个答案:

答案 0 :(得分:0)

这归因于格式说明符%char。这不是有效的格式说明符,C不能理解%char这样的东西。

养成标准库阅读计划使用的功能手册页的习惯。例如,scanf。通读该手册页的转化部分。这将指导您有关scanf()将使用哪种格式说明符。

查看那里的变量:

char name;

它是一个char,只能容纳一个字符。 要存储名称,我假设您需要多个char。因此,您可能需要使用字符数组。

因此,将其更改为:

char name[MAX_LENGTH_NAME];  //MAX_LENGTH_NAME can be a macro set to some value, example 32?

然后将您的scanf()工人姓名呼叫更改为

scanf("%s", name);   // Note: `&` is gone here!

scanf()获取返回值,并为意外返回添加一些错误处理,这将为您免除许多痛苦。

printf()说,它是一个程序,可以为10个工人计算一些东西,

for(i=0;i<=10;i++)

这将循环11次,而不是10次。我希望您不是第11个工作人员:-)。
将其更改为for(i = 0; i < 10; i++)

此外,conio.h是非标准的。如果您在要求使用此标头的平台上进行开发,则可能需要考虑切换到更好的开发环境。例如,带有bash终端和标准gcc编译器的linux计算机。

最后,void main很糟糕,请改用int main(void)。要详细了解原因,请阅读What should main() return?

重要提示:GUI设计以及您在控制台上抛出的消息非常重要。 He 是严格男性化的:D周围没有女工吗?如果您开始打印名称而不是 He 。可能会更好。

答案 1 :(得分:0)

  1. 名称应为char数组==> 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

答案 2 :(得分:0)

您的代码中几乎没有问题。首先是这个

scanf("%char",&name);

是错误的,您应该提供正确的格式说明符来扫描%char,而不是name

char name;
scanf("%c",&name);

运行成功,但我希望您不希望一个人name用单个字符表示,因此最好将name声明为char数组。对于例如

char name[MAX_LEN];/*define MAX_LEN] */

然后使用

scanf("%s",name);

并且for循环条件i<=10将循环旋转11而不是10次(如您提到的10个工作程序),可能需要i<10

也不要使用void main() { }

int main(void) {

    return 0;
}

编辑:-由于您修改了问题。和 这是第一次显示正确的结果,但是当我第二次输入工人的名字时,它给我的输出是错误的。我无法检测到错误。 ?

为避免这种情况

scanf("%c",&c);

应该是

scanf(" %c",&c); /* give the white space before %c */

附带说明,强烈建议检查诸如scanf()之类的预定义函数的返回值,以了解它们是成功还是失败,为此请阅读手册页。对于例如

char name;
int ret = scanf(" %c",&name);
if(ret == 1) {
    /* do something */
}
else {
   /* error handling */
}

答案 3 :(得分:0)

您的代码有几个问题:

  • conio.h标头没有用,
  • 您没有保留足够的空间来存储名称,
  • 您不检查scanf结果。

更正后,您的代码可能类似于:

/* only include stdio.h, conio.h is not portable and unnecessary here */
#include <stdio.h>

/* main should return an int*/
int main(void)
{
    int n=0;
    int i,k;
    /* often, a name is composed of several characters */
    char name[128];
    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\n");

        if (1 != scanf("%s", name))
        {
            /* on error, exit loop */
            perror("scanf");
            break;
        }

        printf("Enter the number of hours worked\n");
        if (1 != scanf("%d",&n))
        {
            /* scanf did not manage to read one integer, exit loop */
            perror("scanf");
            break;
        }
        if (n>10) {
            k=n-10;
            printf("%s is eligible for overtime for overtime of rs %d\n", name, k*12); 

        }
        else{
            printf("%s is not eligible to any overtime in rs 0 \n",name);
        }
    }
    getchar();

    return 0;
}

最后,这里是关于scanf的好读物:http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html

相关问题