如何读取C中以空格分隔的0-9位数字?
我正在尝试制作一个程序,读取C语言中以空格分隔的正数,并向任何其他输入格式发出错误消息 例如,以下输入是正确的:如何读取C中以空格分隔的0-9位数字?,c,arrays,io,C,Arrays,Io,我正在尝试制作一个程序,读取C语言中以空格分隔的正数,并向任何其他输入格式发出错误消息 例如,以下输入是正确的: 0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0 7 6 5 4 3 1 2 3 ... 对于所有其他输入,程序应终止并打印错误消息。例如: 0 1,2 3 4-5 67 89 0123456789 0a2b3c4d5e6f7g8h9i ... 以下是我的尝试: ... int inputArr[999]; int length = 0; char
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0
7 6 5 4 3
1 2 3
...
对于所有其他输入,程序应终止并打印错误消息。例如:
0 1,2 3 4-5 67 89
0123456789
0a2b3c4d5e6f7g8h9i
...
以下是我的尝试:
...
int inputArr[999];
int length = 0;
char c = getchar();
while ( c != '\n' ) {
if ( isdigit(c) ) {
inputArr[length] = c - '0';
length++;
} else {
printf ("Wrong Input Format!\n");
}
c = getchar();
if ( c != ' ' ) {
printf ("Wrong Input Format!\n");
} else {
continue;
}
}
...
但即使输入正确,也会发出错误消息
更新:
当我输入以下内容时:
0 1 2 3 4 5 6 7 8 9
我希望程序不会给我任何错误消息,但我正好得到10条错误消息(在删除行exit(1);
)。我假设1和之后的每个字符都有一条错误消息(即9条消息),结尾的“\n”字符有一条消息。尝试以下操作:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
void e() { puts("Bad input."); exit(EXIT_FAILURE); }
int main(void)
{
for (int c1 = 0, c2 = 0; c1 != EOF && c2 != EOF; )
{
c1 = getchar();
if (c1 == EOF || c1 == '\n') continue; // file or line ends in "x "
if (!isdigit(c1)) e();
c2 = getchar();
if (c2 != EOF && c2 != '\n' && c2 != ' ') e();
printf("Got input: '%c'.\n", c1);
}
}
标志可用于在读取数字时发出信号,并防止连续数字。连续空格将不会被拒绝
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main( void)
{
int inputArr[999];
int c = 0;
int length = 0;
int gotdigit = 0;
while ( ( c = getchar ( )) != '\n' && c != EOF) {
if ( isdigit ( c) && !gotdigit) {//found digit and no prior consecutive digit
inputArr[length] = c - '0';
length++;
if ( length >= 999) {
break;
}
gotdigit = 1;//set true to prevent consecutive digits
} else {
if ( c == ' ') {
gotdigit = 0;//set false. found space so next digit is ok
}
else {//not a space or was consecutive digit
printf ("Wrong Input Format!\n");
}
}
}
return 0;
}
#包括
#包括
#包括
内部主(空)
{
int-inputArr[999];
int c=0;
整数长度=0;
整数位数=0;
而((c=getchar())!='\n'&&c!=EOF){
if(isdigit(c)&&&!gotdigit){//找到的数字,没有先前的连续数字
inputArr[length]=c-“0”;
长度++;
如果(长度>=999){
打破
}
gotdigit=1;//设置为true以防止连续数字
}否则{
如果(c=''){
gotdigit=0;//设置为false。已找到空格,因此下一个数字是确定的
}
else{//不是空格或是连续数字
printf(“输入格式错误!\n”);
}
}
}
返回0;
}
谢谢,但不完全是:标题的目的主要是为了在索引上列出,以引起人们的注意。但这篇文章的主体也应该是一个问题。它应该准确地陈述你的期望,哪里出了问题,以及你的具体问题,尽可能地缩小范围。不要只是问“为什么这不起作用”,而是明确地问“为什么X不做Y乘Z,当手册声称X应该做Y(引文)时”,等等。你的逻辑似乎很混乱。您拥有所有这些if
s,但它们没有else
s。因此,您基本上没有机会在正确的时间发现错误。你的换行符检测也不完整。您没有检测输入结束的机制。我添加了else语句,但我仍然不明白为什么输入格式正确时会收到错误消息。非常感谢。我要玩玩它。我正在考虑使多个空格成为可接受的输入格式。@user6005857:您可能希望scanf
而不是手动执行所有这些操作。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main( void)
{
int inputArr[999];
int c = 0;
int length = 0;
int gotdigit = 0;
while ( ( c = getchar ( )) != '\n' && c != EOF) {
if ( isdigit ( c) && !gotdigit) {//found digit and no prior consecutive digit
inputArr[length] = c - '0';
length++;
if ( length >= 999) {
break;
}
gotdigit = 1;//set true to prevent consecutive digits
} else {
if ( c == ' ') {
gotdigit = 0;//set false. found space so next digit is ok
}
else {//not a space or was consecutive digit
printf ("Wrong Input Format!\n");
}
}
}
return 0;
}