-1

I'm working on my first simple C program in Visual Studios 2017. The goal is to get two user inputs which have to be integers.

I go through the array and check if every character is an integer but it still returns "The input is not a valid number". Also after retrying 3 times the console crashes.

"projectExample.exe has triggered a breakpoint. occurred" is the displayed message.

int main()
{
    char userInputM[10];
    char userInputN[10];

    //get the value of M from the user
    printf("Enter a value for M: ");
    fgets(userInputM, sizeof(userInputM), stdin);
    //printf(userInputM);

    //check that the entered value for M is a valid positive number
    const int lenOfInput1 = strlen(userInputM);
    for (int i = 0; lenOfInput1; i++) {

        if (!isdigit(userInputM[i])) {
            printf("The input is not a valid number. Try again");

            printf("Enter a value for M: ");
            fgets(userInputM, sizeof(userInputM), stdin);
            printf(userInputM);
        }
    }

    //check that the entered value for N is a number 

    //convert the user input for M to an int for calculation
    //int factorM = atoi(userInputM);
    //printf("%d", factorM);

    //int result = calculate();
    //int a;
    //scanf("%d", &a);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    Don't forget that `fgets()` will include the newline, and newline is not a digit. Also, don't forget that recalcitrant users might type some blanks or tabs before and/or after the number. You might want to allow that as valid, but not if there are spaces in the number. Inside your loop, you need to re-measure the length of the input after getting more data — you need a `lenOfInput1 = strlen(userInputM);` after the embedded `fgets()`. The loop condition `for (int i = 0; lenOfInput1; i++)` should probably be `for (int i = 0; i < lenOfInput1; i++)` — or you might need to subtract one for `\n`. – Jonathan Leffler Sep 27 '17 at 23:41
  • 1
    `size_t len; if (!fgets(userInputM, sizeof(userInputM), stdin)) {/*handle error*/} len = strlen(userInputM); if (len && userInputM[len-1] == '\n') userInputM[--len] = 0;` (don't forget `#include ` and do the same for both inputs) – David C. Rankin Sep 27 '17 at 23:43

2 Answers2

0

If you need to make sure that the number is an integer, you could read in the input into an int with scanf() and check the return value.

int M, rv=-1;
while(rv!=1)
{
    if(rv==0)
    {
        getchar();
    }
    rv=scanf("%d", &M);
}

scanf() would return the number of variables to which values were assigned successfully which will be 1 if the above scanf() is successful.

If the input was not a number and hence the scanf() returns 0, the entered value, which is not a number, is still left unconsumed in the input buffer and that needs to be consumed. It could be done with a getchar(). Otherwise, scanf() will keep trying to read the same value and rv will be 0 each time.

In this way, you don't need a string buffer or a need to use atoi().

Have a look here as well.


As pointed out in the comments, fgets() will read in the trailing new line (\n) into the string and \n is not a digit.

You could replace the \n with \0 like

str[strlen(str)-1]='\0';
J...S
  • 5,079
  • 1
  • 20
  • 35
  • I was trying to avoid using scanf() because Visual Studios complaints that its not safe but I'll change the preprocessor definition and try it. – Diego Duarte Sep 28 '17 at 01:30
0
int count = 0;
int i = 0;
while (userInputM[i])
{
    if (userInputM[i] >= '0' && userInputM[i] <= '9') //if it's an integer
     {
        ++count;  //do something like creating new array and put the
                  //data inside or in my case I count the # of ints
        ++i;
      }    
    else
        ++i;
}
josemartindev
  • 1,413
  • 9
  • 17