字符串未完全打印C

时间:2015-07-08 20:01:37

标签: c printf

注意:这是为了做作业,我只是想知道为什么我弄乱了打印,而不是完成的程序。

#include    <stdio.h>

char    get_band( int band_number, char band_color[] );
void    get_resistance( int resist, int power );

int main()
{
    int     resist;
    int     power;
    get_resistance(resist, power );
}

void get_resistance( int resist, int power )
{
    int     band_number;
    char    band_color[3];
    char    color[3];


    get_band( band_number, band_color );
    printf("%s", band_color);
}
char get_band( int band_number, char band_color[] )
{
    int     x;
    x=0;
    while (x < 3)
    {
        printf("Which band would you like to select? (1-3)\nDo not select one you have selected prior!\t");
        scanf("%d", &band_number);

        if (band_number = 1)
        {
            printf("What color would you like to assign here?\t");
            scanf("%s", &band_color[0]);
            x++;
        }
        else if (band_number = 2)
        {
            printf("What color would you like to assign here?\t");
            scanf("%s", &band_color[1]);
            x++;
        }
        else if (band_number = 3)
        {
            printf("What color would you like to assign here?\t");
            scanf("%s", &band_color[2]);
            x++;
        }

    }
    return (*band_color);

}

因此,当我运行它时,我没有得到错误或警告,但我得到的是我输入的最后一种颜色。例如,我按顺序输入绿色,蓝色,黄色。我会打印回黄色。无论我使用什么顺序的数字,我总是回到最后输入的颜色。

2 个答案:

答案 0 :(得分:4)

您的代码存在两个主要问题:

首先:

if (band_number = 1) {
  ...
}

=是赋值运算符==等于运算符。由于band_number = 1始终评估为1,因此始终采用分支。

第二

char    band_color[3];

然后再说:

scanf("%s", &band_color[0]);

你没有宣布足够的空间,而且你在先前的价值上踩踏。此外,您希望静态分配一个字符串数组,您需要执行以下操作:

void get_resistance( int resist, int power )
{
  int     band_number;
  char    band_color[3][20];


  get_band( band_number, band_color );
  printf("%s\n", band_color[0]);
  printf("%s\n", band_color[1]);
  printf("%s\n", band_color[2]);
}

void get_band( int band_number, char band_color[][20] )
{
  int     x;
  x=0;
  while (x < 3)
    {
      printf("Which band would you like to select? (1-3)\nDo not select one you have selected prior!\t");
      scanf("%d", &band_number);
      printf("What color would you like to assign here?\t");

      scanf("%s", band_color[band_number - 1]);
      x++;
    }

}

我已经大大简化了你的代码逻辑,因为你有很多重复。关键的一点是char band_color[3][20]将为3个20个字符长的字符串(包括终止字符)声明足够的空间。 get_band的函数原型已更改为适应新类型的band_color

代码简化实际上删除了if语句,使用的观察结果是用户输入的数字总是比数组索引大1。然后,您可以使用该数字减1来索引数组,即band_color[band_number - 1]

请注意,如果在提示时键入两个长的字符串,则容易受到缓冲区溢出的影响。这不是高质量的代码,只是试图证明你的错误

如果可能找到this link helpful with regards to getting user input

也是一个很好的link to why using scanf is bad for user input

答案 1 :(得分:0)

你的程序有些问题。不必要地有很多不良的冗余传递。您可以将band_color声明为全局变量,也可以在get_band函数中声明它。 band_number也是如此。没有理由在get_resistance中声明它只是为了将它传递给get_band。只需在get_band中声明它,因为它是唯一使用它的地方。然后,您可以返回指向band_color数组的指针以用于printf。 在get_band的if语句中,你错误地写了评估者。你使用=应该在哪里==。

if (band_number == 1)

这导致您的代码允许进入第一个if并仅将变量分配给[0]数组。你的数组只需要[2]就可以得到3个条目。您还需要使用第二组括号声明数组中字符串的长度。

char band_color[2][10];