user3321794
user3321794

Reputation: 1

C++ Returning two values in Array

I am almost done with my code; however, one part is not working well. Simply, reading from a file that contains only numbers(ex. cars sold). by using an array, trying to get the total of those numbers, max, and the number of the max index. My question is: how can I return the two values from my MaxSold function? it returns only the max added to the index which is not correct. the result should point to the employee number then the max. This is my code so far:

#include <iostream>
#include <fstream>
/*#include <vector>
#include <iomanip>*/
void Read(int arryList[], int size);
void Print(int arryList[], int size);
int total(int arryList[], int size);
int MaxSold(int arryList[], int size, int& number);
using namespace std;
ifstream inFile("C:\\cars.dat");
int main()
 {
 int cars[7];
 int i;
Read(cars,7);
Print(cars,7);
cout<<"The total of sold cars is: "<<total(cars, 7)<< "\n";
cout<<"The Max "<< MaxSold(cars, 7, i);
 }
  void Read(int arryList[], int size){
    for(int i = 0; i < 7; i++)
{
    inFile >> arryList[i];

}
return;
}
void Print(int arryList[], int size){
for (int i = 0; i < 7; i++){

    cout  << i + 1 << "-"<< arryList[i] << "\n";

}
return ;
 }

 int total(int arryList[], int size){
 int sum = 0;
  for (int i = 0; i < size; i++){


 sum +=arryList[i];

 }
 return sum;
 }
  int MaxSold(int arryList[], int size, int& number){
  int Maximum= 0;
  int relate=0;
  for( int i=0 ; i<7 ; i++){

   if (arryList[i] > Maximum){
    Maximum = arryList[i];
    relate = i+1;
   }
  }
  return Maximum, relate;
 }

Upvotes: 0

Views: 85

Answers (2)

Ed Swangren
Ed Swangren

Reputation: 124790

You cannot return more than one value from a function. Of course, that value can be a container for multiple values. It can be your own custom type, but the simplest way would be to return a std::pair<int,int>.

std::pair<int, int> MaxSold(int arryList[], int size, int& number)
{
    // ...
    return std::make_pair(Maximum, relate);
}

Upvotes: 1

P0W
P0W

Reputation: 47844

Use std::pair

#include<utility>
//..
std::pair<int,int>  MaxSold(int arryList[], int size, int& number)
{
    //...
    return std::make_pair( Maximum, relate );
}

Then,

std::pair<int,int> p = MaxSold(cars, 7, i) ; 

std::cout<< p.first ;  //maximum
std::cout<< p.second ; //relate

Upvotes: 1

Related Questions