c - 在 C 中使用 getchar()

标签 c getchar

在 C 中使用 getchar() 时,您将获得输入字符的 ascii 值。如果您需要循环使用 getchar() 输入的值并将输入的数字(包括“.”)转换为 float ,如何转换该值并根据用户想要输入的值的数量在多行中读取?到目前为止,我设法通过使用带有计数器的 while 循环来让用户输入检查他们想要将多少个值转换为浮点。我的问题是使用内部循环将 getchar() 中的值一次转换为浮点型,其中数字最终被格式化为浮点型。有什么想法吗?

char input;
float value;
int count = 0;
int i = 0;
scanf("%i", count)
while(i < count)
{
    input = getchar();
    while(input != '\n')
    {
        input = input - '0';
        printf("%d",value);
    }
    i++;
}

读入后我要么陷入无限循环,要么陷入 0.0 值。有什么想法吗?

最佳答案

根据评论中提到的实际问题,我已经编辑了代码并根据您描述的内容编写了它。

#include<stdio.h>

void main()
{
    int count = 0;
    int i = 0;
    int count_digits=0;
    char output[50];        //For storing the input
    scanf("%d",&count);
    while(i < count)
    {
        printf("\n\nInput:%d       ",i+1);     //For printing the input number.
        scanf("%s",&output);            //taking input as a String

        int char_no=0;                  //character no pointer
        count_digits=0;                 //for counting number of characters (in our case : Digits) before decimal points which is then to be printed after decimal points

        while(output[char_no]!='\0' && output[char_no]!='.')  //printing all digits before decimal point or print whole number if it doesn't contain decimal point
        {
            printf("%c",output[char_no]);     //printing character(Digit)
            char_no++;                  //incrementing character number
            count_digits++;             //Counting total number of digits before decimal point
        }



        if(output[char_no]!='\0')       //If it is not the end of character array means number contains decimal point
        {
            printf("%c",output[char_no]);   //printing decimal point as a character
            char_no++;                  //Incrementing character pointer
        }
        else
            printf(".");               //If number doesn't contain decimal point it will print it.

        while(count_digits>0 && output[char_no]!='\0')  //loop will run till no of digits after decimal point gets printed or reaching at the end of input
        {
            printf("%c",output[char_no]);   //printing the character as it is
            char_no++;              //incrementing character number
            count_digits--;       //decrementing total no of digits to be printed after decimal point as one character gets printed.
        }

        while(count_digits>0)    //if input ends but still the no of digits after decimal points are less than the no. of digits before decimal points, It will print zeros to make no.of digits after and before decimal points equal
        {
            printf("0");        //printing 0
            count_digits--;     //decrementing total no of digits to be printed after decimal point as one character gets printed.
        }
        i++;
    }
}
上述的

示例输出是: Output 希望这能解决您的问题。

关于c - 在 C 中使用 getchar(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39323290/

相关文章:

c - getchar() 和计算 C 中的句子和单词

c - getchar_unlocked() 实际上是如何工作的?

c - 如何跳过读取 fscanf 中的内容

c - 仅公开 header 中一小部分函数

c - 聚合类型不完整,无法定义

c - getchar() 在 C 中给出输出

c - 无法从 C 中的 getchar 获得正确的变量赋值

无法比较 getchar != '/n' ,收到警告 : multi-character character constant

c - `strcpy(x+1, SEQX)` 是做什么的?

c++ - 一道面试题