need help excluding capital letters and numbers

Hello,
I wrote the below program to encrypt and decrypt words, words that are in lower case and no numbers or symbols.
My question is how can I add to the code so that it gives an error when capital letters or numbers or symbols are entered?

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
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main ()
{
	string method, map, word, str;
	int encryption, decryption;

	cout<<"What is the method (encryption or decryption)?"<<endl;
	cin>>method;
 
    if (method=="encryption")
    {
		cout<<"What is the translation map (type 'default' to use default):"<<endl;
        cin>>map;
        if (map=="default")
        {
			cout<<"What is the single word to translate:"<<endl;
            cin>>word;

			for(int i = 0; i < word.length(); i++)
				word.at(i) = 'a' - word.at(i) + 122; //'z' == 122
			
            cout<<"encrypted word: "<<word<<endl;                 
		}
		else 
		    cout<<"encryption cannot be performed."<<endl;
	}
    else if (method=="decryption")
    {
        cout<<"What is the translation map (type 'default' to use default):"<<endl;
        cin>>map;
        if (map=="default")
        {
		cout<<"What is the single word to translate:"<<endl;
            cin>>word;

			for(int i = 0; i < word.length(); i++)
				word.at(i) = 'a' - word.at(i) + 122; //'z' == 122
				
			cout<<"decrypted word: "<<word<<endl;
        }
        else
            cout<<"decryption cannot be performed."<<endl;
    }
    else
		cout<<"Error: invalid method choice."<<endl;

	
	return 0;
}
while writing line 25 or 42, you should know how to, just check if chars are between 97 to 122.
does this look right? (line 25)

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
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main ()
{
	string method, map, word, str;
	int encryption, decryption;

	cout<<"What is the method (encryption or decryption)?"<<endl;
	cin>>method;
 
    if (method=="encryption")
    {
		cout<<"What is the translation map (type 'default' to use default):"<<endl;
        cin>>map;
        if (map=="default")
        {
			cout<<"What is the single word to translate:"<<endl;
            cin>>word;
            for(unsigned short i = 0; i < word.size(); i++)
            {
                if((word[i] >= 'a') && (word[i] <= 'z'))
                {
		        	for(int i = 0; i < word.length(); i++)
				        word.at(i) = 'a' - word.at(i) + 122; //'z' == 122
			
            cout<<"encrypted word: "<<word<<endl;                 
                }
                
                else
                cout<<"not valid"<<endl;
            }       
        }
		else 
		    cout<<"encryption cannot be performed."<<endl;
	}
    else if (method=="decryption")
    {
        cout<<"What is the translation map (type 'default' to use default):"<<endl;
        cin>>map;
        if (map=="default")
        {
		cout<<"What is the single word to translate:"<<endl;
            cin>>word;

			for(int i = 0; i < word.length(); i++)
				word.at(i) = 'a' - word.at(i) + 122; //'z' == 122
				
			cout<<"decrypted word: "<<word<<endl;
        }
        else
            cout<<"decryption cannot be performed."<<endl;
    }
    else
		cout<<"Error: invalid method choice."<<endl;

	
	return 0;
}
Last edited on
1
2
3
4
5
6
7
 	std::string word = "hello";
 	for (char x: word)
 	{
 		if (! (97<=x<=122) )
 		std::cout << "bad input"; 		
 	}
 	
Topic archived. No new replies allowed.