0

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;
 }
1
  • To explain: the comma-operator in C++ evaluates the expression x, y by evaluating x, ignoring whatever it evaluates to, then evaluating y and using it in whatever context x, y appeared. For example: int x = 3 + f(), 4 * 2; evaluates 3 + f() - so if f() contains say std::cout << "hi!\n"; return 42; you'd get "hi\n" output, but then it throws away the 3 + 42 = 45 value and proceeds to evaluate 4 * 2, yielding 8, and that is the value assigned into x. Something similar is happening for return Maximum, relate; - effectively Maximum, is thrown away and ignored. Commented Feb 18, 2014 at 5:21

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.