1
在这个程序中的东西100层结构:我想有一个循环或类似的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct person{
char name[30];
char address[30];
int phoneNumber[30];
char creditRating[30];
};
int main(){
struct person p;
printf("What is the person's name?\n");
scanf(" %s", p.name);
printf("What is the person's address?\n");
scanf(" %s", p.address);
printf("What is the person's phone number?\n");
scanf("%d", &p.phoneNumber);
printf("What is the person's credit rating?\n");
scanf(" %s", p.creditRating);
printf("The person's name is %s\n", p.name);
printf("The person's address is %s\n", p.address);
printf("The person's phone number is %d\n", p.phoneNumber);
printf("The person's credit rating is %s\n", p.creditRating);
return 0;
}
我能有这样的事情
For(i=0;i>=n;i++)
struct person [i];
printf("What is the person's name?\n");
scanf(" %s", [i].name);
printf("What is the person's address?\n");
scanf(" %s", [i].address);
printf("What is the person's phone number?\n");
scanf("%d", &[i].phoneNumber);
printf("What is the person's credit rating?\n");
scanf(" %s", [i].creditRating);
我想有100层结构与他们的投入。有点难以一一写出,如:
struct person p;
.....
struct person q;
.....
//and etc...
我该如何避免这种情况?
'struct person p [100];对于(i = 0; i <100; i ++){... scanf(“%29s”,p [i] .name); ...' – BLUEPIXY
要小心。使用'scanf(“%s”,...)'表示名称和地址字段不能带空格。你可能会用'fgets()'来读取行和'sscanf()',以便在必要时分析它们。谨防换行符。此外,你应该通过测试返回值来检查'scanf()'函数调用是否成功。如果不是1,则出现问题。是;你必须每次检查。干得不错,因为不想将相同的代码写出100次。这在编程中很重要。避免重复。 –
空格键是否存在问题 - 因为代码需要格式化以使其可读 –