【问题标题】:fgets wants input twicefgets 想要输入两次
【发布时间】:2020-10-25 14:08:50
【问题描述】:

我对 C 语言还是很陌生,并且在如何缩进代码方面确实存在问题,所以我提前为此道歉。我试图让程序输入字符串并输出可读性分数。我已经把代码全部写出来了,但是,每当输入一个阶段并按下回车键时,程序不会执行其余的代码,而是我必须在继续之前输入其他内容。我不知道为什么!请问有人可以帮忙吗?

#include <string.h>
#include <stdlib.h>
#include <math.h>
 
 
 
 
int main(void)
{
    char str[500]; // think of this like an array
    int alphabet =0 , i =0, word=0, special_characters =0, vowels = 0;
   
   
       printf("Input the string:\n");
       fgets(str,500,stdin);   // <== issue here where I  have to input string twice for while loop to occur!
       
 
 
     /* Checks each character of string*/
   
    while(str[i] !='\0')// while there is a character and not empty space
    {
       
   
   
        if(str[i] ==  'a' || str[i] == 'e' || str[i] == 'i' ||
            str[i] == 'o' || str[i] == 'u' || str[i] == 'A' ||
            str[i] == 'E' || str[i] == 'I' || str[i] == 'O' ||
            str[i] == 'U')
        {
            ++vowels;
           
        }
     
        else if((str[i] ==' ' || str[i]=='\n' || str[i]=='\t'))
        { // if there is not space, tab or enter, then logically there is a word
            word++; // after not finding a space it is told to look for next one
        }
        else if((str[i] =='?' || str[i]=='!' || str[i]=='.')) // looks for sentance enders
        {
            special_characters++; // keeps going until text ends
        }
        else if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
        {
        // only  i is just a counter of each letter in
        // this case. alphabet is told to increase
 
            alphabet++; // made this a loop that goes onto next letter
           
 
        }
 
     
         i++; //  tells i to scan over ALL if loops by being INSDIE the while loop but not
         // part of the IF statements!
         
    }    
   
        //L = Letters ÷ Words × 100 = 639 ÷ 119 × 100 ? 537
 
        //S = Sentences ÷ Words × 100 = 5 ÷ 119 × 100 ? 4.20
                   
        double letters = vowels + alphabet;
        double w = word;
        double d = 100.0;
        double L = (letters/w)*d;
        double e = special_characters;  // sentances
        double S= (e/w)*100;
        scanf("%lf",&S);
        double index = (0.0588 * L) -(0.296 * S)- 15.8;
        scanf("%lf",&index);
        double round(double index);
                 
 
 
       
        if(index <=1)
        {
            printf("Below Grade 1\n");
        }
        else if(index >=3 && index <4)
        {
            printf(" Grade 3\n");
           
        }
        else if(index >=5 && index <6)
        {
            printf(" Grade 5\n");
           
        }
        else if(index >=7 && index <8)
        {
            printf(" Grade 7\n");
           
        }
        else if(index >=8 && index <9)
        {
            printf(" Grade 8\n");
           
        }
        else if(index >=9 && index <10)
        {
            printf(" Grade 9\n");
           
        }
        else if(index >=10 && index <11)
        {
            printf(" Grade 9\n");
           
        }
        else if(index >=16)
        {
            printf(" Grade 16+\n");
           
        }
       
 
 
 
}


【问题讨论】:

标签: c input


【解决方案1】:

您的问题可能在scanf() 上更进一步

试试

        // ...
        double S= (e/w)*100;
        printf("Enter value for S:\n");   // suggestion for feedback
        if (scanf("%lf", &S) != 1) {
            //scanf failed
            fprintf(stderr, "scanf failed.\n");
            exit(EXIT_FAILURE);
        }
        //...

【讨论】:

  • 非常感谢!为什么这会导致问题?
  • fgets() 确实没有问题,但是由于您的程序在输入输入和请​​求 S 之间没有输出,因此在您看来它已停止...实际上它已执行所有语句,并愉快地等待 S。打印提示的建议只是向用户提供反馈。
  • 没错,@sirneil 只是在 while 循环后面放了一个 print 语句,通过正常输入来执行你的代码,你就会明白了。
猜你喜欢
  • 2020-03-21
  • 1970-01-01
  • 1970-01-01
  • 2015-05-23
  • 2021-12-19
  • 1970-01-01
  • 2020-07-24
  • 2017-10-13
  • 1970-01-01
相关资源
最近更新 更多