C语言 C 在循环中将输入读入字符数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15721831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C Reading input into an char array in loop
提问by jamesT
I am reading input from stdine.g. name of car, type of car.
我正在读取来自stdin例如汽车名称、汽车类型的输入。
The problem I am facing is, if I entered number of cars 2 and I take
input in loop, the second value is overwritten to the first value via scanf.
我面临的问题是,如果我输入了汽车数量 2 并在循环中输入,则第二个值将通过scanf.
Enter number of cars:2
输入汽车数量:2
Enter name of column :chevy
输入列名称:雪佛兰
Type of car available can be electric,gas - Enter type of car :gas
可用的汽车类型可以是电动、汽油 - 输入汽车类型:汽油
Enter name of column :chevy2
输入列名称:chevy2
Type of car available can be electric,gas - Enter type of car :electric
可用的汽车类型可以是电动的,汽油的 - 输入汽车类型:电动
now if I print all I see is electric
现在如果我打印所有我看到的都是电动的
#include <stdio.h>
int main(int argc, char *argv[])
{
setbuf(stdout, NULL);
int carNum;
int i;
char carName[50];
char carType[200];
printf("\nEnter number of cars:");
scanf("%d",&carNum);
for(i=0;i<carNum;i++)
{
printf("\nEnter name of car :");
scanf("%s", &carName[i]);
printf("\nType of car available can be electric,gas - Enter type of car %d: ");
scanf("%s", &carType[i]);
}
for(i=0;i<carNum;i++)
{
printf("\nName of car %d: ",i+1);
printf("\n%s", &carName[i]);
printf("\nType of car %d: ",i+1);
printf("\n%c", &carType[i]);
}
return 0;
}
回答by Grijesh Chauhan
EditNew answer:
编辑新答案:
As you have shown you requirement that you wants to read some number of car names and types You need array of strings for carNameand cartypeinstead of simple char array. like(for an essay solution):
正如您所展示的,您需要读取一定数量的汽车名称和类型,您需要字符串数组,carName而cartype不是简单的字符数组。喜欢(对于论文解决方案):
#define LEN 100 // declare sufficient length size
char carName[50][LEN]; // in main()
char carType[50][LEN];
and your correct scanf and printf statements should be like this:
你正确的 scanf 和 printf 语句应该是这样的:
scanf("%d",&carNum);
for(i=0;i<carNum;i++){
printf("\nEnter name of car :");
scanf("%s", carName[i]);
printf("\nType of car available can be electric,gas - Enter type ofcar: ");
scanf("%s", carType[i]);
}
for(i=0;i<carNum;i++){
printf("\nName of car %d: ",i+1);
printf("\n%s", carName[i]);
printf("\nType of car %d: ",i+1);
printf("\n%s", carType[i]);
}
(notice: when I user %sI give char*as argument in scanf as well as in printf)
old answer:In first for loop, Your scanf statements are wrong :
(注意:当我使用时,我在 scanf 和 printf 中都%s给出char*了参数)
旧答案:在第一个 for 循环中,您的 scanf 语句是错误的:
scanf("%s", &carName[i]);
^ remove [i]
scanf("%s", &carType[i]);
^ remove [i]
You just need to write like:
你只需要这样写:
// printf
scanf("%s", &carName);
//print
scanf("%s", &carType);
in Second loop in printf statements are wrong.
在 printf 语句中的第二个循环中是错误的。
printf("\n%c", &carType[i]);
^ not need
correct is
正确的是
printf("\n%c", carType[i]);
Also I think your requirement is:
另外我认为你的要求是:
printf("\n%s", carType);
回答by stdcall
The arrays you're declaring doesn't represent strings, it represents a continuous memory composed of 50 bytes, and 200 bytes. This is enough to store a single 50 character string, and 200 character string respectivly. What you need is an array of strings, declared like this.
您声明的数组不代表字符串,它代表由 50 个字节和 200 个字节组成的连续内存。这足以分别存储单个 50 个字符的字符串和 200 个字符的字符串。你需要的是一个字符串数组,声明如下。
char * carnames[MAX_INPUT];
I sugesst you create a buffer for reading each string, and then use strdup()which will copy and allocate the string to it's place in carnames.
我建议您创建一个缓冲区来读取每个字符串,然后使用strdup()它将复制并将字符串分配到它在 carnames 中的位置。
Here's an example:
下面是一个例子:
char buffer[80];
char * carnames[MAX_INPUT];
for(i=0;i<carNum;i++)
{
scanf("%s", buffer);
carnames[i] = strdup(buffer);
}
Be aware that strdup allocates memory for the string, so once you're done you should free the memory.
请注意 strdup 为字符串分配内存,因此一旦完成,您应该释放内存。

