Function return problems

Oct 19, 2008 at 9:05pm
Hi, I'm taking a C++ class and I'm having trouble with my solution. My problem is that my functions don't appear to be returning their values to the uninitialized variables being sent to them.
So far everything I've tried has been unsuccessful. the ones I'm having trouble with are the random consonant and random vowel functions passing their return values back to the main. I cannot edit the main function,so adding the lines:
consonant1= generate_random_consonant()
vowel = generate_random_vowel()
would be out of the question.
things I've tried include declaring the variables globally, attempting to use a reference and changing storage classes. none of that worked, in-fact they wouldn't compile for the most part.
here's the code:
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
#include<iostream>
using std::cout;
using std::cin;
#include<cstdlib>
#include<ctime>

void print_random_welcome_phrase();

void print_random_farewell();

char generate_random_consonant(char);

char generate_random_vowel(char);

int number_right(char, char, char, char, char, char);

char get_y_or_n();

int main()
{
	srand(time(0));
	char consonant1, vowel, consonant2, consonant1_guess, vowel_guess, consonant2_guess, again;
	int num_guesses, correct_letters;
	const int MAX_GUESSES_ALLOWED = 10;
	system("cls");
	print_random_welcome_phrase();
	
	do 
	{
		num_guesses = 0;
		generate_random_consonant( consonant1 );
		generate_random_vowel( vowel );
		generate_random_consonant( consonant2 );
		cout << consonant1 << vowel << consonant2; // for testing purposes i added this line
		do
		{
			cout << "\n\nTry to guess the three letter \"word\" I'm thinking of.\nThe word is in the form:"
				 << " consonant vowel consonant.\n";
			cin >> consonant1_guess >> vowel_guess >> consonant2_guess;
			correct_letters =  number_right( consonant1, vowel, consonant2, consonant1_guess, vowel_guess, consonant2_guess );
			++num_guesses;
		}while ( correct_letters != 3 && num_guesses < MAX_GUESSES_ALLOWED );
		
		if ( correct_letters == 3 )
			cout << "You guessed correctly in only " << num_guesses << " tries!.\n";
		else
			cout << "You ran out of guesses. The correct answer was " << consonant1 << vowel << consonant2 << ".\n";

		cout << "Try again? (y or n) ";
		again = get_y_or_n();
		system("cls");
	} while ( again == 'y' );
	
	print_random_farewell();
	system("pause");
	
	return 0;
}

void print_random_welcome_phrase(){
	int welc_phrase;
	welc_phrase = rand() % 5;
	switch(welc_phrase){
		case 0:
		cout << "Hello.\n";
		break;
		case 1:
		cout << "Welcome.\n";
		break;
		case 2:
		cout << "Good day.\n";
		break;
		case 3:
		cout << "Oh Hai.\n";
		break;
		case 4:
		cout << "You rang?.\n";
		break;}
return;}

char generate_random_consonant( char consonant ){
	consonant = rand() % 26;
	do{
	consonant = 'a' + consonant;
	}while (consonant != 'b' && consonant != 'c' && consonant != 'd' && consonant != 'f' 
			&& consonant != 'g' && consonant != 'h' && consonant != 'j' && consonant != 'k' 
			&& consonant != 'l' && consonant != 'm' && consonant != 'n' && consonant != 'p' 
			&& consonant != 'q' && consonant != 'r' && consonant != 's' && consonant != 't' 
			&& consonant != 'v' && consonant != 'w' && consonant != 'x' && consonant != 'y' 
			&& consonant != 'z');// ensures that returned value is a consonant
	cout << consonant;
	return consonant;}

char generate_random_vowel( char vowel ){
	vowel = 'a' + rand() % 26;
	do{
	vowel = 'a' + vowel;
	}while (vowel != 'a' && vowel != 'e' && vowel != 'i' && vowel != 'o' && vowel != 'u');//ensures returned value is a vowel
	cout << vowel;
	vowel = vowel;
	return vowel;}

	// theoretically this should compare the values from the function calls to the user input values
int number_right(char letter1, char letter2, char letter3, char guess1, char guess2, char guess3){
	int correct=0;
	cout << letter1 << letter2 << letter3 << guess1 << guess2 << guess3; // this is only here for my current debugging purposes, so i can see what is being compared
	if (letter1 == guess1)
		correct = correct + 1;
	if (letter2 == guess2)
		correct = correct + 1;
	if (letter3 == guess3)
		correct = correct + 1;
		cout << "You have "<<correct<<" letters correct.";
return (correct);}
// simply gets a y or n
char get_y_or_n(){
	char y_or_n;
	do{
		cout << "Would you like to try another one?";
		cin >> y_or_n;
		tolower (y_or_n);// this doesn't work either, but thats not my main problem right now
		}while (y_or_n != 'y' && y_or_n != 'n');
	return y_or_n;}

void print_random_farewell(){
int bye;
	bye = rand() % 5;
	switch(bye){
		case 0:
		cout << "Bye.\n";
		break;
		case 1:
		cout << "Good-bye.\n";
		break;
		case 2:
		cout << "Later.\n";
		break;
		case 3:
		cout << "Have a nice day.\n";
		break;
		case 4:
		cout << "No! Don't leave me!\n";
		break;}
return;}


if anyone could help me it would be appreciated.
Last edited on Oct 19, 2008 at 9:39pm
Oct 20, 2008 at 3:35am
It's because the way you are using return values/parameters is wrong.

Return values are used like this:

1
2
3
4
5
6
7
int return_one() {
   return 1;
}

int main() {
   int number = return_one();  //number will be 1 because that is what return_one() is returning
}


The way you are using parameters is weird...you are passing a parameter and just using it as a temporary variable...you should just create a temp variable inside of the function.

If you HAVE to pass a parameter, make the function a void function and just pass the variable by reference, then edit it, i.e.:

1
2
3
4
5
6
7
8
9
void change_int_to_two(int &number) {
   number = 2; //since we passed by reference, we are using modifying the actual variable
}

int main() {
   int number = 0;
   change_int_to_two(&number);
   //number is now 2
}
Last edited on Oct 20, 2008 at 3:37am
Oct 20, 2008 at 4:34am
This is building on what firedraco said:

A function can only return 1 value.

However a function can modify every parameter you pass it.

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
// just return a random number, let the caller deal with the result
int randomWrapper1()
{
    return rand();
}

// caller gives us an integer to insert a random number into
void randomWrapper2(int & num)
{
    num = rand();
}

// caller gives us a pointer to the integer that we need to set to a random number
void randomWrapper3(int * num)
{
    *num = rand();
}

int main()
{
    // each of the following lines below call a function to set the value of a variable.
    // each function does so in a different way.
    int aVariable;
    aVarialbe = randomWrapper1();
    cout << aVariable << endl;
    randomWrapper2(aVarialbe);
    cout << aVariable << endl;
    randomWrapper3(&aVarialbe);
    cout << aVariable << endl;
    return 0;
}
Oct 20, 2008 at 6:09pm
However a function can modify every parameter you pass it.


Small note: this only is true when you're using pointers as parameters, like cheif is doing in the code above
Oct 20, 2008 at 6:19pm
Actually it is true in all cases, it's just the parameters that you pass it aren't the same as the parameters you are using inside of the function unless they are pointers/references.
Oct 20, 2008 at 6:43pm
As i have learned (i'm pretty new to programming :)), you don't pass a paramater to a function, but a copie of it. So unless you're using pointers/references (what excactly is the difference between those two?), you won't be able to change the parameter itself, right?
Topic archived. No new replies allowed.