Hello, I need help printing my results to an output file with the program that I have below. I am getting an error "too few arguements to function". It has been a couple of years since I did C++ using .txt, so any help is appreciated. Thank you.

Goals:

- Display the high and low temperatures for each month
- Display highest temperature average out of all months
- Display lowest temperature average out of all months
- Display highest temperature out of all months
-Display lowest temperature out of all months.

My input.txt file: 50 33 45 25 55 32 60 50 75 60 80 74 92 75 98 78 85 60 70 53 50 35 53 20


Example of output I wish to create:

Jan 60 23
Feb 55 20
.
.
Dec 60 25

The average high temperature is 57.50
The average low temperature is 21.50
The highest temperature is 60
The lowest temperature is 20

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;


void getdata(ifstream& inData,ofstream& outData);
double averagehigh(int temperature[][2],double average_highs,ifstream& inData,int lowest_temp,int highest_temp);
double averagelow(int temperature[][2],double average_lows,ifstream& inData,int lowest_temp,int highest_temp);
int indexHighTemp(int temperature[][2],int highest_temp,int lowest_temp,ifstream& inData);
int indexLowTemp(int temperature[][2],int lowest_temp,int highest_temp,ifstream& inData);


int main()
{
 
int Temp[12][2] = {{50,33},{45,25},{55,32},{60,50},{75,60},{80,74},{92,75},{98,78},{85,60},{70,53},{50,35},{53,20}};
string month[12][12] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"};
int temp_highs;
int temp_lows;
int highest;
int lowest;
double average_high;
double average_low;

ifstream inData;
ofstream outData;


 while (inData>>temp_highs>>temp_lows)
 {
   outData<<month<<" "<<temp_highs<<temp_lows<<endl;
 } 


outData<<"The average high temperature is "<<averagehigh(Temp,average_high,inData)<<endl;
outData<<"The average low temperature is "<<averagelow(Temp,average_low,inData)<<endl;
outData<<"The highest temperature is "<<indexHighTemp(Temp,temp_highs,inData)<<endl;
outData<<"The lowest temperature is "<<indexLowTemp(Temp,temp_lows,inData)<<endl;



inData.close();
outData.close();

system ("PAUSE");
return 0;
}




void getdata(ifstream& inData,ofstream& outData)
{
   inData.open ("input.txt"); // file the contains the numbers
   outData.open ("input_output.txt"); // file that contain the results
}     
     

double averagehigh(int temperature[][2], double average_highs,ifstream& inData,ofstream& outData,int lowest_temp,int highest_temp)
{
       
int sum = 0;  

 while (inData>>highest_temp>>lowest_temp)
  {
   for (int i=0; i<2; i++)
    {
    for (int j=0; j<12; j++)
    {
    sum+=highest_temp;       
    average_highs = ( sum / 12 );
    }
    }
  }
  
  return average_highs;
        
       
}

double averagelow(int temperature[][2], double average_lows,ifstream& inData,ofstream& outData,int lowest_temp,int highest_temp)
{
       
int sum = 0;

  while (inData>>highest_temp>>lowest_temp)
   {
     for (int i=0; i<2; i++)
     {
     for (int j=0; j<12; j++)
     {
     sum+=lowest_temp;       
     average_lows = ( sum / 12 );
     }
     }
    }
      
     return average_lows;
       
}

int indexHighTemp(int temperature[][2], int highest_temp, int lowest_temp,ifstream& inData,ofstream& outData)
{
       
      while (inData>>highest_temp>>lowest_temp)
      {
          for (int i=0; i<2; i++)
          {
             for (int j=0; j<12; j++)
          {
            if(temperature[i][j] > highest_temp)
            highest_temp = temperature[i][j];
          }
          }
      }   
       
       return highest_temp;
       
}


int indexLowTemp(int temperature[][2], int lowest_temp, int highest_temp,ifstream& inData,ofstream& outData)
{
       
     
      while (inData>>highest_temp>>lowest_temp)
      {
         for (int i=0; i<12; i++)
          {
             for (int j=0; j<2; j++)
          {
            if(temperature[i][j] < lowest_temp)
            lowest_temp = temperature[i][j];
          }
          }
      }     
       
     return lowest_temp;
       
}

line 27 and 28: you failed to open the files

ifstream inData("myfile.txt");
ofstream outData("outfile.txt");

>>"too few arguements to function

Carefully check and recheck the argument list in the function prototypes that appear at the top of your program with the parameter list that is passed on lines 37-40. There apparently are some discrepancies.

NinjaLink,

Take a look at function definition,

double averagehigh(
                   int temperature[][2],           // 1st argument
                   double average_highs,       // 2nd
                   ifstream& inData,                // 3rd
                   ofstream& outData,            // 4th
                   int lowest_temp,                // 5th
                   int highest_temp                // 6th
                   )

and the declaration of same function is,

double averagehigh
                  (
                  int temperature[][2],      // 1st argument
                  double average_highs,   // 2nd
                  ifstream& inData,            // 3rd  -- 4th missing
                  int lowest_temp,             // 5th
                  int highest_temp             // 6th
                  );

Syntax of called function (line #37) inside the main is,

outData<<"The average high temperature is "
        << averagehigh
             ( 
              Temp,                 // 1st
              average_high,    // 2nd
              inData                // 3rd ---4th,5th,6th missing
              )
        <<endl;

Thank you for your replys. I have corrected the errors reported above to the best of my ability.

However, I am now receiving 2 errors that I do not know how to correct and need help with. My updated program is below

" invalid initialization of reference of type 'std:;ifstream&' from expression of type 'std::ofstream' in passing argument 4 of 'double averagehigh(int(*)[22], double, std::ifstream&, std::ifstream&, int, int)' invalid initialization of reference of type 'std::ifstream&' from ex for lines " (37,9) and (38,10)

and


" invalid initialization of reference of type 'std:;ifstream&' from expression of type 'std::ofstream' in passing argument 5 of 'double averagehigh(int(*)[22], double, std::ifstream&, std::ifstream&, int, int)' invalid initialization of reference of type 'std::ifstream&' from ex for lines " for lines (39,11), (49,12)

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;


void getdata(ifstream& inData,ofstream& outData);
double averagehigh(int temperature[][2],double average_highs,ifstream& inData,ifstream& outData,int lowest_temp,int highest_temp);
double averagelow(int temperature[][2],double average_lows,ifstream& inData,ifstream& outData,int lowest_temp,int highest_temp);
int indexHighTemp(int temperature[][2],int highest_temp,int lowest_temp,ifstream& inData,ifstream& outData);
int indexLowTemp(int temperature[][2],int lowest_temp,int highest_temp,ifstream& inData,ifstream& outData);


int main()
{
 
int Temp[12][2] = {{50,33},{45,25},{55,32},{60,50},{75,60},{80,74},{92,75},{98,78},{85,60},{70,53},{50,35},{53,20}};
string month[12][12] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"};
int temp_highs;
int temp_lows;
int highest;
int lowest;
double average_high;
double average_low;

ifstream inData("input.txt");
ofstream outData("output.txt");


 while (inData>>temp_highs>>temp_lows)
 {
   outData<<month<<" "<<temp_highs<<temp_lows<<endl;
 } 


outData<<"The average high temperature is "<<averagehigh(Temp,average_high,inData,outData,temp_lows,temp_highs)<<endl;
outData<<"The average low temperature is "<<averagelow(Temp,average_low,inData,outData,temp_lows,temp_highs)<<endl;
outData<<"The highest temperature is "<<indexHighTemp(Temp,temp_highs,temp_lows,inData,outData)<<endl;
outData<<"The lowest temperature is "<<indexLowTemp(Temp,temp_lows,temp_highs,inData,outData)<<endl;



inData.close();
outData.close();

system ("PAUSE");
return 0;
}




void getdata(ifstream& inData,ofstream& outData)
{
   inData.open ("input.txt"); // file the contains the numbers
   outData.open ("output.txt"); // file that contain the results
}     
     

double averagehigh(int temperature[][2], double average_highs,ifstream& inData,ifstream& outData,int lowest_temp,int highest_temp)
{
       
int sum = 0;  

 while (inData>>highest_temp>>lowest_temp)
  {
   for (int i=0; i<2; i++)
    {
    for (int j=0; j<12; j++)
    {
    sum+=highest_temp;       
    average_highs = ( sum / 12 );
    }
    }
  }
  
  return average_highs;
        
       
}

double averagelow(int temperature[][2], double average_lows,ifstream& inData,ifstream& outData,int lowest_temp,int highest_temp)
{
       
int sum = 0;

  while (inData>>highest_temp>>lowest_temp)
   {
     for (int i=0; i<2; i++)
     {
     for (int j=0; j<12; j++)
     {
     sum+=lowest_temp;       
     average_lows = ( sum / 12 );
     }
     }
    }
      
     return average_lows;
       
}

int indexHighTemp(int temperature[][2], int highest_temp, int lowest_temp,ifstream& inData,ofstream& outData)
{
       
      while (inData>>highest_temp>>lowest_temp)
      {
          for (int i=0; i<2; i++)
          {
             for (int j=0; j<12; j++)
          {
            if(temperature[i][j] > highest_temp)
            highest_temp = temperature[i][j];
          }
          }
      }   
       
       return highest_temp;
       
}


int indexLowTemp(int temperature[][2], int lowest_temp, int highest_temp,ifstream& inData,ofstream& outData)
{
       
     
      while (inData>>highest_temp>>lowest_temp)
      {
         for (int i=0; i<12; i++)
          {
             for (int j=0; j<2; j++)
          {
            if(temperature[i][j] < lowest_temp)
            lowest_temp = temperature[i][j];
          }
          }
      }     
       
     return lowest_temp;
       
}

Check the parameter list again -- according to function prototype the 4th parameter for averagehigh() is supposed to be ifstream but you are passing ofstream. You need to correct one of the two.

Thank you. I was able to print information to the output file. However, I am unable to display the months. I am receiving "0x22fcc0" in place of each month next to the high and low temperatures.

0x22fcc0 50 33
0x22fcc0 45 25
0x22fcc0 55 32
0x22fcc0 60 50
0x22fcc0 75 60
0x22fcc0 80 74
0x22fcc0 92 75
0x22fcc0 98 78
0x22fcc0 85 60
0x22fcc0 70 53
0x22fcc0 50 35
0x22fcc0 53 20


The code that i currently have to print the months is

" while (inData>>temp_highs>>temp_lows)
{
outData<<month<<" "<<temp_highs<<" "<<temp_lows<<endl;
}
"


I tried to use a for loop, but "0x22fcc0" multiplied several times. I don't know any where else to print out the month in the program Where do I need to check to correct this problem.

Also, my calculations didn't come out pretty either. With the code that I have so far with my calculations, am I on the right track? Do you know where I need to look?

My updated program is listed below. Thank you for your help

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;


void getdata(ifstream& inData,ofstream& outData);
double averagehigh(int temperature[][2],double average_highs,ifstream& inData,ofstream& outData,int lowest_temp,int highest_temp);
double averagelow(int temperature[][2],double average_lows,ifstream& inData,ofstream& outData,int lowest_temp,int highest_temp);
int indexHighTemp(int temperature[][2],int highest_temp,int lowest_temp,ifstream& inData,ofstream& outData);
int indexLowTemp(int temperature[][2],int lowest_temp,int highest_temp,ifstream& inData,ofstream& outData);


int main()
{
 
int Temp[12][2];
string month[12][12] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"};
int temp_highs;
int temp_lows;
int highest;
int lowest;
double average_high;
double average_low;



ifstream inData("input.txt");
ofstream outData("output.txt");

 while (inData>>temp_highs>>temp_lows)
 {
       outData<<month<<" "<<temp_highs<<" "<<temp_lows<<endl;
 } 


outData<<"The average high temperature is "<<averagehigh(Temp,average_high,inData,outData,temp_lows,temp_highs)<<endl;
outData<<"The average low temperature is "<<averagelow(Temp,average_low,inData,outData,temp_lows,temp_highs)<<endl;
outData<<"The highest temperature is "<<indexHighTemp(Temp,temp_highs,temp_lows,inData,outData)<<endl;
outData<<"The lowest temperature is "<<indexLowTemp(Temp,temp_lows,temp_highs,inData,outData)<<endl;



inData.close();
outData.close();

system ("PAUSE");
return 0;
}




void getdata(ifstream& inData,ofstream& outData)
{
   inData.open ("input.txt"); // file the contains the numbers
   outData.open ("output.txt"); // file that contain the results
}     
     

double averagehigh(int temperature[][2], double average_highs,ifstream& inData,ofstream& outData,int lowest_temp,int highest_temp)
{
       
int sum = 0;  

 while (inData>>highest_temp>>lowest_temp)
  {
 //  for (int i=0; i<2; i++)
 //   {
 //   for (int j=0; j<12; j++)
 //   {
    sum+=highest_temp;       
    average_highs = ( sum / 12 );
   // }
  //  }
  }
  
  return average_highs;
        
       
}

double averagelow(int temperature[][2], double average_lows,ifstream& inData,ofstream& outData,int lowest_temp,int highest_temp)
{
       
int sum = 0;

  while (inData>>highest_temp>>lowest_temp)
   {
 //    for (int i=0; i<2; i++)
   //  {
   //  for (int j=0; j<12; j++)
   //  {
     sum+=lowest_temp;       
     average_lows = ( sum / 12 );
  //  }
  //   }
    }
      
     return average_lows;
       
}

int indexHighTemp(int temperature[][2], int highest_temp, int lowest_temp,ifstream& inData,ofstream& outData)
{
       
      while (inData>>highest_temp>>lowest_temp)
      {
          for (int i=0; i<2; i++)
          {
            for (int j=0; j<12; j++)
          {
            if(temperature[i][j] > highest_temp)
            highest_temp = temperature[i][j];
          }
          }
      }   
       
       return highest_temp;
       
}


int indexLowTemp(int temperature[][2], int lowest_temp, int highest_temp,ifstream& inData,ofstream& outData)
{

      
      while (inData>>highest_temp>>lowest_temp)
      {
         for (int i=0; i<12; i++)
          {
             for (int j=0; j<2; j++)
          {
                     
            if(temperature[i][j] < lowest_temp)
            lowest_temp = temperature[i][j];
          }
          }
      }     
       
     return lowest_temp;
       
}

>>outData<<month<<" "<<temp_highs<<" "<<temp_lows<<endl;

month is a 2d array of strings. How else do you expect that statement to behave??? If you want it to print the month name then you have to tell it which one, like month[0].

I took your advice and changed "month" to a regular array. I didn't notice that I had it in a two dimensional array. I have the information printed to the screen, but I have been trying for about an hour to get the screen to look like this:

Jan 50 33
Feb 45 25
etc.


but it is looking like this:

"Jan 50 33 Feb 50 33 Mar 50 33 etc."


I have switched the code numerous of times inside and outside of the loop and sometimes I would get something like this:

Jan 50 33
Feb 50 33
etc.


My text input file:

50 33
45 25
55 32
60 50
75 60
80 74
92 75
98 78
85 60
70 53
50 35
53 20


The loop that I am targeting is this:


"while (inData>>temp_highs>>temp_lows)
{
for (int i = 0; i < 12; i++)
{
outData<<month<<" "<<temp_highs<<" "<<temp_lows<<" ";
}

}"

Do anything look wrong in my code? Do you have any suggestions? Thanks

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;


void getdata(ifstream& inData,ofstream& outData);
double averagehigh(int temperature[][2],double average_highs,ifstream& inData,ofstream& outData,int lowest_temp,int highest_temp);
double averagelow(int temperature[][2],double average_lows,ifstream& inData,ofstream& outData,int lowest_temp,int highest_temp);
int indexHighTemp(int temperature[][2],int highest_temp,int lowest_temp,ifstream& inData,ofstream& outData);
int indexLowTemp(int temperature[][2],int lowest_temp,int highest_temp,ifstream& inData,ofstream& outData);


int main()
{
 
int Temp[12][2];
string month[12] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"};
int temp_highs;
int temp_lows;
int highest;
int lowest;
double average_high;
double average_low;



ifstream inData("input.txt");
ofstream outData("output.txt");

 while (inData>>temp_highs>>temp_lows)
{
  for (int i = 0; i < 12; i++)
  {
       outData<<month[i]<<" "<<temp_highs<<" "<<temp_lows<<" ";
  }     

}


outData<<"The average high temperature is "<<averagehigh(Temp,average_high,inData,outData,temp_lows,temp_highs)<<endl;
outData<<"The average low temperature is "<<averagelow(Temp,average_low,inData,outData,temp_lows,temp_highs)<<endl;
outData<<"The highest temperature is "<<indexHighTemp(Temp,temp_highs,temp_lows,inData,outData)<<endl;
outData<<"The lowest temperature is "<<indexLowTemp(Temp,temp_lows,temp_highs,inData,outData)<<endl;



inData.close();
outData.close();

system ("PAUSE");
return 0;
}




void getdata(ifstream& inData,ofstream& outData)
{
   inData.open ("input.txt"); // file the contains the numbers
   outData.open ("output.txt"); // file that contain the results
}     
     

double averagehigh(int temperature[][2], double average_highs,ifstream& inData,ofstream& outData,int lowest_temp,int highest_temp)
{
       
int sum = 0;  

 while (inData>>highest_temp>>lowest_temp)
  {
 //  for (int i=0; i<2; i++)
 //   {
 //   for (int j=0; j<12; j++)
 //   {
    sum+=highest_temp;       
    average_highs = ( sum / 12 );
   // }
  //  }
  }
  
  return average_highs;
        
       
}

double averagelow(int temperature[][2], double average_lows,ifstream& inData,ofstream& outData,int lowest_temp,int highest_temp)
{
       
int sum = 0;

  while (inData>>highest_temp>>lowest_temp)
   {
 //    for (int i=0; i<2; i++)
   //  {
   //  for (int j=0; j<12; j++)
   //  {
     sum+=lowest_temp;       
     average_lows = ( sum / 12 );
  //  }
  //   }
    }
      
     return average_lows;
       
}

int indexHighTemp(int temperature[][2], int highest_temp, int lowest_temp,ifstream& inData,ofstream& outData)
{
       
      while (inData>>highest_temp>>lowest_temp)
      {
          for (int i=0; i<2; i++)
          {
            for (int j=0; j<12; j++)
          {
            if(temperature[i][j] > highest_temp)
            highest_temp = temperature[i][j];
          }
          }
      }   
       
       return highest_temp;
       
}


int indexLowTemp(int temperature[][2], int lowest_temp, int highest_temp,ifstream& inData,ofstream& outData)
{

      
      while (inData>>highest_temp>>lowest_temp)
      {
         for (int i=0; i<12; i++)
          {
             for (int j=0; j<2; j++)
          {
                     
            if(temperature[i][j] < lowest_temp)
            lowest_temp = temperature[i][j];
          }
          }
      }     
       
     return lowest_temp;
       
}

Anyone have any suggestions? I have been stuck for hours trying to display the months in my last post

Each line in the data file represents the high and low temps for the corresponding month, such as the first line is for January, second line for Feb, third line is March, etc. So all you have to do is have an integer that counts the lines read. So the first line read would display month[0]

int count = 0;
 while (inData>>temp_highs>>temp_lows)
{
       outData<<month[count]<<" "<<temp_highs<<" "<<temp_lows
            <<"\n"; // new line here
      ++count; // increment the counter

}

Thanks again.

For the calculations, am I on the right track with the for loops or am I doing it completely wrong for functions: averagehigh, averagelow, indexHighTemp, and indexLowTemp? Currently, I am receiving ugly numbers.

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;


void getdata(ifstream& inData,ofstream& outData);
double averagehigh(int temperature[][2],double average_highs,ifstream& inData,ofstream& outData,int lowest_temp,int highest_temp);
double averagelow(int temperature[][2],double average_lows,ifstream& inData,ofstream& outData,int lowest_temp,int highest_temp);
int indexHighTemp(int temperature[][2],int highest_temp,int lowest_temp,ifstream& inData,ofstream& outData);
int indexLowTemp(int temperature[][2],int lowest_temp,int highest_temp,ifstream& inData,ofstream& outData);


int main()
{
 
int Temp[12][2];
string month[12] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"};
int temp_highs;
int temp_lows;
int highest;
int lowest;
double average_high;
double average_low;


ifstream inData("input.txt");
ofstream outData("output.txt");

int count = 0; while (inData>>temp_highs>>temp_lows)
{       outData<<month[count]<<" "<<temp_highs<<" "<<temp_lows           
 <<"\n"; // new line here      
 ++count; // increment the counter 
 }

outData<<"The average high temperature is "<<averagehigh(Temp,average_high,inData,outData,temp_lows,temp_highs)<<endl;
outData<<"The average low temperature is "<<averagelow(Temp,average_low,inData,outData,temp_lows,temp_highs)<<endl;
outData<<"The highest temperature is "<<indexHighTemp(Temp,temp_highs,temp_lows,inData,outData)<<endl;
outData<<"The lowest temperature is "<<indexLowTemp(Temp,temp_lows,temp_highs,inData,outData)<<endl;



inData.close();
outData.close();

system ("PAUSE");
return 0;
}




void getdata(ifstream& inData,ofstream& outData)
{
   inData.open ("input.txt"); // file the contains the numbers
   outData.open ("output.txt"); // file that contain the results
}     
     

double averagehigh(int temperature[][2], double average_highs,ifstream& inData,ofstream& outData,int lowest_temp,int highest_temp)
{
       
int sum = 0;  

 while (inData>>highest_temp>>lowest_temp)
  {
 //  for (int i=0; i<2; i++)
 //   {
 //   for (int j=0; j<12; j++)
 //   {
    sum+=highest_temp;       
    average_highs = ( sum / 12 );
   // }
  //  }
  }
  
  return average_highs;
        
       
}

double averagelow(int temperature[][2], double average_lows,ifstream& inData,ofstream& outData,int lowest_temp,int highest_temp)
{
       
int sum = 0;

  while (inData>>highest_temp>>lowest_temp)
   {
 //    for (int i=0; i<2; i++)
   //  {
   //  for (int j=0; j<12; j++)
   //  {
     sum+=lowest_temp;       
     average_lows = ( sum / 12 );
  //  }
  //   }
    }
      
     return average_lows;
       
}

int indexHighTemp(int temperature[][2], int highest_temp, int lowest_temp,ifstream& inData,ofstream& outData)
{
       
      while (inData>>highest_temp>>lowest_temp)
      {
          for (int i=0; i<2; i++)
          {
            for (int j=0; j<12; j++)
          {
            if(temperature[i][j] > highest_temp)
            highest_temp = temperature[i][j];
          }
          }
      }   
       
       return highest_temp;
       
}


int indexLowTemp(int temperature[][2], int lowest_temp, int highest_temp,ifstream& inData,ofstream& outData)
{

      
      while (inData>>highest_temp>>lowest_temp)
      {
         for (int i=0; i<12; i++)
          {
             for (int j=0; j<2; j++)
          {
                     
            if(temperature[i][j] < lowest_temp)
            lowest_temp = temperature[i][j];
          }
          }
      }     
       
     return lowest_temp;
       
}

Are there anyone available to give me suggestions my calculations?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.