Caeser Cipher

Hey guys,
I'm trying to displace letters in a sentence using a few functions but I can't figure out why I'm unable to exit the loop of getchar. Any other mistakes please let me know as I'm a beginner. Thanks in advance.

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
  
#include <iostream>
using namespace std;

char askSentence (){
    int c;
    cout << "Sentence: ";
    do{
      c = getchar();
    }while(getchar() != '\n');
		
	return c;
}

int displacement (){
	int displ;
    cout << "The displacement: ";
    cin >> displ;
    
    return displ;
}

char modify(char &letter, int disp){
	char x;
	do{
	    
	    if(letter <= 'z' && letter >= 'a'){
          x = letter + desp;
        }else if (letter <= 'Z' && letter >= 'A'){
            x = letter + desp;
            }else if (letter > 'Z'){
                x = letter - 'Z' + 'A' - 1;
                }else if (letter > 'z'){
                    x = letter - 'z' + 'a' - 1;
                  }
      }while(letter != '\n');
	
	return x;
}


int main(){
    int disp = displacement();
    char s = askSentence();
    cout << modify(s, disp);

	
	return 0;
}
try c on line 10.
that is
while (c != '\n');

what is 'desp' (no such variable...) should be disp

asksentence only returns 1 letter. it should return a string.

That is all I see quickly...
Last edited on
char represents the type for just 1 character - not a sentence.

Have you covered std::string yet - or c-style null-terminated string arrays? This is what is needed to encrypt/decrypt a sentence as a whole.

Topic archived. No new replies allowed.