Program using void functions

Hello. I am trying to learn C++ and I am making a program that turns phone numbers into words. For example, if the user inputs '4384357' out pops 'GETHELP'. I am trying to accomplish this via void functions but I'm having a time with it. Can anyone help me to figure out why nothing is happening? Thank you.

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

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;

void GetString (float& number); 
void CalcString (float& number);

int main()
{                                                                    
	float number;

	GetString (number);
	CalcString (number);

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

	return 0;
	
}

void GetString (float& number)
{ 
	int size;
	string letterin;
	string letterout;
	
	cout << "Please enter string or press Q to exit:   " << endl;
	cout << endl;
	getline(cin, letterin);
	size = letterin.length();
	cout << endl;
	cout << "The word is:   " << endl;
	cout << endl;
		
}

void CalcString (float& number)
{
	int size;
	string letterin;
	string letterout;
	int index = 0;
	char numberin;

	string Letters[26] = {"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"};

	while (letterin != "Q")
	{
		for (int index = 0; index++;)
		{
			wordin = letterin[index];
			cout << letterout << "  ";
		}

	}

	cout << "Please enter string or press Q to exit:   " << endl;
	cout << endl;
	getline(cin, letterin);

	while (letterin == "Q")
	{
		return;
	}

	size = letterin.length();
	cout << endl;
	cout << "The word is:   " << endl;
	cout << endl;

}
In your function CalcString, you have this at line 51 above:

while (letterin != "Q")

What is the value of letterin the first time round? You didn't set it.

What inside the while loop will actually change letterin so the loop can end? Nothing,. There is no change. It will loop forever.

wordin = letterin[index];
You're trying to read letterin[index]. What's the size of the string letterin at this point? Zero. You never set it to anything.
The syntax of your void functions is correct. That has nothing to do with the problems you're having. Your program logic is messed up.

13) Why is number a float? telephone numbers don't have decimal places.
34) What is the point of getting size? You don't use it.
36) Why are you outputing "the word is" here? You haven't calculated the string yet. You also output "the word is" at line 72.
33) You get lettersin into a local variable. The value goes out of scope when you exit GetString.
38) You pass number by reference, but you never set it.
51) You reference letterin, but lettersin is uninitialized. You're testing letterin for Q before ever asking the user to enter it.
53) You're missing the exit condition (2nd term) for your loop.
55) Where is wordin defined?
55) What's the point of wordin. You don't use it.
56) letterout is uninitialized.
61) You asking the same question you did in GetString.
65) Ever heard of an if statement? No need for a loop.
70) Same comment as line 34.
72) You never output the word.
Presumably you meant to calculate the word from number, but you never do so.


Thank you, guys, for your help so far! I believe that I am getting it. I made a different program along the same lines that has the same problem. I don't know what's happening here. I set up a void function so that when the user inputs a string - say, "AB" - out will pop "Alpha Bravo" - the ICAO keywords. But, the problem is, it doesn't do that. What is wrong here?

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

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;

void GetString (char& word); 
void CalcString (char& word);

int main()
{                                                                    
	char word;
	
	GetString (word);

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

	return 0;
	
}

void GetString (char& word)
{ 
	char stringin;

	cout << "Please enter string or press 9 to exit:   " << endl;
	cout << endl;
	cin >> stringin;
	stringin = toupper(stringin);

	while (stringin == '9')
	{
		return;
	}

	cout << endl;
	cout << "The phonetic version is:   " << endl;
	cout << endl;
	
	while (stringin != '9')
	{
		switch (stringin)
		{
			case 'A' : cout << "Alpha ";
					   stringin++;
					   break;
			case 'B' : cout << "Bravo ";
					   stringin++;
					   break;
                        case 'C' : cout << "Charlie ";
					   stringin++;
					   break;

// etc, etc


		}

	}
	

	cout << "Please enter string or press 9 to exit:   " << endl;
	cout << endl;
	cin >> stringin;

	cout << endl;
	cout << "The phonetic version is:   " << endl;
	cout << endl;

}

stringin++
What are you trying to do with this? You're taking a single character, and adding one to it. This will, for example, change an A to a B, or a e to an f.

I think you don't understand the difference between a char and a char array and a pointer to a char. You need to learn this.
Last edited on
What do I do in order to end the loop?
What do I do in order to end the loop?

Ignore that previous comment; I edited it. I didn't read it properly and I thought you were trying to move a pointer instead of just adding a number to a char.

If I had to guess, you're expecting every input word to end in a 9, and then you're trying to go through each letter in turn until you get to the 9.

You're hampered in this by not understanding what a char/char array/char pointer is.
Thank you for your time, Moschops. I should have explained this more thoroughly, I apologize. What I am trying to do is allow the user to input any combination of letters in order to get the ICAO equivalent - that is, until they press '9' in order to terminate the program. The problem I am having is that I can easily translate 'A' into 'Alpha' but if they put 'AC', instead of reading 'Alpha Charlie', it reads 'Alpha Bravo Charlie'. I assume that's because of stringin++. How do I modify this program so that it does what I want? Remember, I'm still very much a beginner. Thank you!
Does anyone have any solution? I'm learning about char arrays and pointers, but I'm not exactly sure how to implement them.
15,24) You pass word to GetString, but never use it.
33) You don't need a loop to test, just an if statement.
1
2
if (stringin == '9')
    return;

42) You don't need to check if stringin is '9'. If it were, you would have returned previously.
64-70) What is the purpose of these lines? The value of stringin you just asked for will be lost when you exit the function.
69) You've already output that earlier.

edit to add:
47,50,53) As Moschops said, you don't want to increment the value of stringin.

Last edited on
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
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;

void GetString (char& stringin); 

int main()
{                                                                    
	char stringin;
	
	GetString (stringin);

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

	return 0;
	
}

void GetString (char& stringin)
{ 
	
	cout << "Please enter string or press 9 to exit:   " << endl;
	cout << endl;
	cin >> stringin;
	stringin = toupper(stringin);

	if (stringin == '9')
		return;
	
	cout << "The phonetic version is:   " << endl;
	cout << endl;
	
	switch (stringin)
		{
			case 'A' : cout << "Alpha ";
					   break;
			case 'B' : cout << "Bravo ";
					   break;
		}

}
		


This should fix those problems. The problem I am having - and again, I am still very new to C++ - is that I want to move the pointer to the next letter in the user inputted string. I can get the one-letter ones well. 'A' = 'Alpha'. But, I can't get the pointer to move to the next letter and output that word. i.e. 'BG' into 'Bravo Golf'. It just says 'Bravo' for a split second and then exits. What is the problem?
Okay, that looks a lot better.

char stringin only allocates a single character of storage.
In spite of what you named it, it is not a string, or even an array.
Try declaring it as a string.
You'll also need a loop to iterate through each of the characters in the string.
Last edited on
So, a loop rather than a switch?
No, you need both.
You need to iterate through each character in the string, testing one character on each iteration.
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
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;

void GetString (string& stringin); 

int main()
{                                                                    
	string stringin;
	
	GetString (stringin);

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

	return 0;
	
}

void GetString (string& stringin)
{ 
	
	cout << "Please enter string or press 9 to exit:   " << endl;
	cout << endl;
	cin >> stringin;

	if (stringin == "9")
		return;
	
	cout << "The phonetic version is:   " << endl;
	cout << endl;
	
	while (stringin != "9")
		{
				
			if(stringin == "A", "a")
				cout << "The letter A is Alpha " << endl;
			else
				if(stringin == "B", "b")
					cout << "The letter B is Bravo " << endl;
				else
					if (stringin == "C", "c")
						cout << "The letter C is Charlie " << endl;
					else
						if(stringin == "D", "d")
							cout << "The letter D is Delta " << endl;
						else
							if(stringin == "E", "e")
								cout << "The letter E is Echo " << endl;
							else
								if (stringin == "F", "f")
									cout << "The letter F is Foxtrot " << endl;
								else
									if (stringin == "G", "g")
										cout << "The letter G is Golf " << endl;
									else
										if(stringin == "H", "h")
											cout << "The letter H is Hotel " << endl;
										else
											if(stringin == "I", "i")
												cout << "The letter I is India " << endl;
											else
												if(stringin == "J", "j")
													cout << "The letter J is Juliet " << endl ;
												else
													if(stringin == "K", "k")
														cout << "The letter K is Kilo " << endl;
													else
														if(stringin == "L", "l")
															cout << "The letter L is Lima " << endl;
														else
															if(stringin == "M", "m")
																cout << "The letter M is Mike " << endl;
															else
																if (stringin == "N", "n")
																cout << "The letter N is November " << endl;
															else
																if(stringin == "O", "o")
																cout << "The letter O is Oscar " << endl;
															else
																if(stringin == "P", "p")
																	cout << "The letter P is Papa " << endl;
																else
																	if(stringin == "Q", "q")
																		cout << "The letter Q is Quebec " << endl;
																	else
																		if(stringin == "R", "r")
																			cout << "The letter R is Romeo " << endl;
																		else
																			if(stringin == "S", "s")
																				cout << "The letter S is Sierra " << endl;
																			else
																				if(stringin == "T", "t")
																					cout << "The letter T is Tango " << endl;
																				else
																					if(stringin == "U", "u")
																						cout << "The letter U is Uniform " << endl;
																					else
																						if(stringin == "V", "v")
																							cout << "The letter V is Victor " << endl;
																						else
																							if(stringin == "W", "w")
																								cout << "The letter W is Whiskey " << endl;
																							else
																								if(stringin == "X", "x")
																									cout << "The letter X is X-ray " << endl;
																								else
																									if(stringin == "Y", "y")
																										cout << "The letter Y is Yankee " << endl;
																									else
																										if(stringin == "Z", "z")
																											cout << "The letter Z is Zulu " << endl;
																										else
																											cout << "Letter entered was not alphabetic " << endl;
			
			}
		
}


Here's what I have now. My question is - how do you get the user out of the loop and then have them go through it again looking for the next letter in the string that they input?
One character at the time
1
2
3
char character;
while( cin>>character )
   cout << ICAO(character) << ' ';


if(stringin == "A", "a") is incorrect. I suppose that you want if(stringin=="A" or stringin=="a")

BTW ¿what crappy indenter are you using?
Last edited on
Wow, that's a nice if else staircase you have formatted there.
Two comments:
1) Why did you take out the switch statement?
2) Your loop isn't going to exit because stringin will always be not equal to "9" since you've made sure of that at line 30.

Try the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
size_t  len;
char    temp;
len = stringin.size();
for (int i=0; i<len; i++)
{  temp = toupper(stringin[i]);
    switch (temp)
    { case 'A': cout << "Alpha " << endl;
	     break;
       case 'B': cout << "Bravo " << endl;
                     break;
...
    }
}



Topic archived. No new replies allowed.