Reference variables not working

I have an issue, my teacher assigned me to use reference variables, but the program doesn't work ( its about sorting 3 strings entered by user ), and I tried removing the reference variables and removing the getStrings in the sortStrings void. Why would it not work? All help is welcome.

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
#include <iostream>
#include <string>
using namespace std;
void  getStrings (string &first, string &second, string &third);
void  sortStrings (string &lowest, string &middle, string &highest);
int main()
{
	string first = "";
	string second = "";
	string third = "";
	cout << "Please enter a string: ";
	cin >> first;
	cout << "Please enter a second string: ";
	cin >> second;
	cout << "Please enter a third string: ";
	cin >> third;
	getStrings(first, second, third);
	sortStrings(first, second, third);
}

void  getStrings (string  &first, string  &second, string  &third)
{
	string &string1 = first;
	string &string2 = second;
	string &string3 = third;
}

void  sortStrings (string  &first, string  &second, string  &third)
{
	string string1;
	string string2;
	string string3;
	getStrings(first, second, third);
	if (string1 < string2 && string1 < string3)
	{
		if (string2 < string3)
		{
			cout << string1 << ", " << string2 << ", " << string3;
		}
		else if (string2 > string3)
		{
			cout << string1 << ", " << string3 << ", " << string2;
		}
	}
	else if (string2 < string1 && string2 < string3)
	{
		if (string1 < string3)
		{
			cout << string2 << ", " << string1 << ", " << string3;
		}
		else if (string1 > string3)
		{
			cout << string2 << ", " << string3 << ", " << string1;
		}
	}
	else if (string3 < string1 && string3 < string2)
	{
		if (string1 < string2)
		{
			cout << string3 << ", " << string1 << ", " << string2;
		}
		else if (string1 > string2)
		{
			cout << string3 << ", " << string2 << ", " << string1;
		}
	}
}
Last edited on
Like so
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
#include <iostream>
#include <string>
using namespace std;
void  getStrings (string &first, string &second, string &third);
void  sortStrings (string &lowest, string &middle, string &highest);
int main()
{
	string first = "";
	string second = "";
	string third = "";
	getStrings(first, second, third);
	sortStrings(first, second, third);
}

void  getStrings (string  &first, string  &second, string  &third)
{
	cout << "Please enter a string: ";
	cin >> first;
	cout << "Please enter a second string: ";
	cin >> second;
	cout << "Please enter a third string: ";
	cin >> third;
}

void  sortStrings (string  &first, string  &second, string  &third)
{
	if (first < second && first < third)
	{

Line 17, you've already gotten the strings from the user before you make this function call.

Moreover, the function itself creates 3 variables set to the 3 strings, and then does nothing with them.

And then on line 33, you once again call this useless function, and then go on to use 3 uninitialized strings.

After the function call on line 33, string1, string2, and string3 are still empty. The function you call has variables with the same name, but they are NOT the same variables.
@OP The reference variables, which are addresses, hence & notation are just a means for shunting around the original strings through the various functions you choose. So the local variable names - a, b, c, x, y etc - don't have to be the oroginal name of the string. In fact it's better and clearer if they aren't!

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

using namespace std; // Debateable ...

void getStrings(string&, string&, string&); // NOTE: No variable names here req'd
void sortStrings(string&, string&, string&);
void swap(string&, string&);
void display(string&, string&, string&);

int main()
{
    string first = "";
    string second = "";
    string third = "";
    
    getStrings(first, second, third);
    display(first, second, third);
    
    sortStrings(first, second, third);
    display(first, second, third);
}

void  getStrings(string& a, string& b, string& c)
{
    cout << "       Please enter a string: ";
    cin >> a;
    
    cout << "Please enter a second string: ";
    cin >> b;
    
    cout << " Please enter a third string: ";
    cin >> c;
    
    return;
}

void  sortStrings(string& a, string& b, string& c)
{
    if (a > c)
       swap(a, c);

    if (a > b)
       swap(a, b);
    
    if (b > c)
       swap(b, c);
    
    return;
}

void swap(string& x, string& y)
{
    string temp;
    
    temp = x;
    x = y;
    y = temp;
    
    return;
}

void display(string& a, string& b, string& c)
{
    cout
    << "The 3 strings are: "
    << a << ' ' << b << ' ' << c << '\n';
    return;
};
Hello nikovch,

you wrote:

my teacher assigned me to use reference variables


That is fine, but post the instructions, directions, specifications, specs as I like to think of them, so anyone reading this will know what needs to be done.

Looking at your program:
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
#include <iostream>
#include <string>

using namespace std;

void  getStrings(string &first, string &second, string &third);
void  sortStrings(string &lowest, string &middle, string &highest);

int main()
{
    string first;
    string second = "";
    string third = "";

    cout << "Please enter a string: ";
    std::getline(std::cin, first);

    cout << "Please enter a second string: ";
    cin >> second;

    cout << "Please enter a third string: ";
    cin >> third;

    getStrings(first, second, third);

    sortStrings(first, second, third);
}

Other than line 4, which is best not to use, you are good down to line 11.

Also notice how the blank lines make the code easier to read. The first benefit is to you and the next is for anyone who has to read you code to figure out what is wrong. So make it as easy as you can to read the code. It also helps when you are trying to figure out what is wrong.

When defining a std::string lines 11 and 12 are the same. both define a variable that has a zero size and contain nothing, so the ( = "") is just a waste of typing because it does nothing. Now if you want to put something between the double quotes and give the variable a starting value then it becomes useful.

Line 17 demonstrates the use of the unformatted input of "getline" to enter your strings. Formatted input, cin >> first;, will take what was entered from the keyboard and stored in the input buffer and extract from the input buffer until it finds a space or new line "\n" whichever comes first. So if you type in (The quick brown fox.) only (The) will be put into the variable "first". Then the next cin >> second; will not wait for any keyboard input, but take what is left in the input buffer and put what is next into "second", (quick), until it finds a space and stops. And "third" would end up with (brown) leaving (fox\n) for any "cin" that would follow.

"std::getline()" being unformatted input will preserve the spaces and extract everything including the "\n" which it discards leaving the input buffer empty.

Moving ahead a bit:
1
2
3
4
5
6
void  getStrings(string  &first, string  &second, string  &third)
{
    string &string1 = first;
    string &string2 = second;
    string &string3 = third;
}

The function definition is correct, but inside the {}s is wrong. This function name implies the it is to get the user input. So lines 15 - 22 should be here.

The line string &string1 = first; just defines a reference to the variable "first". This may help you understand this: https://www.learncpp.com/cpp-tutorial/611-references/ I do not believe this is the type of reference that your teacher meant.

In the sort function you start with this:
1
2
3
4
5
6
7
8
9
void  sortStrings (string  &first, string  &second, string  &third)
{
	string string1;
	string string2;
	string string3;

	getStrings(first, second, third);

	if (string1 < string2 && string1 < string3)

You define "string1", "string2" and "string3", but never give them any value. Then you call "getStrings" passing "first", "second" and "third" to either have the user reenter what they have already typed or to get new information. Is there a point to this?

Your if and else if statements are comparing strings that have no value. What they should be comparing is "first", "second" and "third".

Although you do have the ability to change the variable names in the function I would stay with what you have done because it makes the code easier to understand. Changing the variable names just makes it harder to keep track of what you are working with. Now and then there are reasons that changing the variable names does have a use, but that would be rare.

One of the things that works to your advantage is scope. What it covers and how long it lasts.

I think with salem c's suggestions and what I have said you should be able to come up with a better solution.

Andy
Changing the variable names just makes it harder to keep track of what you are working with. Now and then there are reasons that changing the variable names does have a use, but that would be rare.


There are a number of sound reasons why I chose to change the names, and as frequently as I did:
1. It demonstrates the use of addresses by exaggerating the naming.
2. It assists debugging and avoids/highlights accidental misnaming or misunderstandings.
3. It is even considered good practice to use simple variable names within functions along with the good practice to keep the line count of functions as low as practicable.
4. They assist re-use.

Of course Namby Pamby - Handy Andy you resort to offering your usual illiterate opinions based on nebulous assumptions and know-all rubbish like 'rare occasions', and 'that would be rare' as if you would know, and as if you would know how to write a working piece of code.

Let the OP decide and MYOFB, Namby

I came up with a solution that worked, and that's all that matters for me. I cleaned it up. and got a good grade on it. Both suggestions helped immensely. :)
Well said @nikovch.

I think there were more than 3 suggestions actually, but that aside different people bring different insights and its your call what if any or all of them help you.

Unfortunately some suggesters need to be (serially) reminded this is not a competition of who is the greatest fountain of knowledge on what is after all only a computer language application - not much more than writing a recipe for a machine to follow. Most fountains are like the rest of us, mere squirts.

Congrats on getting a good mark!
Topic archived. No new replies allowed.