0

I'm having a problem with a c code, that is testing triangles. I'd like to restrict the input at the first question to a number from 1 - 255. That works so far. But i also want to include a error if the input is wrong.

At the part where the user is asked to type a first, second and third number i'd like it to accept only 0.0 < FLT_MAX. Again i'd like it to prompt an error at wrong input, and then ask again for the number.

Any help would be appreciated.

Thanks,

Dommas

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define ROW 255
#define COLUMN 3
#define B 0
#define COL 0

int count;
int x = 0;
int a = 0;
double zahl[ROW][COLUMN];

int main() 
{
memset(zahl, 0, sizeof(zahl[0][0]) * ROW * COLUMN);

char input[255];
int first = 0;

//read input
do
{
    printf("Please enter the number of triangles to check: ");
    if(fgets(input, sizeof(input), stdin) == NULL)
    {
        return 0;
    }
    first = atof(input);
} while(first < 1 || first > ROW);


for (count = 0; count < first; count++) {
    printf("Please enter the first number of the triplet: ");
    scanf("%lf", &zahl[x][COL]);
    printf("Please enter the second number of the triplet: ");
    scanf("%lf", &zahl[x][COL+1]);
    printf("Please enter the third number of the triplet: ");
    scanf("%lf", &zahl[x][COL+2]);
    x++;
}
}
Dommas
  • 1
  • Define "input is wrong". Maybe [this](https://stackoverflow.com/questions/47073328/c-need-an-alternative-for-fflush/47073918#47073918) helps. And what prevents you from printing an error message if the number does not meet your requirement? – Jabberwocky Nov 02 '17 at 16:35
  • At the first input everything that's not an positive integer from 1 - 255. – Dommas Nov 02 '17 at 16:37
  • At the second, third and fourth, everything else than a double from 1 - FLT_MAX is wrong. – Dommas Nov 02 '17 at 16:38
  • Well, write the code. – Jabberwocky Nov 02 '17 at 16:38
  • Pseudo code: if input does not satify requirement (e.g not in 1..255) then print an error message and ask again. – Jabberwocky Nov 02 '17 at 16:40
  • Well as i am pretty much a beginner in c programming, you can probably imagine what problems i am facing in terms of understanding why it doesn't work or how i can make it work. I already tried a lot of things and i cant' get it to work. A more specific answer to this problem would be very kind. – Dommas Nov 02 '17 at 16:41
  • After `first = atof(input);` add code `if (first < 1 || first > ROW) { puts("error at wrong input"); }` Did you author the posted code? – chux - Reinstate Monica Nov 02 '17 at 17:10
  • Well. Looks like you're doing everything right except printing the error message. Which is confusing. Because you're using `if` statements correctly, and you're using`printf` correctly. So if you can write this code, you can check a condition and print a message. – MFisherKDX Nov 02 '17 at 17:14
  • Thats what i tried, and yes it is indeed my code. @chux What it does is fine till i enter something like '1f1f1f1f1f1f1f1f' If i do that a error gets printed, but it seems to accept a number and goes on to the other inputs below. Any idea how i can implement the validation from above to the scans on the double numbers? – Dommas Nov 02 '17 at 17:41
  • Research and use `strtof()` / `strtol()` rather than `atof()` or `atoi()`. It provides error checking. Drop `scanf()`. Use `fgets()` for all user input. – chux - Reinstate Monica Nov 02 '17 at 17:52

0 Answers0