Have a getline error, don't know what to fix.

So towards the bottom, I have a function called void message. Ideally, I am trying to get a message from english to morse code. I'm not sure what I have to fix for the getline(cin,letter) to fix it. Any help would be appreciated. I haven't worked a lot on multiple functions either.

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
  //James Wirth, February 13th, 2017
//The purpose of this program is to send a morse coded document with the correct amount of charge and translate each letter if the user is unaware of the identification of a letter to morse.
//I had some trouble trying to figure out the algorithms of converting the amount into a proper coin situated fee.  It has something to do with the dollars.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
	
	const int pennyvalue = 1;
	const int nickelvalue = 5;
	const int dimevalue = 10;
	const int quartervalue = 25;
	const int dollarvalue = 100;
	
	int wordsSent(int);
	void message(string);
	void readFile();
	
int main()
{
	int choice;
	
	cout << "Would you like to: " << endl;
	cout << "1 - Process your bill or translate code?" << endl;
	cout << "2 - Translate to Morse Code?" << endl;
	cout << "3 - Process a data file?" << endl;
	cout << "4 - Quit?" << endl;
	
	cin >> choice;
	
	while (choice > 4 || choice < 1)
{
	cout << "Whoops, you didn't input a correct value.  Try again." << endl;
	cin >> choice;
}
}
	
int wordsSent(int)
{
	int words, streetnumbers, zip, amount, dollars, quarters, dimes, nickels, pennies, choice, changeindollars, changeinquarters, changeindimes, changeinnickels, changeinpennies;
	double total, total1, total2;
	string name, street, city, state;
	
	cout << "You've chosen to process your bill." << endl;
	
	//This part of the program asks the user to input info about their info.
	
	cout << "What is your name? ";
	cin >> name;
	
	cout << "What are the numbers in your street address? ";
	cin >> streetnumbers;
	
	cout << "What is the name of the street you live on? "; 
	cin >> street;
	
	cout << "What's the city name? ";
	cin >> city;
	
	cout << "What's the state? ";
	cin >> state;
	
	cout << "What's the zipcode? ";
	cin >> zip;
	
	cout << "How many words sent? ";
	cin >> words; 
	
	//Where the algorithms are made.
	
	total1 = (words/5) * 1.50;
	total2 = words * .5;
	if (total1<total2)
	{
	total = total1;
	}
	else
	{
	total = total2;
	}
	
	//This will display all of the info they put in.
	
	cout << name << endl;
	cout << streetnumbers << endl;
	cout << street << endl;
	cout << city << ", ";
	cout << state << ", ";
	cout << zip << endl;
	cout << words << endl;
	cout << "Your fee is " << total << " dollars" <<endl;
	cout << endl;
	
	cout << "Enter the amount you want to give in terms of pennies: ";
	cin >> amount;
	
	while (amount < total * 100)
	{
	cout << "Oops, seems like you put in less than the fee.  Please try again." << endl; 	//If a choice isn't one asked for, repeats.
	cin >> amount;
}
	amount = amount - total * 100;
	changeindollars = amount / 100;
	amount = amount % 100;
	changeinquarters = amount % 100 /25;
	amount = amount % 25;
	changeindimes = amount % 10;
	amount = amount % 10;
	changeinnickels = amount % 5;
	amount = amount % 5;
	changeinpennies = amount % 1;
	amount = amount % 1;
	
	cout << "Your change in dollars is: " << changeindollars << endl;
	cout << "Your change in quarters is: " << changeinquarters << endl;
	cout << "Your change in dimes is: " << changeindimes << endl;
	cout << "Your change in nickels is: " << changeinnickels << endl;
	cout << "Your change in pennies: " << changeinpennies << endl;

	return 0;
}
void message(string)
{
	char one, two, letter, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, three;
	int choice;
	
	if (choice == 2)	//This choice will take the letter inputed, capitalized or not, and return the morse code represention.
	{
	cout << "Enter a message: " << endl;
	cin.ignore();
	getline(cin,letter);
	
	if (letter == 'a' || 'A')
	cout << "a translates to .-" << endl;
	return 0;
	if (letter == 'b' || 'B')
	cout << "b translates to -..." << endl;
	return 0;
	if (letter == 'c' || 'C')
	cout << "c translates to -.-." << endl;
	return 0;
	if (letter == 'd' || 'D')
	cout << "d translates to -.." << endl;
	return 0;
	if (letter == 'e' || 'E')
	cout << "e translates to ." << endl;
	return 0;
	if (letter == 'f' || 'F')
	cout << "f translates to ..-." << endl;
	return 0;
	if (letter == 'g' || 'G')
	cout << "g translates to --." << endl;
	return 0;
	if (letter == 'h' || 'H')
	cout << "h translates to ...." << endl;
	return 0;
	if (letter == 'i' || 'I')
	cout << "i translates to .." << endl;
	return 0;
	if (letter == 'j' || 'J')
	cout << "j translates to .---" << endl;
	return 0;
	if (letter == 'k' || 'K')
	cout << "k translates to -.-" << endl;
	return 0;
	if (letter == 'l' || 'L')
	cout << "l translates to .-.." << endl;
	return 0;
	if (letter == 'm' || 'M')
	cout << "m translates to --" << endl;
	return 0;
	if (letter == 'n' || 'N')
	cout << "n translates to -." << endl;
	return 0;
	if (letter == 'o' || 'O')
	cout << "o translates to ---" << endl;
	return 0;
	if (letter == 'p' || 'P')
	cout << "p translates to .--." << endl;
	return 0;
	if (letter == 'q' || 'Q')
	cout << "q translates to --.-" << endl;
	return 0;
	if (letter == 'r' || 'R')
	cout << "r translates to .-." << endl;
	return 0;
	if (letter == 's' || 'S')
	cout << "s translates to ..." << endl;
	return 0;
	if (letter == 't' || 'T')
	cout << "t translates to -" << endl;
	return 0;
	if (letter == 'u' || 'U')
	cout << "u translates to ..-" << endl;
	return 0;
	if (letter == 'v' || 'V')
	cout << "v translates to ...-" << endl;
	return 0;
	if (letter == 'w' || 'W')
	cout << "w translates to .--" << endl;
	return 0;
	if (letter == 'x' || 'X')
	cout << "x translates to -..-" << endl;
	return 0;
	if (letter == 'y' || 'Y')
	cout << "y translates to -.--" << endl;
	return 0;
	if (letter == 'z' || 'Z')
	cout << "z translates to --.." << endl;
	return 0;
}
}
void readFile()
{
	ifstream inputFile;
	string yes, data, input;
	int choice;
	if (choice == 3) 	//A file will be read through this choice displaying the informatino with the text file.
{						//Didn't know how to do proper spaces after each subject.
	inputFile.open("TelegramData.txt");
	while (inputFile >> data)
	{
		cout << data << endl;
	}
	inputFile.close();
}
	
	if (choice == 4)	//Closes program.
{
	cout << "Thank you.  Closing program." << endl;
	exit(0);
}
}

	
One problem is this:

if (letter == 'a' || 'A') // This is always true since 'A' is != 0 and hence interpreted as true

it is allowed but not valid, change to

if (letter == 'a' || letter == 'A')

letter represents a single char, therefore you cannot use getline(...), change line 131 to cin >> letter

Line 127: Since choice does not have any valid value this line makes no sense. Remove it.

A void function cannot return anything. Hence return 0; is not valid within message(...). Remove the 0.
Getline is used with std::string not char
Hello llSPEEDll,

In addition to what coder777 and Yanson have said your code will not even compile. And if it would the program would end after you enter something on line 29 whether the while loop executes or not.

Before the closing brace of main you need a return 0; not necessary, but good programming. And after line 35 is a good place for a switch to process the menu choice.

The proto type for the message function is written correctly, but the function definition is written incorrectly. The function definition should be void message(std::string message) With std:: being optional when using using namespace std;. But the parameter in the function is pointless since you start your function by getting a message from the user.

Line 124 does nor contain one variable that you use in the function and letter needs to defined as a string i.e., std::string letter;. Not sure what you had in mind here, but it does not work.

Line 131 as Yanson said "Getline is used with std::string not char " std::cin.getline(char* s, streamsize n) here you can define "s" as a char or a char array and "n" is the number of characters you want to input.

From line 135 on all of the return 0;s are not necessary because the function does nor return anything being a "void" function. And at the end the function does not need any kind of return statement.

A suggestion, wrap lines 133 to 209 in a for loop to process the string of the message that the user inputs.

Once you clear up all the problems and get the program to compile you will have a better idea of how the program is working.

If you know anything about "maps" it is a better way to handle the translation from a letter to Morse code.

Hope that helps,

Andy
Hi,

1. The variable choice in the function void message(), is outside the scope of the variable choice that is defined in main(). As such and because you haven't initialized choice inside message, it will contain garbage.

Read and run the following program. Note: it's a .c program so compile using gcc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h> 

void print_me()
{
	int a; //Unitialized; Contains garbage
	printf("In print_me(). The value of a is %d.\n", a);
}

int main()
{
	int a = 10; 
	print_me(); //Does it print "The value of a is 10.\n"?
	
	int pauseConsole; 
	scanf("%d",&pauseConsole);
	return 0; 
}


The solution is simple: just include another parameter in the definition of the function message() that represents the choice the user has selected. But before you do that, read on to my other suggestions.

2. Your main() function doesn't call any of the functions that is suppose to do something after a user has selected a menu option. All it does is take in user input and store the input value into the variable choice and terminates.

A simple way to implement a "menu" is by using a switch-statement.

See https://www.tutorialspoint.com/cplusplus/cpp_switch_statement.htm

3. The long list of if-statements is inefficient, un-maintainable, difficult to read and cumbersome to modify. Consider using a data structure to associate a string of dits-and-dahs i.e., ".._" with an index value (i.e., an index into an array). Also, since morse-code encodes uppercase alpha-characters, why don't you just write a function to convert the string to either lower-case or upper-case first?

For example, use an array of string literals where each array element is a string-literal corresponding to the dot/slash representation of an alpha-character in morse code.

morse_char[0] = "._" //A
morse_char[1] = "_..." //B
morse_char[2] = "_._." //C

Note: Use the fact that the ascii values of letters (uppercase or lowercase) are in consecutive order AND array indices are consecutive in order to translate between a C++ char and a string literal.
Last edited on
Topic archived. No new replies allowed.