【发布时间】:2014-03-26 17:30:07
【问题描述】:
好的,这里我有这个sn-p的代码:
struct Computer
{
char name[20];
float hdd;
int price;
int ram;
};
struct Computer list[7];
int n = 0;
for(n; n < 7; n++)
{
printf("\nEnter PC name, 20 characters:");
/* here should be name entry for list[n].name */
}
很多方法都行不通。我尝试了fgets()、getch()、scanf() 和许多其他方法,但要么不起作用,要么没有足够的功能。
fgets(list[n].name, 20, stdin) 无法识别条目中的 EOF,这是必需的。
list[n].name[counterChar] = getch() 只是有一些奇怪的行为,并且有很多回声的例程。
scanf("%c", &list[n].name[counterChar]) 让用户在每个符号后按回车键。
我需要的是:名称限制为 20 个字符,包括空格,如果是 EOF,则跳出循环。
【问题讨论】:
-
对于完整的
name条目,您希望如何终止每个输入?例如,假设您想将其命名为"Ha Ha!",您想为此做什么?
标签: c string pointers struct char