How to design a function that can check name and number whether it is valid or not? If the name contains symbol and/or number while number contains alphabet and/or symbol, it will prompt the user to key in again the valid input.
How to design a function that can check name and number whether it is valid or not? If the name contains symbol and/or number while number contains alphabet and/or symbol, it will prompt the user to key in again the valid input.
Last edited by ulti-killer; 06-12-2012 at 03:17 AM.
A loop, and various calls to the likes of isdigit() et al in ctype.h
Code:char buff[BUFSIZ]; while ( fgets( buff, BUFSIZ, stdin ) != NULL ) { if ( validate(buff) ) { // do your thing } else { // complain } }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Why it doesn't work? It shows 2 error about error C1075: end of file found before the left brace '{' and IntelliSense: argument of type "char *" is incompatible with parameter of type "int". The name variable in line 19 has red underline.
Code:#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> // Function main begins program execution int main(void) { char name [50], *line; // Prompt and read the user's na5me printf("Name: "); // Remove newline line = strchr(name,'\n'); *line = '\0'; fflush(stdin); while ( fgets(name, sizeof(name), stdin ) != NULL ) { if ( isalpha (name) ) { printf("Name: %s", name); } else { printf("Enter name again"); // Prompt and read the user's name printf("Name: "); // Remove newline line = strchr(name,'\n'); *line = '\0'; fflush(stdin); } system ("pause"); }
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
Why fflush(stdin) is wrongCode:fflush(stdin);