Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
while循环中的scanf不评估第一个信息块_C - Fatal编程技术网

while循环中的scanf不评估第一个信息块

while循环中的scanf不评估第一个信息块,c,C,尝试获取scanf以使用isdigit迭代和计算字符串的每个部分。然而,它似乎跳过了第一个“块”,从而抵消了一切。关于我做错了什么的建议 int main (void) { int icount = 0; int c = 0; int compare = 0; int len = 0; char s[256] = ""; printf("Enter a string:\n\n"); while (scanf("%s", s) == 1) {

尝试获取
scanf
以使用
isdigit
迭代和计算字符串的每个部分。然而,它似乎跳过了第一个“块”,从而抵消了一切。关于我做错了什么的建议

int main (void) {
    int icount = 0;
    int c = 0;
    int compare = 0;
    int len = 0;
    char s[256] = "";
    printf("Enter a string:\n\n");
    while (scanf("%s", s) == 1) {
       scanf("%255s", s);
       len = strlen(s);
       printf("the string is %d characters long\n", len);
       while (len > 0) {
          printf("c is on its s[%d] iteration\n", c);
          if (isdigit(s[c])) {
             compare = compare + 1;
          }
          c++;
          len--;
       }
       if (compare == strlen(s)) {
          icount = icount + 1;
       }
       c++;
    }
    printf("\ni count is %d\n", icount);
    return 0;
}
当我运行它时,我会像这样不断地返回数据:

/a
输入一个字符串:
17测试17
字符串长度为4个字符
c正在进行s[0]迭代
c正在进行s[1]迭代
c正在进行s[2]迭代
c正在进行s[3]迭代
字符串长度为2个字符
c正在进行s[5]迭代
c正在进行s[6]迭代
我数的是0

从上面的评论中,我相信这可能就是您想要的

#include <ctype.h>
#include <stdio.h>

int main (void)
{
    int  icount;
    int  index;
    char string[256];

    printf("Enter a string:\n\n");

    icount = 0;
    while (scanf("%255s", string) == 1)
    {
       int isNumber;

       isNumber = 1;
       for (index = 0 ; ((string[index] != '\0') && (isNumber != 0)) ; ++index)
       {
          printf("index is on its string[%d] iteration\n", index);
          if (isdigit(string[index]) == 0)
            isNumber = 0;
       }
       if (isNumber != 0)
          icount += 1;
    }
    printf("\nicount is %d\n", icount);

    return 0;
}
#包括
#包括
内部主(空)
{
int icount;
整数指数;
字符串[256];
printf(“输入字符串:\n\n”);
i计数=0;
while(scanf(“%255s”,字符串)==1)
{
整数;
isNumber=1;
对于(索引=0;((字符串[index]!='\0')&&(isNumber!=0));++index)
{
printf(“索引位于其字符串[%d]迭代\n”,索引);
if(isdigit(字符串[索引])==0)
isNumber=0;
}
如果(isNumber!=0)
i计数+=1;
}
printf(“\n计数为%d\n”,i计数);
返回0;
}

由于我的知识水平是。。。好易于理解的 . 谢谢你对那次迭代的帮助,第二次扫描就要把我逼疯了

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int main (void) {
    int icount = 0;
    int c = 0;
    int compare = 0;
    char s[256] = "";
    printf("Enter a string:\n\n");
    while (scanf("%255s", s) == 1) {
       compare = 0;
       for (c = 0 ; s[c] != '\0' ; c++) {
          if (isdigit(s[c])) {
             compare = compare + 1;
          }
       }
       if (compare == strlen(s)) {
          icount = icount + 1;
       }
    }
    printf("%d integers\n", icount);
    return 0;
}
#包括
#包括
#包括
#包括
内部主(空){
int-icount=0;
int c=0;
int比较=0;
字符s[256]=“”;
printf(“输入字符串:\n\n”);
而(扫描频率(“%255s”,s)==1){
比较=0;
对于(c=0;s[c]!='\0';c++){
if(isdigit(s[c])){
比较=比较+1;
}
}
如果(比较==strlen){
icount=icount+1;
}
}
printf(“%d个整数\n”,i计数);
返回0;
}

你为什么要扫描两次,是打字错误吗?我的意思是复制粘贴问题。删除
scanf()
之后的
while(scanf(…)==1){
?我想你还需要在循环中的某个地方将
c
重置为0。不,这不是一个输入错误。我想我必须使用第一个scanf作为我的条件,第二个作为循环中实际发生的情况。你正在跳过第一个
scanf()
ed string,这就是为什么or程序会出现问题,但是你也可以使用比
while(len>0)
更好的方法,比如
for(i=0;s[i]!='\0';++i)
如果我在循环中将c重置为零,那么它不会回头看s[0]数组的一部分,而不是移动到下一个块?看起来它肯定会工作。我用了一些非常简单的东西,虽然我在这一点上对C的知识水平很好..简单。。。