简体   繁体   English

为什么我没有收到 output?

[英]Why I am getting no output?

I have written this code to convert a string into float number (IMPLEMENTED MY OWN ATOF() function in c)我已经编写了这段代码来将字符串转换为浮点数(在 c 中实现了我自己的 ATOF() function)

the output should be this: output 应该是这样的:

Enter A number: 
6
your number is : 6.00

CORRECTED: but I get this.更正:但我明白了。

 Enter A number: 
    6
Press enter to continue:

I am getting this output:我得到这个 output:

 Enter A number: 
        6
your number is: 0.00
Press enter to continue:

The following is the code that I wrote.以下是我编写的代码。 I feel like I have missed something with the pointer or might have made mistake in the calculation.我觉得我错过了指针的某些内容,或者可能在计算中出错了。 But as for as I see, everything seems good for me but why it is giving me 0.00?但就我所见,一切对我来说似乎都很好,但为什么它给了我 0.00?

#include <stdio.h>
#include <stdlib.h>
#define SIZE 250

float myAtof(char *string, char *error);

int main()
{
    char string[SIZE];            // Array declaration.
    float fnum1;
    char errorState=0;

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

     fnum1=myAtof(string,&errorState);
   
   if (errorState==0){
        printf("Your number is: %.2f \n", fnum1);
    }
    else if (errorState==1){
        printf("Error has been occurred due to inappropriate input!\n");
    }

    return 0;
}

float myAtof(char* string, char* error){          // Function to convert string into float.

     *error != '1';

     float num=  0;
     float decimalFlag = 1;
     int decimalNumberFound = 0;



    for(int i = 0; i< strlen(string); i++){
        char ch = string[i];
       


if(ch != '.' && (ch < '0' || ch >'9'))
        {
            *error ='1';
            return 0;
            }
        if( decimalNumberFound == 1 && ch == '.')
        {
            *error = '1';
            return 0;

            }
        if(ch = '.')
        {
            decimalNumberFound = 1;

            }

        else {
            num = num * 10 + (ch - '0');

            if(decimalNumberFound ==1)

                decimalFlag = decimalFlag * 10;

            }
    }

        num = num / decimalFlag;

        return num;

}

I will not tell you where the error is, but remember never to code "this will not happen".我不会告诉你错误在哪里,但请记住永远不要编写“这不会发生”的代码。 Here, you do:在这里,您可以:

if (errorState==0) {
    printf("Your number is: %.2f \n", fnum1);
} else if (errorState==1) {
    printf("Error has been occurred due to inappropriate input!\n");
}

You just said, " it will never happen that errorState is neither 0 nor 1".你刚才说,“永远不会发生errorState 既不是 0 也不是 1”。

Try rather:试试吧:

if (errorState==0) {
    printf("Your number is: %.2f \n", fnum1);
} else if (errorState==1) {
    printf("Error has been occurred due to inappropriate input!\n");
} else {
    printf("Rats! errorCode is %d! This cannot be happening!\n", (int)errorCode);
}

so that whatever happens , you should get some input.这样无论发生什么,您都应该得到一些输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM