Validate user input

Hi all,

I am new to the forum. I am trying to validate user input to be between 1 to 100. Anything lower or higher will ask user to key in number again. It works fine with numbers, however, if user input alphabet, it keeps looping endlessly. Below is my code, kindly help to take a look and suggest where it went wrong and how i can fix it. Thanks a lot indeed.

#include <stdio.h>
int main()
{
float number;
do
{
printf("Please key in number (between 1 to 100) : ");
number = 0;
scanf("%f", &number);
printf("Amount you key is:%0.2f\n", number);
}while(!(number>= 1 && number<= 100));
printf("Valid number\n");
return 0;
}
Maybe you could add a check using isdigit.

http://www.cplusplus.com/reference/clibrary/cctype/isdigit/
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
  float number = 0.0;
  while(number < 1 || number > 100)
  {
    cout << "Please key in number (between 1 to 100) : ";
    cin >> number;
    if(number >= 1 && number <= 100) break;
    cout << endl << "Invalid number.  You entered: " << fixed << number << endl;
  } 
  cout << "Valid number" << endl; 
  return 0;
}
@OP: You have a syntax error on line one, the correct way to include a C header is by putting two forward slashes before the hash (#).
Last edited on
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>

int main(void)
{
    float number;
    do
    {
        printf("Please key in number (between 1 to 100) : ");
        number = 0;
        if(scanf("%f", &number) == 0 )
        {
            printf("Input was not valid\n");
            int   ch;
            while ((ch = getchar()) != '\n' && ch != EOF);
        }
        else
        {
            printf("Amount you key is:%0.2f\n", number);
        }
    
    }while(!(number>= 1 && number<= 100));
    
    printf("Valid number\n"); 
    return 0;
}

@OP: You have a syntax error on line one, the correct way to include a C header is by putting two forward slashes before the hash (#).


what do you mean by two forward slashes? and what do you mean by syntax error (in line 1)?
The very first line reads:
#include <stdio.h>

But really, there should be two forward slashes before the hash (#) because it is a C header.
@Grey Wolf: when i run your code, i encountered "Error 2 error C2143: syntax error : missing ';' before 'type' (line14)."

It is referring to "int ch;" .

It is just a declaration, and looks ok to me. But i don't know why it shows error to me. Any idea?

Btw, I am using Microsoft visual c++ 2010 express. Thank you.


If you use a C++ compiler, write C++ code! It's sure easier than C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std; //if you want

int main()
{
    cout << "Enter a value in the range (0, 100]: ";
    int v;
    while((cin >> v), (v > 100 || v <= 0))
    {
        cin.clear();
        cout << "Try again: ";
    }
    cout << "You entered a valid value of " << v << endl;
    cin.sync(); cin.ignore(cin::size_type(-1), '\n'); //hold console open
}
@LB: so it should be written like this?
 
//#include <stdio.h> 

like that? wasn't it a comment?
@chipp: yes that is what I meant.
closed account (z05DSL3A)
c beginner1986 wrote:
@Grey Wolf: when i run your code, i encountered "Error 2 error C2143: syntax error : missing ';' before 'type' (line14)."
I suppose that if you have your compiler set to pedantic then is should be:
1
2
3
4
5
...
int   ch;
printf("Input was not valid\n");
while ((ch = getchar()) != '\n' && ch != EOF);
...


hi Grey Wolf: can you kindly help to guide me how to set my compiler to pedantic. Thank you.
@chipp: yes that is what I meant.

i didn't get the point, why we should comment the include line ?
Chipp, because it is a C header.
Topic archived. No new replies allowed.