0

Good day guys, I'm a beginner of c++ programming, and suffering from a petty problem.

This code is to set typed values upperbound.

#include "../../std_lib_facilities.h"

int change(int a, int b) {
    if (a > b) {
        int c = a; a = b; b = c;
    }
    return a, b;
}

int main() {
    int a, b, c;
    while (cout << "Type 3 numbers: \n" && cin >> a >> b >> c) {
    a,b = change(a, b);
    b,c = change(b, c);
    a,b = change(a, b);
    cout << a << ", " << b << ", " << c << "\n";
    }
    simple_error("You typed invalid number.\n");
}

In this case, for instance, when I type '2 5 3' it returns '2, 5, 5' not '2, 3, 5' as expected. I found that

b,c = change(b, c);

this line has problem debugging by cout, but I don't know the reason. How could I solve it? Thanks for reading.

noError
  • 21
  • 2
  • 2
    There's a bunch of issues in your code, but if you turn on warnings, you'll catch most of them. Also, `change` looks like it's just implementing a form of `std::swap`. – cigien May 19 '21 at 23:39
  • Thank you! wish I could know many of functions in c++. – noError May 19 '21 at 23:44
  • https://stackoverflow.com/questions/16713245/whats-the-best-way-to-return-a-tuple-from-function-in-c11 could be considered duplicate depending on whether you need an explanation of "operator ," which this code uses or actually show how to return tuples/deconstruction assignment – Alexei Levenkov May 19 '21 at 23:44
  • What behavior are you expecting with `b,c = change(b,c);`? Search your favorite C++ reference for "comma operator". – Thomas Matthews May 19 '21 at 23:48

2 Answers2

4

The statement return a, b; does not do what you think it does. The signature of your function shows us that it returns a single int, not a pair. You can't have multiple return values in C++. You could make your return value be a struct that holds two integers, but it's probably easier to use references to do what you are trying to do:

void change(int & a, int & b) {
    if (a > b) {
        int c = a; a = b; b = c;
    }
}
David Grayson
  • 84,103
  • 24
  • 152
  • 189
1

The code seems to be trying to use certain C++ idioms, but doesn't do them correctly. I've tried to correct them here for you. (Otherwise, David Grayson’s answer is also good, and also idiomatic C++.)

To do a destructured binding, you'll need to use a tie.

To return a pair of values, you'll need to return a pair or tuple. And then do a return {a, b}; to return the a and b as a pair (or tuple).

The successful input should skip doing the simple_error handler. Here, I've done it by doing an early return EXIT_SUCCESS;.

#include <cstdlib>
#include <iostream>
#include <string>
#include <tuple>
#include <utility>

using std::cin;
using std::cout;
using std::pair;
using std::string;
using std::tie;

namespace {

auto change(int a, int b) -> pair<int, int> {
    if (a > b) {
        int c = a; a = b; b = c;
    }
    return {a, b};
}

void simple_error(string const& s) {
    cout << "Error: " << s;
}

} // anon

int main() {
    int a, b, c;
    while (cout << "Type 3 numbers: \n" && cin >> a >> b >> c) {
        tie(a, b) = change(a, b);
        tie(b, c) = change(b, c);
        tie(a, b) = change(a, b);
        cout << a << ", " << b << ", " << c << "\n";
        return EXIT_SUCCESS;
    }
    simple_error("You typed invalid number.\n");
    return EXIT_FAILURE;
}
Eljay
  • 4,648
  • 3
  • 16
  • 27
  • We generally discourage code-only answers without explanations – Mooing Duck May 19 '21 at 23:54
  • @MooingDuck • In between boss fights in World of Warcraft, I've added some explanations between what the OP provided, and how to do it in C++ (assuming my inferences are correct). – Eljay May 20 '21 at 00:01