I have an idea..but i am not sure if it is the best solution..I thought if you dont need any spaces for the user to enter his/her name and surname.You can test each of the pressed keys with standart character functions like isalpha() so you are sure entered character is alphabetic and if it is alphabetic you write it on screen and save it.Otherwise you do nothing..Another solution is simple but not pretty..You can read all the line to a string variable and then in a for loop or with the help of a standart function like find_first_not_of you can search for the first character that is not alphabetic and if you find such a character you either terminate the program or reset the string and ask again..First solution is prettier but i couldn't manage to provide an example because i could not control console output everything that i write number or not displays in console output.but cin.get() function with combination isalpha() works pretty well but since i could not control the output using string class to read whole line is a better idea..But if you want to go C-Style here is your code:
#include <cstdio>
#include <cctype>
int main ()
{
char c;
char name[20]={};
int i = 0;
puts ("Enter yourname");
do {
c=getchar();
if(isalpha(c))
name[i++]=c;
} while (c!='\n');
puts(name);
return 0;
}
only the alphabetic characters will be read into character array other characters is just ignored here..
|