#strof() errno seems not to be setting

5 messages · Page 1 of 1 (latest)

steady pineBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

devout oxide
#

Complete code..

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<math.h>

#define MAX_NUMBERS 100
#define INPUT_SIZE 20
int main(int argc, char *argv[argc + 1]){
        float nInput[MAX_NUMBERS] = {};
        char input[INPUT_SIZE];
        int nInputCounter = 0;
        float maxNum;

        printf("Enter a positive number(Enter 0 to terminate)\n");
        printf("[DEBUG] HUGE_VAL:%f,HUGE_VALF:%f, HUGE_VALL:%Lf\n",
                        HUGE_VAL,
                        HUGE_VALF,
                        HUGE_VALL);
        for(int i = 0; i < MAX_NUMBERS; i++){
                char *endptr;
                memset(input, '\0', INPUT_SIZE);
                printf("Enter a number: ");
                fgets(input, INPUT_SIZE, stdin);
                errno = 0;
                nInput[i] = strtof(input, &endptr);
                if(input == endptr){
                        fprintf(stderr, "No input, try again.\n");
                        continue;
                }
                if(errno != 0 || isinf(nInput[i]) != 0){
                        perror("Invalid input, try again.");
                        continue;
                }
                if(nInput < 0){
                        fprintf(stderr, "Invalid input, try again.");
                        continue;
                }
                if(nInput[i] == 0){
                        printf("Number of entered numbers: %d\n", nInputCounter);
                        break;
                }
                nInputCounter++;
        }

        if(nInputCounter == 0){
                printf("No numbers entered\n");
                return EXIT_SUCCESS;
        }

        maxNum = nInput[0];
        for(int i = 1; i < nInputCounter; i++){
                if(maxNum < nInput[i]){
                        maxNum = nInput[i];
                        continue;
                }
        }

        printf("The largest number entered was: %f\n", maxNum);
        return EXIT_SUCCESS;
}
steady pineBOT
#

@devout oxide

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

devout oxide
#

!solved

steady pineBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity