功能来读取用户输入的句子 [英] Function to read sentence from user input
本文介绍了功能来读取用户输入的句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想读取用户输入的问题,一句我的功能是,当我试图把它它跳过第二次尝试。任何解决方案?
无效readString(的char *数组,字符*提示,诠释大小){
的printf(%S,提示);
焦炭℃;诠释计数= 0;
字符*发送=阵列;
而((C =的getchar())!='\\ n'){
发[计数] = C;算上++;
如果(大小<计数){免费(数组);打破; } //让ü保留最后指数'\\ 0'
} }
下面是如何设法叫它:
字符义务[1500];
烧焦dodatno [1500];readString(义务,回车义务,1500);
readString(dodatno,输入时间,1500);
下面是例如输入:
这是一些句子
所以我后期瓦纳做到这一点:
的printf(%S |%S \\ n,义务,dodatno);
和获取:
这是一些句子|这就是另一句话
块引用>解决方案有你去:)
无效readString(的char *数组,字符*提示,诠释大小){
的printf(%S,提示);
INT℃;诠释计数= 0;
而((C =的getchar())='\\ n'和;!&安培;!C = EOF);
而((C =的getchar())!='\\ n'){
数组[计数] = C;算上++;
如果(计数==(大小 - 1)){打破; }
}
数组[统计] ='\\ 0';
}I am trying to read sentence from user input problem with my function is it skips second try when I try to call it. Any solution?
void readString(char *array, char * prompt, int size) { printf("%s", prompt); char c; int count=0; char * send = array; while ((c = getchar()) != '\n') { send[count] = c; count++; if (size < count){ free(array); break; } //lets u reserve the last index for '\0' } }
Here is how try to call it:
char obligation[1500]; char dodatno[1500]; readString(obligation, "Enter obligation", 1500); readString(dodatno, "Enter hours", 1500);
Here is example of inputs: "This is some sentence"
so latter I wana do this:
printf(" %s | %s \n",obligation, dodatno);
and get:
This is some sentence|This is another sentence
解决方案There you go :)
void readString(char *array, char * prompt, int size) { printf("%s", prompt); int c; int count=0; while((c = getchar()) != '\n' && c != EOF); while ((c = getchar()) != '\n') { array[count] = c; count++; if (count == (size - 1)) { break; } } array[count] = '\0'; }
这篇关于功能来读取用户输入的句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文