tags:

views:

226

answers:

4

Hello,

I have a simple problem;

Here is the code :

#include<stdio.h>
main(){
 int input;
 printf("Choose a numeric value");
 scanf("%d",&input);
}

I want the user to only enter numbers ... So it has to be something like this :

#include<stdio.h>
main(){
 int input;
 printf("Choose a numeric value");
 do{
   scanf("%d",&input);
 }while(input!= 'something');
}

My problem is that I dont know what to replace in 'something' ... How can I prevent users from inputting alphabetic characters ?

EDIT

I just got something interesting :

#include<stdio.h>
main(){
 int input;
 printf("Choose a numeric value");
 do{
   scanf("%d",&input);
 }while(input!= 'int');
}

Adding 'int' will keep looping as long as I enter numbers, I tried 'char' but that didnt work .. surely there is something for alphabets right ? :S Please reply !

Thanks for your help !

+4  A: 

You can't prevent the user from entering anything he wants -- you can only ignore anything s/he enters that you don't "like".

A typical pattern is to read a string with fgets, then scan through the string and check that all the input was digits with isdigit. If it was all digits, then convert to an integer; otherwise, throw it away and get the input again.

Alternatively, use strtol to do the conversion. It sets a pointer to the end of the data it could convert to a number; in this case you (apparently) want it to point to the end of the string.

If you don't mind some non-portable code, you can read one character at a time, and throw away anything but digits (e.g. with getch on Windows).

Jerry Coffin
I agree with your statement. But how do I implement that code in mine ? Do I use a do-while loop as well ? Sorry im not an expert in C :(
ZaZu
Yes, it's usually a do-while loop. `do { read(data); } while (!all_digits(data)); int value = convert(data);`.
Jerry Coffin
Thanks for the reply. These commands are a bit too advanced for me xD This could be an easy way that you told me, but I just dont know how they work.Thank you anyway, I am trying to find a way by manipulating ACSII variables .. Wonder if that would work ?
ZaZu
I have updated my original post, ascii manipulation works.
ZaZu
*"You can't prevent the user from entering anything he wants ..."* - sure you can ... with a suitably modified keyboard :-)
Stephen C
+1  A: 

You should not use scanf to read in numbers - see http://www.gidnetwork.com/b-63.html

Use fgets instead.

However, if you must use scanf, you can do this:

  #include <stdio.h>
  int main() {
      char text[20];
      fputs("enter some number: ", stdout);
      fflush(stdout);
      if ( fgets(text, sizeof text, stdin) ) {
          int number;
          if ( sscanf(text, "%d", &number) == 1 ) {
              printf("number = %d\n", number);
          }
      }
      return 0;
  }
DVK
Thanks for the informative reply..How about using some kind of ACSII manipulation to prevent alphabetic characters ?
ZaZu
+2  A: 

The strtol library function will convert a string representation of a number to its equivalent integer value, and will also set a pointer to the first character that does not match a valid number.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
...
int value;
char buffer[SOME_SIZE];
char *chk;
do 
{
  printf("Enter a number: ");
  fflush(stdout);
  if (fgets(buffer, sizeof buffer, stdin) != NULL)
  {
    value = (int) strtol(buffer, &chk, 10); /* assume decimal format */
  }
} while (!isspace(*chk) && *chk != 0);

If chk points to something other than whitespace or a string terminator (0), then the string was not a valid integer constant. For floating-point input, use strtod.

John Bode
Thank you very much for taking your time and answering.Pretty advanced commands used there, ill research about them to understand more :)
ZaZu
A: 

Personally, I would read the input into a buffer and scan that string for my number.

char buffer[100];
float value;
do {
scanf("%s", buffer);
} while ( sscanf(buffer,"%f", &value) != 1 )

This will loop until the first thing the user enters on the line is a number. The input could be anything but will only get past this block when the first thing entered is a number.
example input:
43289 (value is 43289)
43.33 (value is 43.44)
392gifah (value is 392)
ajfgds432 (continues to loop)

thelaws