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