function not working

Hello, here is my code, I am doing a morse code translator. The probloem is that I am trying to get a line from the user but the program runs without inputting the user line.

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

using namespace std;

int Encode();

int main(){
    cout << "Encode = 0 --- Decode = 1:";
    int start;
    cin >> start;
    if(start == 0){
             Encode();
             }
    if(start == 1){
               cout << "not yet";
               }
    system("pause");
    return 0;
    
    }

int Encode(){
     string UsrEng;
     cout << "Enter what do you wanto to translate to morse code: \n";
     getline(cin, UsrEng);
     int numbs;
     numbs = UsrEng.length();
     cout << numbs << endl;
	for (int i = 0; i<numbs; i++){
        if (UsrEng[i] == ' ')
           cout << "  ";
        else if (UsrEng[i] == 'A' || UsrEng[i] == 'a')
             cout << ".- ";
        else if (UsrEng[i] == 'B' || UsrEng[i] == 'b')
             cout << "-... ";
        else if (UsrEng[i] == 'C' || UsrEng[i] == 'c')
             cout << "-.-. ";
        else if (UsrEng[i] == 'D' || UsrEng[i] == 'd')
             cout << "-.. ";
        else if (UsrEng[i] == 'E' || UsrEng[i] == 'e')
             cout << ". ";
        else if (UsrEng[i] == 'F' || UsrEng[i] == 'f')
             cout << "..-. ";
        else if (UsrEng[i] == 'G' || UsrEng[i] == 'g')
             cout << "--. ";
        else if (UsrEng[i] == 'H' || UsrEng[i] == 'h')
             cout << ".... ";
        else if (UsrEng[i] == 'I' || UsrEng[i] == 'i')
             cout << ".. ";
        else if (UsrEng[i] == 'J' || UsrEng[i] == 'j')
             cout << ".--- ";
        else if (UsrEng[i] == 'K' || UsrEng[i] == 'k')
		     cout << "-.- ";
        else if (UsrEng[i] == 'L' || UsrEng[i] == 'l')
		     cout << ".-.. ";
        else if (UsrEng[i] == 'M' || UsrEng[i] == 'm')
	         cout << "-- ";
        else if (UsrEng[i] == 'N' || UsrEng[i] == 'n')
		     cout << "-. ";
        else if (UsrEng[i] == 'O' || UsrEng[i] == 'o')
             cout << "--- ";
        else if (UsrEng[i] == 'P' || UsrEng[i] == 'p')
		     cout << ".--. ";
        else if (UsrEng[i] == 'Q' || UsrEng[i] == 'q')
		     cout << "--.- ";
        else if (UsrEng[i] == 'R' || UsrEng[i] == 'r')
		     cout << ".-. ";
        else if (UsrEng[i] == 'S' || UsrEng[i] == 's')
		     cout << "... ";
        else if (UsrEng[i] == 'T' || UsrEng[i] == 't')
		     cout << "- ";
        else if (UsrEng[i] == 'U' || UsrEng[i] == 'u')
		     cout << "..- ";
        else if (UsrEng[i] == 'V' || UsrEng[i] == 'v')
		     cout << "...- ";
        else if (UsrEng[i] == 'W' || UsrEng[i] == 'w')
		     cout << ".-- ";
        else if (UsrEng[i] == 'X' || UsrEng[i] == 'x')
		     cout << "-..- ";
        else if (UsrEng[i] == 'Y' || UsrEng[i] == 'y')
		     cout << "-.-- ";
        else if (UsrEng[i] == 'Z' || UsrEng[i] == 'z')
		     cout << "--.. ";
        else{
		     cout << "You have not entered a character in the american";
	         cout << " alphabet. Please re-run the program and try again.";
        }
 
}
cout <<endl;
 
}



this part is what is not working, because the user should insert the line that he wants to translate to morse code.

1
2
3
4
5
6
7
int Encode(){
     string UsrEng;
     cout << "Enter what do you wanto to translate to morse code: \n";
     getline(cin, UsrEng);
     int numbs;
     numbs = UsrEng.length();
     cout << numbs << endl;

I think because you're using cin>> and getline(), use cin.ignore() ._. I don't really understand why
Maybe some else can explain it
Last edited on
Here is what my console display:

1
2
3
4
Encode = 0
Enter what do you wanto to translate to morse code:
0

Press any key to continue . . .






I use getline(cin, UsrEng) because it means taht will take the complete line from the input that the user will insert
All I know is don't mix cin>> and getline(), I think that getline is taking '\n' character that left in buffer from cin
I'm usng cin.ignore(some_number_here,'\n') before getline to fix it
Topic archived. No new replies allowed.