program that accept only characters

Can someone help me? How to make this in order to accept only a character. For example the program ask to enter a name and I input number and it will display an error message and ask again to enter a name. And about the y/n, how to make it will accept only "y" and "n" or "Y" and "N", no other letter or number. Otherwise, it will display an error message and will ask again y/n. Lastly, is i can't type names with space, it got and error. How to fix this?

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//This codes is all about flames
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
int main()
{
    int x,y,z,i,j,r,s;
    char choice, boy[22],girl[22],a[11]={' ','f','l','a','m','e','s'},b[11];

    do
    {
        //input the names
        cout<<"\nEnter the name of the boy: \n"<<endl;
        cin>>boy;
        cout<<"\nEnter the name of the girl: \n"<<endl;
        cin>>girl;
        x=strlen(boy);
        y=strlen(girl);
        z=x+y;

        //eliminating the letters of the two string
        for(i=0; i<x; i++)
        {
            for(j=0; j<y; j++)
            {
                if(boy[i]==girl[j])        //for comparing the names
                {
                    boy[i]=girl[j]=' ';
                    z=z-2;
                    break;
                }
            }
        }

        //eliminating the letters of FLAMEs
        for(r=6;r>1;r--)
        {
            s=z%r;

            if(s==0)
            {
                s=r;
                i=1;
            }
            else
            {
                a[s]='\0';
                i=s+1;
            }
            j=1;
            while(1)
            {
                if(i==s)
                {
                    break;
                }
                b[j]=a[i];
                if(i==r)
                {
                    i=0;
                }
                i++;
                j++;
            }
            for(i=1;i<=r-1;i++)
            {
                a[i]=b[i];
            }
        }

        //the result
        cout<<"\nRelationship status: \n"<<endl;
        switch(a[1])
        {
        case 'f':
            cout<<"FRIENDSHIP";
            break;
        case 'l':
            cout<<"LOVE";
            break;
        case 'a':
            cout<<"AFFECTION";
            break;
        case 'm':
            cout<<"MARRIAGE";
            break;
        case 'e':
            cout<<"ENEMY";
            break;
        case 's':
            cout<<"SIBLINGS";
            break;
        }
        cout<<endl<< "\nDo you wish to repeat the game?(y/n): \n"<<endl;
        cin>>choice;
        system("CLS");
        if(choice=='N' || choice== 'n')
        {
            cout<< "\t\tThank you for trying! Good bye! \n"<<endl;
        }
    }
    while(choice=='y' || choice=='Y');
return 0;
}
Last edited on
you can iterate over your c-string checking isalpha() for each letter, if that if false, the string is not valid.

<< skips whitespace. use getline to allow spaces.
Last edited on
Here is the last part of your code, with the control feature you mentioned.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
		do {
			cout << endl << "\nDo you wish to repeat the game?(y/n): \n" << endl;
			cin >> choice;
			system("CLS");
			if (choice == 'N' || choice == 'n')
			{
				cout << "\t\tThank you for trying! Good bye! \n" << endl;
			}
			else if (choice != 'Y' && choice != 'y') { cout << "Not a valid choice." << endl; }
		} while (choice != 'Y' && choice != 'y' && choice != 'N' && choice != 'n');
	} while (choice == 'y' || choice == 'Y');

	cin.ignore();
	cin.get();

	return 0;
}
thanks it solve the problem but 2 problem left is when i enter: ex. mary joy it got an error.

i tried puting
1
2
cout<<"\nEnter the name of the boy: \n"<<endl;
getline(cin,boy);

and i got an error

other problem is when i type a number it continue to calculate. i want; when the user ask to enter the name it must be a names, if he input numbers it will display an error messege and ask to input again a name.
Last edited on
To fix the first problem, you will need to do a lot of code rewrite. Switch from char arrays to strings. Study strings and how to work with them a bit first.
Topic archived. No new replies allowed.