0
//libraries
#include <iostream>

//standard namepace
using namespace std;


int Car() {
    int a;
    int b;
    
    cout << "Fuel Tank" << endl;
    cin >> a;
    cout << "MPG" << endl;
    cin >> b;

    return a, b;
}
int main() {
    int a;
    int b;
    
    a,b = Car();
    
    cout << "Print Values " << (a,b);    // <--- Line 25

    return 0;
}

Let's say you put 10 and 15 as the first and second input. Why is 15 the only variable to print in the cout statement on line 25.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
Forge Mods
  • 25
  • 1
  • 9

3 Answers3

5

That's not how C++ works.

You need:

std:: pair<int, int>  Car() {
    ...
    return {a, b};
}
auto [a, b] = Car();
std::cout << a << ", " << b;

What you have:

int Car()

Car is a function which returns 1 int.

return a, b;

Here you have the comma operator which evaluates every argument and discards all but the last one. So it returns b.

a, b = Car();
(a, b)

Again the comma operator. a is discarded and b is assigned. Then a is discarded and b is printed.

bolov
  • 72,283
  • 15
  • 145
  • 224
  • the ```auto [a, b] = Car();``` doesn't seem to be working for me I am getting "language feature 'structured bindings' requires compiler flag '/std:c++17'" – Forge Mods Jan 23 '21 at 02:46
  • You need to set C++17 in settings > C++ > language or something like that. – bolov Jan 23 '21 at 03:02
  • thank you very much it was set to 14 is that bad? – Forge Mods Jan 23 '21 at 03:07
  • I believe c++14 is the current default in Visual Studio 2019. – drescherjm Jan 23 '21 at 03:12
  • No, it's not bad. C++ is constantly evolving. There's a new version every 3 years. C++14 must be the latest fully implemented version of your Visual Studio version so that's why it's the default. Every C++ version is backwards compatible (with very few and narrow cases). – bolov Jan 23 '21 at 03:17
3

Unlike Python, C++ does not have a built in notion of a tuple. Your Car function is declared to return a single integer, and so one integer you will get. An alternative is to use std::pair<int, int> in #include <utility> like so:

std::pair<int, int> Car() {
  // ...
  return std::make_pair(a, b);
}

I assume the program compiles because in C/C++, the comma separated expression list as you wrote is evaluated in order and only the last expression or item in that list is returned. So your first Car() function returns the last integer b, and you only initialize b in your assignment in main() to that other b. Likewise, your cout only prints b, hence 15.

1

What I would recommend doing is making the function a void and passing the variables by reference. When you pass the variables by reference, you can change their value in the function and they will not be lost due to scope. This is because passing by reference references the location in memory where that variable is stored, instead of creating a copy like passing by value does (which you did).

#include <iostream>
using namespace std;

void car(int &a, int &b); // function prototype calling for a and b to be passed by 
                          // reference

int main() 
{
    int a = 0;
    int b = 0;
    
    car(a, b);
    
    cout << "Print Values " << a << " " << b;

    return 0;
}

void car(int &a, int &b) 
{
    int temp = 0;
    cout << "Fuel Tank" << endl;
    cin >> a;
    cout << "MPG" << endl;
    cin >> b;
    
}

As you see, instead of returning values, the function changes the values of the variables by accessing their location.

Edit: Typically, functions will go below main and you have prototypes for the functions above main (as seen in my example).

Another thing, you named the function, "Car();". Typically, functions start with the first word as a lowercase letter with the following words capitalized. This makes it easier to not confuse them with constructor function names for classes, in which the first letter of the first word is usually capitalized.

cmbtmstr
  • 25
  • 6