| | |
chars and floats
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
btw ..Here is a simpler program that throws away the error characters:
There is nothing magic about the number "10000." Just choose a large number. There is actually a maximum-sized number for purposes like this that is built into the C++ standard
C Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int x = 0; do { cout << "Enter a number (-1 = quit): "; if(!(cin >> x)) { cout << "Please enter numeric characters only." << endl; cin.clear(); cin.ignore(10000,'\n'); } else if(x != -1) { cout << "You entered " << x << endl; // or goto any line of the code that u want // } } while(x != -1); cout << "Quitting program." << endl; return 0; }
Real Eyes Realize Real Lies
My Resume
My Resume
yeah ,Occasionally you may need a routine in on of your programs that restricts input. An example required all numeric input, no commas or other funny characters. There are really no C routines to deal with this, so you must concoct your own. As a foundation, you can use the INPUT.C program introduced here
In this example, u needed a routine to input a numerical value. The scanf function could not be used; if the user typed in a number such as 40,000, then scanf would interpret that as two values, 40 and zero. The gets function could read input, but the comma again would cause only the value 40 to be read. The best solution would be to write a bulletproof input routine that would do several things:
Restrict the user from typing in more characters than necessary (in this case, only 5 digits)
Ensure that only numbers zero through 9 could be entered.
Allow the user the ability to backspace and erase as well as potentially cancel input.
Each of these conditions can easily be met with a modified input function,
The following program shows how such a routine could be written. Pay attention to the input function and the slew of case statements toward the end.
Type the above source code into your editor.
Normally, the input function would allow any text to be entered. This is done by including the following snippet of code as the default:
However, in this program, that snippet is used only when the characters '0' through '9' are entered. The default condition - which holds true for all other characters - is merely a break statement. This means that only the numbers zero through 9 are allowed as input, and whatever string is modified by the input routine will be a number with no nonsense.
Similar modifications to the standard input routine can allow further restrictions. For example, you could modify the program to accept only uppercase characters. (In that case an isupper test would have to be made before input was examined.) Or you can restrict input to vowels or letters common to both Roman and Greek typefaces. It just requires a little deft manipulation and your very own input routine.
In this example, u needed a routine to input a numerical value. The scanf function could not be used; if the user typed in a number such as 40,000, then scanf would interpret that as two values, 40 and zero. The gets function could read input, but the comma again would cause only the value 40 to be read. The best solution would be to write a bulletproof input routine that would do several things:
Restrict the user from typing in more characters than necessary (in this case, only 5 digits)
Ensure that only numbers zero through 9 could be entered.
Allow the user the ability to backspace and erase as well as potentially cancel input.
Each of these conditions can easily be met with a modified input function,
The following program shows how such a routine could be written. Pay attention to the input function and the slew of case statements toward the end.
C Syntax (Toggle Plain Text)
Name: S_INPUT.C #include <stdio.h> #include <conio.h> #include <stdlib.h> #define CR 0x0d //carriage return #define LF 0x0a //Line feed #define BACKSPACE 0x08 #define NULL 0 //Empty character #define TRUE 1 #define FALSE 0 void input(char *string, int length); void main() { char string[6]; long value; printf("Enter a 5-digit value: "); input(string,5); value = atol(string); printf("\nYou entered %ld\n",value); } /* The customized input() routine */ void input(char *string, int length) { int done = FALSE; int index = 0; char ch; string[0] = NULL; // init the buffer do { ch = getch(); /* Check to see if the buffer is full */ if (index == length) { switch(ch) { case CR: break; case BACKSPACE: break; default: ch = NULL; break; } } /* process the keyboard input */ switch(ch) { case CR: //Enter key putchar(ch); putchar(LF); string[index] = NULL; done = TRUE; break; case BACKSPACE: if(index==0) { break; } else { putchar(ch); putchar(' '); putchar(ch); index--; break; } case NULL: break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': putchar(ch); string[index] = ch; index++; break; default: break; } } while(!done); }
Normally, the input function would allow any text to be entered. This is done by including the following snippet of code as the default:
C Syntax (Toggle Plain Text)
putchar(ch); string[index] = ch; index++; break;
Similar modifications to the standard input routine can allow further restrictions. For example, you could modify the program to accept only uppercase characters. (In that case an isupper test would have to be made before input was examined.) Or you can restrict input to vowels or letters common to both Roman and Greek typefaces. It just requires a little deft manipulation and your very own input routine.
Real Eyes Realize Real Lies
My Resume
My Resume
•
•
•
•
Originally Posted by sroberts82
sorry, I mean in C.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Use of getc()/putc() for reading/writing chars, floats, etc. from binary files? (C++)
- Problem with memory allocation =( (C)
Other Threads in the C Forum
- Previous Thread: Locking the mouse button down
- Next Thread: AnsiString to Const Char*
| Thread Tools | Search this Thread |
Tag cloud for C
#include ansi array arrays asterisks binarysearch calculate centimeter changingto char convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi






