9 Replies - 1501 Views - Last Post: 09 August 2009 - 08:41 PM Rate Topic: -----

#1 sphughes77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-July 09

Expected primary expression compiler error

Post icon  Posted 09 August 2009 - 07:17 PM

I keep getting an 'expected primary expression before ',' token error on my WriteDat function call. I cannot figure it out.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;


bool ReadDat(double rainfall[]);

void CalcDat(double rainfall[], int SIZE);

void SortDat(double rainfall[], int SIZE);

void WriteDat(double rainfall[], int SIZE, ofstream results, double avgrain, double totalrain);

int main()
{
	// Opening input file
	ifstream inFile;
	
	cout << "Reading rainfall amounts.\n";
	inFile.open ("rain.txt");
	if (!inFile)
	   cout << "Error opening file!\n";
	
	// Opening/creating output file
	ofstream outFile;
		   outFile.open ("results.txt");
		   if (!outFile)
			  cout << "Error opening write file!\n";
	
	
	int SIZE = 24;
	double rainfall[SIZE];
	double totalrain;
	double avgrain;
	
	outFile << "Average Rainfall  Total Rainfall  Highest Month  Lowest Month\n";
	
	// Reading file into array
	ReadDat(rainfall);
	
	// Calculating data
	CalcDat(rainfall, SIZE);
	
	// Sorting the data
	SortDat(rainfall, SIZE);
	
	// Writing data
	WriteDat(rainfall, SIZE, ofstream, avgrain, totalrain);
	
	// Closing input file.
	inFile.close ();
	
	// Closing output file.
	outFile.close ();
	system ("pause");
}

// This function reads the rainfall data from a file
bool ReadDat(ifstream& rain, double rainfall[])
{
	 int count;
	 int SIZE = 24;
		  
	 for (count = 0; count < SIZE; count++)
		 rain >> rainfall [count];
	 if (!rain)
		return false;
	 return true, count;
}

// This function calculates the average rainfall, most rainfall, least 
// rainfall, and total rainfall for the year
void CalcDat (double rain[], const int SIZE)
{
	 int count;
	 double totalrain = 0;
	 double avgrain;
	 
	 for (count = 0; count < SIZE; count++)
		 totalrain = rain[count] + totalrain;
	 avgrain = totalrain/count;
	 return;
}

// This function sorts the data in the array
void SortDat(double array[], const int SIZE)
{
	 bool swap;
	 double rain;
	 
	 do
	 {
		 swap = false;
		 for (int count = 0; count < (SIZE - 1); count++)
		 {
			 if (array[count] > array[count + 1])
			 {
				  rain = array[count];
				  array[count] = array[count + 1];
				  array[count + 1] = rain;
				  swap = true;
			 }
		 }
	 } while (swap);
}


// This function writes the results to a file
void WriteDat(double rainfall[], const int SIZE, ofstream results, double avgrain, double totalrain)
{
	 int count;
	 
	 results << setw(5) << avgrain;
	 results << setw(5) << totalrain;
	 for (count = 0; count < SIZE; count++)
		results << rainfall[count] << endl;
	 return;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Expected primary expression compiler error

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Expected primary expression compiler error

Posted 09 August 2009 - 07:20 PM

You need to pass in "outFile" not "ofstream". The variable not the class.
Was This Post Helpful? 0
  • +
  • -

#3 sphughes77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-July 09

Re: Expected primary expression compiler error

Posted 09 August 2009 - 07:28 PM

I changed it to outFile and when I compiled it using DEV C++ it stopped compiling and opened up a file called ios_base.h Any ideas?

View Posteker676, on 9 Aug, 2009 - 06:20 PM, said:

You need to pass in "outFile" not "ofstream". The variable not the class.

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3199
  • View blog
  • Posts: 19,230
  • Joined: 14-September 07

Re: Expected primary expression compiler error

Posted 09 August 2009 - 07:38 PM

You have 4 threads all on this one program (inherently the same question(s)). Keep it in one thread please.


What were the error messages? What is the updated code? As much as I might want to, I lack the ability to read minds.
Was This Post Helpful? 0
  • +
  • -

#5 sphughes77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-July 09

Re: Expected primary expression compiler error

Posted 09 August 2009 - 07:43 PM

Sorry about that. I am new to the site. I changed the ofstream to outFile and now I am getting invalid conversion from void to double, double to int, and conversion from `int' to non-scalar type `std::ofstream' requested. All of these errors are on the WriteDat function call. Here is the updated code:
 

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;


bool ReadDat(double rainfall[]);

void CalcDat(double rainfall[], int SIZE);

void SortDat(double rainfall[], int SIZE);

void WriteDat(double rainfall[], const int SIZE, ofstream results, double avgrain, double totalrain);

int main()
{
	// Opening input file
	ifstream inFile;
	
	cout << "Reading rainfall amounts.\n";
	inFile.open ("rain.txt");
	if (!inFile)
	   cout << "Error opening file!\n";
	
	// Opening/creating output file
	ofstream outFile;
		   outFile.open ("results.txt");
		   if (!outFile)
			  cout << "Error opening write file!\n";
	
	
	int SIZE = 24;
	double rainfall[SIZE];
	double totalrain;
	double avgrain;
	
	outFile << "Average Rainfall  Total Rainfall  Highest Month  Lowest Month\n";
	
	// Reading file into array
	ReadDat(rainfall);
	
	// Calculating data
	CalcDat(rainfall, SIZE);
	
	// Sorting the data
	SortDat(rainfall, SIZE);
	
	// Writing data
	WriteDat(outFile, rainfall, SIZE, avgrain, totalrain);
	
	// Closing input file.
	inFile.close ();
	
	// Closing output file.
	outFile.close ();
	system ("pause");
}

// This function reads the rainfall data from a file
bool ReadDat(ifstream& rain, double rainfall[])
{
	 int count;
	 int SIZE = 24;
		  
	 for (count = 0; count < SIZE; count++)
		 rain >> rainfall [count];
	 if (!rain)
		return false;
	 return true, count;
}

// This function calculates the average rainfall, most rainfall, least 
// rainfall, and total rainfall for the year
void CalcDat (double rain[], const int SIZE)
{
	 int count;
	 double totalrain = 0;
	 double avgrain;
	 
	 for (count = 0; count < SIZE; count++)
		 totalrain = rain[count] + totalrain;
	 avgrain = totalrain/count;
	 return;
}

// This function sorts the data in the array
void SortDat(double array[], const int SIZE)
{
	 bool swap;
	 double rain;
	 
	 do
	 {
		 swap = false;
		 for (int count = 0; count < (SIZE - 1); count++)
		 {
			 if (array[count] > array[count + 1])
			 {
				  rain = array[count];
				  array[count] = array[count + 1];
				  array[count + 1] = rain;
				  swap = true;
			 }
		 }
	 } while (swap);
}


// This function writes the results to a file
void WriteDat(double rainfall[], const int SIZE, ofstream results, double avgrain, double totalrain)
{
	 int count;
	 
	 results << setw(5) << avgrain;
	 results << setw(5) << totalrain;
	 for (count = 0; count < SIZE; count++)
		results << rainfall[count] << endl;
	 return;
}


View PostKYA, on 9 Aug, 2009 - 06:38 PM, said:

You have 4 threads all on this one program (inherently the same question(s)). Keep it in one thread please.


What were the error messages? What is the updated code? As much as I might want to, I lack the ability to read minds.

Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3199
  • View blog
  • Posts: 19,230
  • Joined: 14-September 07

Re: Expected primary expression compiler error

Posted 09 August 2009 - 08:01 PM

Your prototype outlines the order and variable type of all parameters, that has to MATCH the function definition and your implementation. Your parameters being passed in your program are not in the correct order.
Was This Post Helpful? 0
  • +
  • -

#7 sphughes77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-July 09

Re: Expected primary expression compiler error

Posted 09 August 2009 - 08:10 PM

When I put them all in the same order and compile, the compiler, DEV C++, opens a file named ios_base.h and the active window switches to that file and it acts like it is trying to compile it. I was at this point earlier and could not get past it. Moving the outFile to the beginning of the parameters being passed in made it so this did not happen. Any ideas?

View PostKYA, on 9 Aug, 2009 - 07:01 PM, said:

Your prototype outlines the order and variable type of all parameters, that has to MATCH the function definition and your implementation. Your parameters being passed in your program are not in the correct order.

Was This Post Helpful? 0
  • +
  • -

#8 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Expected primary expression compiler error

Posted 09 August 2009 - 08:26 PM

First you need to pass in all file streams by reference. Then you need to make sure your prototypes match the function definitions.

I just compiled it after fixing those errors and it works.

bool ReadDat(ifstream&, double rainfall[]); // changed this function prototype


void WriteDat(double rainfall[], int SIZE, ofstream& results, double avgrain, double totalrain); // change the stream to a reference

int main()
{
  // no code change here
   
   // Changed here... Initialized values
	const int SIZE = 24;
	double rainfall[SIZE];
	double totalrain = 0;
	double avgrain = 0;
   
	outFile << "Average Rainfall  Total Rainfall  Highest Month  Lowest Month\n";
   
	// Reading file into array
	ReadDat(inFile, rainfall); // changed this.. you have to pass in the stream
   
	
}

// This function reads the rainfall data from a file
bool ReadDat(ifstream& rain, double rainfall[])
{
	 int count;
	 int SIZE = 24;
		 
	 for (count = 0; count < SIZE; count++)
		 rain >> rainfall [count];
	 if (!rain)
		return false;
	 return true;  // changed HERE!! you can't return two values
}

// This function calculates the average rainfall, most rainfall, least
// rainfall, and total rainfall for the year
void CalcDat (double rain[], const int SIZE)
{
	 // no changed code
}

// This function sorts the data in the array
void SortDat(double array[], const int SIZE)
{
	 // no changed code
}


// This function writes the results to a file
void WriteDat(double rainfall[], const int SIZE, ofstream& results, double avgrain, double totalrain) // passed stream in by reference
{
   //... no changed code
}



Look at the changes I made and add them to your code.

This post has been edited by eker676: 09 August 2009 - 08:27 PM

Was This Post Helpful? 1
  • +
  • -

#9 wildgoose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: Expected primary expression compiler error

Posted 09 August 2009 - 08:34 PM

In bool ReadDat(ifstream& rain, double rainfall[])

return true, count; <--- What is count doing here?
There's nothing wrong with your sort but it can be faster with a couple mods. Look for //@@@
void SortDat(double array[], const int SIZE)
{
	 bool swap;
	 double rain;
	 int nTop;

	 nTop = SIZE-1;
	do
	 {
		 swap = false;
		 for (int count = 0; count < nTop; count++)
//@@@		 for (int count = 0; count < (SIZE - 1); count++)
		 {
			 if (array[count] > array[count + 1])
			 {
				  rain = array[count];
				  array[count] = array[count + 1];
				  array[count + 1] = rain;
				  swap = true;
			 }
		 }
		 nTop--;  // count-1 is now in its final position
	 } while (swap);
}



Note that in the outer loop each indexed item becomes the next highest count when the inner loop completes as it is bubbled up and since we know its the highest, no need to check it anymore!
Was This Post Helpful? 0
  • +
  • -

#10 sphughes77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-July 09

Re: Expected primary expression compiler error

Posted 09 August 2009 - 08:41 PM

Thank you everyone. Now that the code compiles I can handle it from here. I appreciate all of the help.

View Postwildgoose, on 9 Aug, 2009 - 07:34 PM, said:

In bool ReadDat(ifstream& rain, double rainfall[])

return true, count; <--- What is count doing here?
There's nothing wrong with your sort but it can be faster with a couple mods. Look for //@@@
void SortDat(double array[], const int SIZE)
{
	 bool swap;
	 double rain;
	 int nTop;

	 nTop = SIZE-1;
	do
	 {
		 swap = false;
		 for (int count = 0; count < nTop; count++)
//@@@		 for (int count = 0; count < (SIZE - 1); count++)
		 {
			 if (array[count] > array[count + 1])
			 {
				  rain = array[count];
				  array[count] = array[count + 1];
				  array[count + 1] = rain;
				  swap = true;
			 }
		 }
		 nTop--;  // count-1 is now in its final position
	 } while (swap);
}



Note that in the outer loop each indexed item becomes the next highest count when the inner loop completes as it is bubbled up and since we know its the highest, no need to check it anymore!

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1