lundi 9 novembre 2020

C++, function with changing amount of inputs (idk how to describe)

The funcion HIGHEST is all good and accepts variable number of arguments. The problem is how do I do variable amount of input? (idk what terms should be use to describe it, but examples should clear things up...)

/*cout << highest(n, A[0],A[1],A[2],A[3]) <<endl;
this is what I would like to achieve, but with a change - the number of array changes with the input file and n defines how many of these will be. ex.: if n = 7 it needs to be (n, A[0],A[1],A[2],A[3], A[4], A[5], A[6]) */

Duom.txt file if needed (i am now only working with 1st number 4 under it others will follow same/similar thing as 1st row they are not important now)

4

2008 5 2000.00 400.25

2010 5 4000.00 320.25

2009 9 3385.00 254.45

2008 6 1900.00 612.59

#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <cstdarg>

using namespace std;
//Variable number of arguments in C++
int highest (int num,...) {
   va_list valist;
   int max = 0;
   int a;
   va_start(valist, num);
   for (int i = 0; i < num; i++) {
        a = va_arg(valist, int);
        if(max < a) max = a;
        else ;
   }
   va_end(valist);
   return max;
}




int main () {
int n, A[25], B[25];
double C[25], D[25];

ifstream fd ("Duom.txt");
ofstream fr ("Rez.txt");
fd >> n;
cout << n <<endl;
for(int i = 0; i < n; i++){
    fd >> A[i] >> B[i] >> C[i] >> D[i];
}
cout << highest(n, ) <<endl;
/*cout << highest(n, A[0],A[1],A[2],A[3]) <<endl;  
this is what I would like to achieve, 
but with a change - the number of array changes with 
the input file and n defines how many of these will be. 
ex.: if n = 7 it needs to be (n, A[0],A[1],A[2],A[3], A[4], A[5], A[6])
*/


    return 0;
}

Aucun commentaire:

Enregistrer un commentaire