在程序下方获取用户输入,直到遇到一个空格。(程序与此一起工作)但我希望用户在仅按“Enter”键时停止它。所以我改变了
(buffer[0] != '\0')
到
(buffer[0] != '\n')
但这不起作用。我怎样才能做到这一点? (我知道“输入”意味着“\ n”)
#include <stdio.h>
int main(void){
int count = 0;
char buffer[80];
char *myDynamicArray[25];
int i = 0;
int k = 0;
puts("Enter integers, press space to end");
while ((i < 25) && (gets(buffer) != 0) && (buffer[0] != '\0'))
{
if ((myDynamicArray[i] = (char *)malloc(strlen(buffer) + 1)) == NULL)
return -1;
strcpy(myDynamicArray[i++], buffer);
}
count = i;
for (k = 0; k < count; k++)
{
printf("%s\n", myDynamicArray[k]);
}
getchar();
}
答案 0 :(得分:4)
当用户点击&#34;输入&#34>时,您当前的程序会停止它。仅限密钥,如果您输入单个空格,它不会停止。
gets
删除换行符并且不将其存储在缓冲区中,因此缓冲区[0]将等于&#39; \ 0&#39;没有输入,如果输入空格,则为。
一些注意事项:
启用编译器警告。警告为您提供潜在错误的提示。
您遗失了2个#include
个文件:stdlib.h
和string.h
。
gets
由于其不安全的性质visavi缓冲区溢出而被弃用。请改用fgets
!
答案 1 :(得分:1)
好的,我能想到的最基本的#em>&#34;传统&#34; 基本方法就是这样做:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ( void )
{
int c, i, j=0;
char buffer[80], *dynArray[5];
for (i=0;i<5;++i)
{
j = 0;
while((c = getchar()) != EOF && c != '\n' && j < 79)
buffer[j++] = c;
if (j == 0)
{//an empty string was encountered!
for (--i;i>-1;--i)
{//use --i, because current i is not allocated yet!
printf("Freeing memory containing \"%s\"\n", dynArray[i]);
free(dynArray[i]);
}
return EXIT_SUCCESS;//exit program here, then
}
buffer[j] = '\0';//add NULL-char
dynArray[i] = calloc(j, sizeof *dynArray[i]);//calloc to initialize memory
if (dynArray[i] == NULL)
exit(EXIT_FAILURE);
strcpy(dynArray[i], buffer);
}
//allow user to see a specific input string again:
puts("Choose a string to see again [1-5], or enter q to proceed");
while ((c = getchar()) != EOF && c != 'q')
{
c -= (1 + '0');//simple atoi trick
if (c < 5 && c > -1)//input char is 1, 2, 3, 4 or 5
printf("string %d: %s\n", c+1, dynArray[c]);
}
//q was fetched from stdin, clear buffer...
while((c = getchar()) != EOF && c != '\n');
for (i=0;i<5;++i)
{
printf("free-ing memory containing: %s\n", dynArray[i]);
free(dynArray[i]);
}
getchar();//press any key to continue...
return 0;
}
你可以随心所欲地玩这个(将其中的一些部分分成函数等)...
这是一个更有用的例子:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int get_key( const int max )
{
int c, r = 0;
while ((c = getchar()) != EOF && c != 'q' && c != '\n')
{
c -= '0';
if (c > -1 && c < 10)
{
if (r > 0)//suppose input was 22, r will be 2 on second iteration of loop
r *=10;//multiply by 10, to get 20
r += c;//add 2 == 22
}//optionally add else, clean buffer and procede if r > 0
}
if (r > max || r == 0)//perhaps move to loop body
r = -1;//out-of-bounds request = -2
if (c == 'q')
{
r = 0;//stop = -1
//clean buffer
while (c != EOF && c != '\n')
c = getchar();
}
return r-1;//subtract 1 to get array index (zero indexed)
}
int main ( void )
{
int c, k, i;
char buffer[80], *myDynamicArray[25];
puts("Enter string");
for (k=0;k<25;++k)
{
i=0;
while((c = getchar()) != EOF && c != '\n' && i < 79)
buffer[i++] = c;
if (i == 0)
break;
if (i == 79)
while (c != EOF && c != '\n')
c = getchar();//clean buffer
buffer[i] = '\0';
myDynamicArray[k] = calloc(i, sizeof *myDynamicArray[k]);
if (myDynamicArray[k] == NULL)
exit(EXIT_FAILURE);
strcpy(myDynamicArray[k], buffer);
}
printf("Choose which string you'd like to see again [1-%d], press q to quit\n", k);
while ((c = get_key(k)) != -1)
if (c != -2)
printf("String %d: %s\n", c+1, myDynamicArray[c]);
else
printf("input must be in the 1 - %d range\n", k);
for (i=0;i<k;++i)
{
printf("%s\n", myDynamicArray[i]);
free(myDynamicArray[i]);
}
printf("Press any key to continue...");
getchar();
return 0;
}