Sorting Letters and Words

Hello, I'm new to C++ and am having a problem with this code. can anyone please
help or give me a tip?

This is a Letter and Word sorting program
Enter two different letters separated by a space: [assume user inputs: b a]
Enter two different words separated by a space: [assume user inputs: bob alex]

Output:

a goes before b
alex goes before bob


// Sorting Letters and Words
#include <iostream>
#include <string>

int main()
{
std::string letter1;
std::string letter2;
std::string letter3;
std::string letter4;
std::string displayName1;
std::string displayName2;
std::string displayName3;
std::string displayName4;

std::cout << "This is a Letter and Word sorting program " << std::endl;
std::cout << "Enter two different letters separated by a space: " << std::endl;
std::cin >> letter1 >> letter2;

std::cout << "Enter two different words separated by a space: " << std::endl;
std::cin >> letter2 >> letter4;

if ((letter1 < letter2) && (letter1 < letter3))
{
if (letter2 < letter3)
{
displayName1 = letter1;
displayName2 = letter2;
displayName3 = letter3;
}
else if (letter2 > letter3)
{
displayName1 = letter1;
displayName2 = letter3;
displayName3 = letter2;
}
else
std::cout << "Error." << std::endl;
}

if ((letter2 < letter1) && (letter2 < letter3))
{
if (letter1 < letter3)
{
displayName1 = letter2;
displayName2 = letter1;
displayName3 = letter3;
}
else if (letter1 > letter3)
{
displayName1 = letter2;
displayName2 = letter3;
displayName3 = letter1;
}
else
std::cout << "Error." << std::endl;
}
if ((letter3 < letter1) && (letter3 < letter2))
{
if (letter1 < letter2)
{
displayName1 = letter3;
displayName2 = letter1;
displayName3 = letter2;
}
else if (letter1 > letter2)
{
displayName1 = letter3;
displayName2 = letter2;
displayName3 = letter1;
}
else
std::cout << "Error." << std::endl;
}

std::cout << displayName1 << " goes before " << displayName2 << std::endl;
std::cout << displayName3 << " goes before " << displayName4 << std::endl;

return 0;
}
can you ask a better question?
So far, I see you appear to be asked for 2 letters in the assignment text, and get 4 in the program?

also, strings can be compared just like letters but case matters, that is b > A or something like that so you have to either work in all the same case or convert to all the same case or maybe just be aware of this issue.

Not sure what you want us to tell you beyond that... ? IT looks like a good start, does it work?
Last edited on
Actually, it is from the textbook that I started to put together. basically, it doesn't work and I have to start from the beginning. I don't know how to create a code that organizes my letters and word. More like the "if" (first timer)

Here is an example:

//type two letters (b a)

std::cout << letter1 << "goes before" << letter2 << std::endl; // a goes before b

//type two words (tom bob)

std::cout << word1 << "goes before" << word2 << std::endl; // bob goes before tom
Last edited on
Hello Frank5093,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



I see from your last post you are letting the book over think the program. For what it is doing I cut it down to this:
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
// Sorting Letters and Words
#include <iostream>
#include <limits>
#include <string>

int main()
{
	std::string letter1;
	std::string letter2;
	std::string word1;
	std::string word2;
	//std::string displayName1; // <--- Do not need.
	//std::string displayName2;
	//std::string displayName3;
	//std::string displayName4;

	std::cout << "This is a Letter and Word sorting program " << std::endl;
	std::cout << "\nEnter two different letters separated by a space: ";  // <--- Changed.
	std::cin >> letter1 >> letter2;

	std::cout << "Enter two different words separated by a space: ";  // <--- Changed.
	std::cin >> word1 >> word2;

	std::cout << "\n\n";  // <--- Added.

	if (letter1 < letter2)
		std::cout<< letter1 << " goes before " << letter2 << std::endl;
	else
		std::cout << letter2 << " goes before " << letter1 << std::endl;

	if (word1 < word2)
		std::cout << word1 << " goes before " << word2 << std::endl;
	else
		std::cout << word2 << " goes before " << word1 << std::endl;

	// A fair C++ replacement for "system("pause")" for Windows. Or a way to pause the program.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;  // <--- Not required, but makes a good break point.	
}

Maybe this will help you understand what is happening a bit easier.

A point about the code. As an example: if ((letter3 < letter1) && (letter3 < letter2)). I did not see where "letter3" was ever given a value after it was defined, so you are comparing an empty string here and the other place it is used. I think in the second "cin" you meant to make the first variable "letter3", but you used "letter2" instead.

Andy
Hello Frank5093,

When I started thinking about your original program and where it came from I started working in it to see what I could do.

One thing I found is that you define "std::string displayName4;", but never give it a value, so when you reach the final "cout" statements "displayName4" is printing an empty string.

I adjusted the beginning of "main" for testing the program. Setting it up this way allows you to skip having to enter something each time the program runs and allows you to concentrate on the rest of the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
	std::string letter1{ "b" }; // <--- Used for testing. Remove the {}s and everything in between when finished.
	std::string letter2{ "a" };

	std::string word1{ "bob" };
	std::string word2{ "alex" };

	std::string displayName1{ "Defined, but empty" }; // <--- Used for testing. Remove the {}s and everything in between when finished.
	std::string displayName2{ "Defined, but empty" };
	std::string displayName3{ "Defined, but empty" };
	std::string displayName4{ "Defined, but empty" };

	std::cout << "\n This is a Letter and Word sorting program " << std::endl;

	std::cout << "\n Enter two different words separated by a space: " << letter1 << ' ' << letter2 << '\n';
	//std::cout << "\n Enter two different letters separated by a space: ";
	//std::cin >> letter1 >> letter2;

	std::cout << "\n Enter two different words separated by a space: " << word1 << ' ' << word2 << '\n';
	//std::cout << "\n Enter two different words separated by a space: ";
	//std::cin >> word1 >> word2; 


When I ran the program with the "letter"s and "word"s this way it produced the output of:

This is a Letter and Word sorting program

 Enter two different words separated by a space: b a

 Enter two different words separated by a space: bob alix

 "a" goes before "b"

 "alix" goes before "Defined, but empty"

 Press Enter to continue:



When I reversed the letters and the names it produced this output:

This is a Letter and Word sorting program

 Enter two different words separated by a space: a b

 Enter two different words separated by a space: alix bob

 "Defined, but empty" goes before "Defined, but empty"

 "Defined, but empty" goes before "Defined, but empty"


 Press Enter to continue:



As you can see "displayName4" is never changed after it was defined and when everything is in the correct order something is not working right.

Since I do not have the book that you are working from I can not say what you have missed or where all the places where you may have used the wrong number.

All I can suggest for now is to compare the book to what you have written and see what is wrong in what you have written.

This program is a good example of how a good variable name can help to spot problems. An example the first if statement: if ((letter1 < letter2) && (letter1 < letter3)) and with a more proper variable name:
if ((letter1 < letter2) && (letter1 < word1)) for the rhs of && you can easily see that you are comparing a string with a single letter to a string with several letters. The returned value of the "string compare" http://www.cplusplus.com/reference/string/string/compare/ may not bee what you want.

I will work on it some more and see if I can figure out how it should be done.

Andy

Edit: typo
Last edited on
- Any time you find yourself using variables like letter1 and letter2, you should probably use an array instead.
- If the program wants letters, then use a char instead of a string. Otherwise the user can type "swizzlestick" when they're supposed to enter a single letter.
- The assignment is probably a precursor to sorting larger sets of data. To do that, you need to actually rearrange the data rather than just print it in a different order.

Modifying Andy's fine 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
// Sorting Letters and Words
#include <iostream>
#include <limits>
#include <string>

int main()
{
    char letters[2];
    std::string words[2];

    std::cout << "This is a Letter and Word sorting program " << std::endl;
    std::cout << "\nEnter two different letters separated by a space: ";  // <--- Changed.
    std::cin >> letters[0] >> letters[1];

    std::cout << "Enter two different words separated by a space: ";  // <--- Changed.
    std::cin >> words[0] >> words[1];

    std::cout << "\n\n";  // <--- Added.


    // If the letters are out of order then swap them.
    if (letters[1] < letters[0]) {
	// You can also use std::swap(letters[0], letters[1]) to do the same thing as below
	char tmp = letters[0];
	letters[0] = letters[1];
	letters[1] = tmp;
    }

    // Same with the words. Swap them if they're out of order
    if (words[1] < words[0]) {
	// You can use std::swap(words[0], words[1]) here too
	std::string tmp = words[0];
	words[0] = words[1];
	words[1] = tmp;
    }

    // Print the output

    std::cout<< letters[0] << " goes before " << letters[1] << std::endl;
    std::cout<< words[0] << " goes before " << words[1] << std::endl;

    // A fair C++ replacement for "system("pause")" for Windows. Or a way to pause the program.
    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.	
}

This is a Letter and Word sorting program

Enter two different letters separated by a space: a b
Enter two different words separated by a space: Andy Dave


a goes before b
Andy goes before Dave


 Press Enter to continue:

Thank you so much, this will teach me how to work with future projects and thank you foe giving me the time to explain how this code works.
Topic archived. No new replies allowed.