-1

我正在与结构作斗争,并且有一些我不知道该怎么做的问题。首先,我必须定义一个结构调用部分,其中包含 int 变量 part_number,i 和一个 25 个字符的字符数组。其次,我必须将 part 定义为 struct 部分的同义词。第三次从键盘读入part_number 和part_name 到变量a 的各个成员中。

include <stdio.h>

int main(void)
{
 struct part_containg

{
    int     part_number,i;
char    part_name[25] !='\0';   
    // set up array of 25 characters and include terminating null character
};

struct  part_containg   part_number,part_number[i];

for(part_number=0; part_number<25;++part_number)  // part_number goes up 1 for each    part_name
{
    printf("Intersert Part Name"\n);
    scanf("%c", &part_name[i]);         // scans for part_name
}
 return 0;
}
4

1 回答 1

0

你有很多语法错误。您应该查阅介绍性教科书:它们真的很有帮助!我已经读了半打,我对自己的评价很低~“C技能连续体”~

无论如何,我们都是从底部开始的。这是一个有很多评论的替代版本:

#include <stdio.h>  /* you need a hash sign to include libraries...!     */
#include <string.h> /* i use this library to copy input rather than loop */

int main() {                      // main does not take an argument

  struct part_containing {
    int part_number;              // what is i for? I removed it
    char part_name[25];           // do the null later, this  is not the time!
  };

  struct part_containing name;    // only declare the struct, not its variables
                                  // p.s. name is an awful name-choice :P

  char input[25];                 // make a local var for input
  puts("Enter part name:\n");     // prompt user for input
  scanf("%s", input);             // capture local var
  strcpy(name.part_name, input);  // copy it into the struct
  name.part_name[25] = '\0';      // null just to be sure!

  puts("Enter number:\n");        // same for the number now
  scanf("%i", &name.part_number); // notice the syntax: &name.part_number...!

  printf("%s and %i", name.part_name, name.part_number); 
  // simple error check
  // did we get the expected input? It can be helpful to check in test runs!

  return 0;
}

它不能回答你作业中的所有问题,但它应该足以让你开始。

希望这会有所帮助!如果您正在研究它并有更多问题,请提问!

于 2013-03-21T01:32:19.737 回答