abc order

Hello once again,

I got first and second name working, but when i add third in if statement it just die on me. Am I not allow to add 2nd < in if? I tried other way, but seem it doesn't. I only can do what I learn from 3 chapter, forgive me for not using some different code for 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
32
33
34
35
  #include "std_lib_facilities.h"

int main()

{
	
	cout << "Hello please give us three name.\n";
	
		string first_name;
		
		string second_name;
		
		string third_name;
		
	cout << "First name = ";
	
				cin >> first_name;
			
	cout << "Second name = ";
	
				cin >> second_name;
			
	cout << "Third name = ";
	
				cin >> third_name;
			
	cout << "\n";
	
	
			if(first_name < second_name && second_name < third_name)
			
	cout << "Those are three name in abc order = " << first_name << second_name << third_name << "\n";
			
	
}


edited: to correction with the example showed
Last edited on
You can't do "if(first_name < second_name < third_name)" because the first part "first_name < second_name" results in a boolean value and there is no way to determine if a string is larger than a boolean.

In your question you say that you can't add a second if, this is not true. You can do this in several ways, for example:
1
2
3
4
5
6
7
if (first_name < second_name)
{
         if (second_name < third_name)
         {
                    cout << "Those are three name in abc order = " << first_name << second_name << third_name << "\n";
         }
}


or

1
2
3
4
if (first_name < second_name && second_name < third_name)
{
         cout << "Those are three name in abc order = " << first_name << second_name << third_name << "\n";
}


In the last one, the && means "and", so the if statement is only true if both second name is larger than first name and third name is larger than second name.

Kind regards, Nico
Hello, i tried both of example you gave.

it seem cout << doesn't show up at all. There no display in cmd at all.
closed account (21vXjE8b)
The condition, in the code, to print something is that first_name < second_name and second_name < third name.
If, for example, first_name > second_name, it won't display anything.
That is strange. What values did you provide for the strings?

Try this code and you will find that it works.
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
#include <iostream>
#include <string>     // std::string, std::stoi

int main()
{
    std::string first_name = "apple";
    std::string second_name = "boat";
    std::string third_name = "canoe";

    if (first_name < second_name && second_name < third_name)
    {
        std::cout << "Those are three name in abc order = " << first_name << second_name << third_name << "\n";
    }
    else std::cout << "The second name is not larger that the first name or the third name is not larger than the second name.\n";

    // and in another order:
    first_name = "Xenon";
    second_name = "Fluor";
    third_name = "Argon";

    if ( (first_name > second_name || second_name > third_name) == false)
    {
        std::cout << "Those are three name in abc order = " << first_name << second_name << third_name << "\n";
    }
    else std::cout << "The first name is larger than the second name or the second name is not larger than the third name.\n";

    // and in yet another order:
    first_name = "Apple";
    second_name = "Milk";
    third_name = "Bread";

    if (first_name < second_name)
    {
         if (second_name < third_name)
         {
                    std::cout << "Those are three name in abc order = " << first_name << second_name << third_name << "\n";
         }
         else std::cout << "The third name is not larger that the second name.\n";
    }
    else std::cout << "The second name is not larger that the first name.\n";
}


Kind regards, Nico
Last edited on
Im too dumb to understand, i tried what book showed, it doesnt work now. I need a example. book show this... its same what i did, but somehow mine just did different. I wanted to add 2nd < to if. seem like i cant do that.


book show this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "std_lib_facilities.h"

int main()

{
	cout << "please enter two names\n";
	string first;
	string second;
	cin >> first >> second;
	if (first == second) cout << "that's the same name twice\n";
	if (first < second)
	cout << first << "is alphabetically before" << second << '\n';
	if (first > second)
	cout << first << " is alphabetically after " << second <<'\n';
}
closed account (21vXjE8b)
Before start developing the code, do a sketch of the problem.

Let's do it... string first will be number 1, string second will be number 2 and string third will be number 3.

You have 3 variables, so you'll have 3! = 3.2.1 = 6 possibilities (so 6 If's statements, ok?).

The possibilities are:
1 < 2 < 3 ~~ 1 < 2 and 2 < 3 ~~ 1 < 2 && 2 < 3
1 < 3 < 2 ~~ 1 < 3 and 3 < 2 ~~ 1 < 3 && 3 < 2
2 < 1 < 3 ~~ 2 < 1 and 1 < 3 ~~ 2 < 1 && 1 < 3
2 < 3 < 1 ~~ 2 < 3 and 3 < 1 ~~ 2 < 3 && 3 < 1
3 < 1 < 2 ~~ 3 < 1 and 1 < 2 ~~ 3 < 1 && 1 < 2
3 < 2 < 1 ~~ 3 < 2 and 2 < 1 ~~ 3 < 2 && 2 < 1

Now implement it to your code!
1
2
3
4
if(first < second && second < third) cout << first << " " << second << " " << third;
if(first < third && third < second) cout << first << " " << third << " " << second;
if(second < first && first < third) cout << second << " " << first << " " << third;
...
Last edited on
Not yet having learned something does not make you dumb, hopefully it just makes yo eager to learn.

Personally I think what the book shows is not a very good example, I would have preferred the example to be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "std_lib_facilities.h"

int main()

{
	cout << "please enter two names\n";
	string first;
	string second;
	cin >> first >> second;
	if (first == second)    // for every test that is related, you should have only one "if(condition)" statement
	{
		cout << "that's the same name twice\n";
	}
	else if (first < second) // if can be followed by several "else if(condition)" statements
	{
		cout << first << "is alphabetically before" << second << '\n';
	}
	else                    // but for every if, you can have only one "else" statement without a condition. This one will be executed if none of the above conditions were appicable
	{
		cout << first << " is alphabetically after " << second <<'\n';
	}
}


and this is why:
1. if you use an if statement without brackets, only the next command depends on the condition, it often happens that you want to do multiple things if a condition is true (for example store a value and print it). If you use brackets, everything within the brackets depends on the condition, so if you get used to this you will not get confused if you want to do multiple things.
2. you can also tell the program what to do if a condition is not true using the else statement, because a condition is always true or not true, this means that you will always get some output.
The condition, in the code, to print something is that first_name < second_name and second_name < third name.
If, for example, first_name > second_name, it won't display anything.

This is because like xSoloDrop stated, there are more outcomes than the ones you were testing for. If you had used an else statement, you would also have gotten an output if condition that you provided was not listed.

So to overcome your problem, make sure that your program knows what to do in every possible condition, either by listing all conditions, or by using the else statement (or both using the "else if").

Note that the number of possibilities will grow fast, with 2 names there were 3 options, with 3 names there are 9, with 4 names there will be 16 options, with x names there will be 2^(x) possibilities (also including the options where variables can be equal).
To list them all becomes unattractive very fast. Later you will probably learn about using functions, that will make solving this problem possible for large numbers of names too.

Kind regards, Nico
Last edited on
Thanks guy, it work ^ ^. I see now, it refuse to show up, because it has more than one to show so it cant. So I add all possibilities. It show up



edited: this week I'll be working on book a lot. Thank for all the help in advance.

too bad I cant afford college for this. But this is best teaching website I found o.O.
Last edited on
Topic archived. No new replies allowed.