-4

I dont know how to make this book exercise that wants me to print out three integers or three strings in numerical/alphabetical order.

I've tried using if statements to solve this but it just fails because im a beginner.

with code like this if(a

cout << a << b << c << endl;

expected results is the numbers i typed in but printed out in numerical order.

#include <iostream> 
#include <string> 
using namespace std; 
int main() { 
    cout << "Enter three whole numbers" << endl; int a,b,c; cin >> a >> b >> c; 
    if(a<b<c) { 
        cout << a << "," << b << "," << c << endl; 
    } 
    if(b<a<c) { 
        cout << b << "," << a << "," << c << endl; 
    } 
    if(c<a<b) { 
        cout << c << "," << a << "," << b << endl; 
    } 
    return 0; 
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190

1 Answers1

0
#include <iostream>
#include <utility>

int main() { 
    std::cout << "Enter three whole numbers" << std::endl; int a,b,c; std::cin >> a >> b >> c;
    if (a > b) std::swap(a, b); // a < b ? c
    if (b > c) std::swap(b, c); // a ? b < c and a < c
    if (a > b) std::swap(a, b); // a < b < c
    std::cout << a << "," << b << "," << c << std::endl;
    return 0; 
}
273K
  • 29,503
  • 10
  • 41
  • 64