Search
 
SCRIPT & CODE EXAMPLE
 

C

how to prevent user from entering char when needing int in c

// return -1 on EOF
int GetPositiveNumber(const char *prompt, const char *reprompt) {
  char buf[100];
  fputs(prompt, stdout);
  fflush(stdout);
  while (fgets(buf, sizeof buf, stdin)) [
    int value;
    if (sscanf(buf, "%d", &value) == 1 && value > 0) {
      return value;
    }
    fputs(reprompt, stdout);
    fflush(stdout);
  }
  return -1;
}

// Usage
int TktAdult = GetPositiveNumber(
    "
Enter amount of adult tickets:" , 
    "
Please enter a positive number!");
if (TktAdult < 0) Handle_End_of_File();
else Success(TktAdult);
Comment

how to prevent user from entering char when needing int in c

#include <stdio.h>
int main()
{
    int num=1;
    int numberRead = 0;
    do{
        printf("Enter a number: ");
        numberRead = scanf("%d",&num);
        while(numberRead != 1){
            printf("That is not a number.
");
           scanf("%*[^
]");  //or fflush(stdin);
            printf("Enter a number: ");
            numberRead = scanf("%d",&num);
        }
        printf("You entered %d
",num);
    }while(num != 0);

    return 0;
}
Comment

how to prevent user from entering char when needing int in c

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

int main()
{
    int i=0, isFloat=0, isChar=0;
    int intNum=0;
    float floatNum=0;
    char str[100];
    printf("Enter a number:");
    scanf("%s",str); // no &
    for(i=0; i<strlen(str); ++i)
    {
        // if not 0-9
        if(!((str[i]>='0')&&(str[i]<='9')))
        {
            if(str[i]=='.')
            {
                isFloat ++;
            }
            else{
                isChar ++;
            }
        }
    }
     if(isChar || isFloat > 1)
     {
         printf("This is not a number.");
     }
     else if(isFloat == 1)
     {
         printf("This is a float.");
         floatNum=atof(str);
         printf("input as float %.2f
",floatNum);
     }
     else {
         printf("This is a valid int.
");
         intNum=atoi(str);
         printf("input as int %d
",intNum);
     }
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
C :: rl_replace_line 
C :: c print size_t 
C :: restart nginx in alpine linix 
C :: find power of a number in c 
C :: %hd c 
C :: see if two strings are equal in C 
C :: take array as input in c 
C :: fonction recursive successeur nombre chaine de caractere en c 
C :: add 2 numbers in c 
C :: c 2d array dimensions 
C :: 0/1 knapsack problem in c 
C :: c bit access union 
C :: how to sleep in c 
C :: tkinter create_line 
C :: why do we need return 0 in c? 
C :: what is system function in c 
C :: multiplication table in c using array 
C :: c int 
C :: c style string 
C :: celsius to fahrenheit formula 
C :: search in gz file 
C :: responsive form bootstrap 4 
C :: c for 
C :: linked list using c 
C :: Installing Tailwind 
C :: terraform fargate cpu 
C :: delay in c programming for linux 
C :: print in c 
C :: nested while loop in c 
C :: Increment & Decrement Operator in C language 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =