Hello, I'm new here and I could use some help please.
Im trying to learn c++ with a book called Programming Principles and Practice Using C++.
Im on Chapter 3 now, and so far not having to many problems. But i am stuck on one of the exercises at the end of the chapter.
I did do a search for this and found one result, but they way they coded it was different than mine.
The exercise is below:

Write a program that prompts the user to enter three integer values, and
then outputs the values in numerical sequence separated by commas. So,
if the user enters the values 10 4 6, the output should be 4, 6, 10. If twO
values arc the same, they should just be ordered together. So, the input 4
54 should give 4, 4, 5.

Now, i thought this would be simple, but I am having some issues with it. Some of it works and some of it doesnt.
I'm sure there are better ways of getting this to work, but as far as I know, im only supposed to use IF's.

My problem is, IF statements 1, 2 and 6 work just fine, but the 3 in the middle dont. I dont really understand why.
When I run it and i try to use 3, which i labeled BAC it works fine if the 4th IF isnt in there,but with it, it outputs both BAC and BCA. 5 does nothing at all.

Hopefully I dont confuse anyone the way i explained it. Been trying to do this for the last 3-4 hours and my brain is fried.

include "std_lib_facilities.h"

int main()
{

int a=0;
int b=0;
int c=0;

cout<<"Please type in 3 numbers.\n";
cout<<"A: "; cin>>a; cout<<"\n";
cout<<"B: "; cin>>b; cout<<"\n";
cout<<"C: "; cin>>c; cout<<"\n";


cout<<"You typed in "<<a<<" "<<b<<" "<<c<<" .\n\n";

// ABC  
// 123

if (a<b && a<c && b<c) // ABC, 123
   cout<<"A is less than B, and C, but B is less than C. ABC.\n\n";

if (a<c && a<b && c<b) // ACB, 132
   cout<<"A is less than B, and C, but C is less than B. ACB.\n\n";  

if (b<a && b<c && a<c) // BAC  213
   cout<<"B is less than A, and C, but A is less than C. BAC. \n\n"; 

if (b<a && b<c && c>a) // BCA 231
   cout<<"B is less than A, and C, but C is less than A. BCA. \n\n";


if (c<a && c<b && a<b)   //cab 312
   cout<<"woot.\n";

if (c<a && c<b && b<a)   //cba 321
   cout<<"blah.\n";

keep_window_open();
}

The third if statement is the same as the fourth if statement, so if the third if statements prints then so does the fourth.

if (b < a && b < c && a < c) is the same as if (b < a && b < c && a < c) because a < c is the same as c > a.

The fifth if statement does do something - if you enter 2, 3, 1.

Exactly what Mandrew said. And here's a tip to clean up your code. Rather than writing:

cout<<"Please type in 3 numbers.\n";
cout<<"A: "; cin>>a; cout<<"\n";
cout<<"B: "; cin>>b; cout<<"\n";
cout<<"C: "; cin>>c; cout<<"\n";

You can do this:

cout << "Please type in 3 number.\n";
cout << "A: ";
    cin >> a;
cout << "B: ";
    cin >> b;
cout << "C: ";
    cin >> c;

If you didn't know, when you do cin and hit return after each value put in, a new line is automatically created. So unless you want to have extra white space then nevermind. Also by what I have seen, it is better practice to put everything on their own lines i.e. dont put 2 lines of code on one line like you did.

In each if statement, did you want to output the text ABC or the values ABC? If you wanted to output the values here is what I would do.

if (a<b && a<c && b<c){ // ABC, 123

    cout << "A is less than B, and C, but B is less than C." << A << B << C << endl << endl; //endl requires using namespace std; to be typed above main. 

}

Also, unless you are using old compilers, just use #include <iostream>. It's all you would need for this code. Oh and make sure you have return 0; before the closing bracket so that to program knows it has hit the end of the program without an error.

I'm fairly new to programming myself but... If it only needs to sort three numbers this might be a better way to do it.

include <iostream>

using namespace std;

int main()
{
int a, b, c;
cin >> a >> b >> c;
int temp;

if (a > b)
{
    temp = a;
    a = b;
    b = temp;
}

if (b > c)
{
    temp = b;
    b = c;
    c = temp;
}

if (a > b)
{
    temp = a;
    a = b;
    b = temp;
}

cout << a << ", " << b << ", " << c << ", " << endl;
return 0;

}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.