• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

Errors C4552 + C2297 in Visual Basic C++ Program!

Melonhead

New Member
Joined
Apr 27, 2010
Messages
7 (0.00/day)
Hello, I am doing an assignment on programming and I have run into 21 errors. The program is not complete, but I just wanted to test it out to make the the output works the way I want it to. The object of the program is to read an inputfile, store info into 4 arrays that each hold a category of values, then write the info to an outputfile.. Employee Ids, hours, payRate, and wages are the arrays. I have searched online for these errors and I haven't found a solution, I do not know what I am doing wrong! Please help! here are the errors I am getting:

1>------ Build started: Project: Pro8, Configuration: Debug Win32 ------
1>Compiling...
1>Pro8.cpp
error C2146: syntax error : missing ';' before identifier 'ofstream'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2146: syntax error : missing ';' before identifier 'ifstream'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2228: left of '.open' must have class/struct/union
1> type is 'int'
error C2059: syntax error : ']'
error C2143: syntax error : missing ';' before '{'
warning C4552: '>>' : operator has no effect; expected operator with side-effect
error C2297: '>>' : illegal, right operand has type 'double'
warning C4552: '>>' : operator has no effect; expected operator with side-effect
error C2297: '>>' : illegal, right operand has type 'double'
warning C4552: '>>' : operator has no effect; expected operator with side-effect
error C2228: left of '.open' must have class/struct/union
1> type is 'int'
error C2297: '<<' : illegal, right operand has type 'const char [53]'
error C2563: mismatch in formal parameter list
error C2568: '<<' : unable to resolve function overload
1> d:\program files\microsoft visual studio 9.0\vc\include\ostream(974): could be 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=wchar_t,
1> _Traits=std::char_traits<wchar_t>
1> ]
1> d:\program files\microsoft visual studio 9.0\vc\include\ostream(966): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> d:\program files\microsoft visual studio 9.0\vc\include\ostream(940): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
warning C4552: '<<' : operator has no effect; expected operator with side-effect
error C2297: '<<' : illegal, right operand has type 'double'
warning C4552: '<<' : operator has no effect; expected operator with side-effect
error C2297: '<<' : illegal, right operand has type 'double'
warning C4552: '<<' : operator has no effect; expected operator with side-effect
error C2297: '<<' : illegal, right operand has type 'double'
error C2563: mismatch in formal parameter list
error C2568: '<<' : unable to resolve function overload
1> d:\program files\microsoft visual studio 9.0\vc\include\ostream(974): could be 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=wchar_t,
1> _Traits=std::char_traits<wchar_t>
1> ]
1> d:\program files\microsoft visual studio 9.0\vc\include\ostream(966): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> d:\program files\microsoft visual studio 9.0\vc\include\ostream(940): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
error C2297: '<<' : illegal, right operand has type 'const char [45]'
error C2563: mismatch in formal parameter list
error C2568: '<<' : unable to resolve function overload
1> d:\program files\microsoft visual studio 9.0\vc\include\ostream(974): could be 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=wchar_t,
1> _Traits=std::char_traits<wchar_t>
1> ]
1> d:\program files\microsoft visual studio 9.0\vc\include\ostream(966): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> d:\program files\microsoft visual studio 9.0\vc\include\ostream(940): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1>Proj8 - 21 error(s), 6 warning(s)

Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <ostream>
using namespace std;

outputFile ofstream;
inputFile ifstream;
const long int ARRAY_SIZE = 20;
long int empId[ARRAY_SIZE];
double hours[ARRAY_SIZE];
double payRate[ARRAY_SIZE];
double wages[ARRAY_SIZE];

int main()
{
	cout << setprecision(2) << fixed << showpoint;
	cout << "\tPAYROLL PROCESSING" << endl << endl;
	
	char fileName[50];
	cout << "Enter the name of the input file ";
	cin >> fileName;
	inputFile.open(fileName);
	while (empId[] != -1)
	{
		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			inputFile >> empId[count];
		}
		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			inputFile >> hours[count];
		}
		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			inputFile >> payRate[count];
		}
	}
	char fileName[50];
	cout << "\nEnter the name of the output file ";
	cin >> fileName;
	outputFile.open(fileName);
	char empnum[50];
	cout << "\nEnter an employee number for your search: ";
	cin >> empnum;
	
		outputFile << "Employee number\t\tHours Worked\tPayrate per Hour\tWages" << endl;
		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			wages[count] = hours[count] * payRate[count];
			outputFile << empId[count];
			outputFile << hours[count];
			outputFile << payRate[count];
			outputFile << wages[count] << endl;
		}
		
		outputFile << "Employees sorted on identification numbers: " << endl << endl;

}
 
Last edited:
I know I told erocker that I would never post on this forum again, but I just had to help, because I love helping when programming is involved, provided I know what I am doing :)

Basically, a majority of your errors were because you had two things typed in backwards, and the rest were simple human-logic errors. Nothing major, we all do these things :o

I changed colors of everything I changed, to make it easier for all to read. So, here it is:

Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <ostream>
using namespace std;

[COLOR="Red"]// You had these backwards. Whenever you use OOP, you call the name of
// the class first and then call the constructor[/COLOR]
[COLOR="Blue"]ofstream outputFile;
ifstream inputFile;[/COLOR]

const long int ARRAY_SIZE = 20;
long int empId[ARRAY_SIZE];
double hours[ARRAY_SIZE];
double payRate[ARRAY_SIZE];
double wages[ARRAY_SIZE];

int main()
{
	cout << setprecision(2) << fixed << showpoint;
	cout << "\tPAYROLL PROCESSING" << endl << endl;
	
	char fileName[50];
	cout << "Enter the name of the input file ";
	cin >> fileName;
	inputFile.open(fileName);
	
	[COLOR="Blue"]int neededForWhileLoopToWork;[/COLOR]
	
	[COLOR="Red"]// You can't say empId[] here, because you need something to be
	// referenced to, so using empId[checkThisPosition] would work.
	// However, try this way first, and if it does not work, post back
	// and we will see what we can do to fix it.[/COLOR]
	while ([COLOR="Blue"]neededForWhileLoopToWork[/COLOR] != -1)
	{
		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			[COLOR="Blue"]neededForWhileLoopToWork = count;[/COLOR]
			inputFile >> empId[count];
		}
		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			inputFile >> hours[count];
		}
		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			inputFile >> payRate[count];
		}
	}
	
	[COLOR="Red"]// It would just be better to name this a different variable name[/COLOR]
	char [COLOR="Blue"]outputFileName[/COLOR][50];

	cout << "\nEnter the name of the output file ";
	cin >> [COLOR="Blue"]outputFileName[/COLOR];
	outputFile.open([COLOR="Blue"]outputFileName[/COLOR]);
	char empnum[50];
	cout << "\nEnter an employee number for your search: ";
	cin >> empnum;
	
		outputFile << "Employee number\t\tHours Worked\tPayrate per Hour\tWages" << endl;
		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			wages[count] = hours[count] * payRate[count];
			outputFile << empId[count];
			outputFile << hours[count];
			outputFile << payRate[count];
			outputFile << wages[count] << endl;
		}
		
		outputFile << "Employees sorted on identification numbers: " << endl << endl;
	
		[COLOR="Red"]// You did not have a return 0 here, so I put one in[/COLOR]
		[COLOR="Blue"]return 0;[/COLOR]
}

Here is a picture of the program running as far as I could get it, because I do not know or have the sample files you are using:

Untitled.png


Post back if you require more assistance.
 
Hello! Thank you for replying. I have changed all of the above, and the output works. But as soon as I input the file name, a prompt appears telling me that the neededForWhileLoopToWork variable needs to be initialized. I set the variable to zero and it worked! But now another problem.. The program does not display the second string which asks for the output filename. Does this have something to do with the while loop and for loops?
 
Just hold on for a little bit, I have some work to do, and then I will come back and see what I can do.
 
Okay, if it is not prompting you to enter an output file name, then the program has not left the big while loop above it. Maybe, count is never being -1, so the loop just continues going on forever?

Come to think of it, and if I am doing this correctly in my head, count will never be -1. Your for loops are counting UP from zero, NOT down, so count will never be negative one.
 
Last edited:
Hmm, I have tried to use an if statement to return -1, if the condition's value was -1. Didn't seem to do anything. Output still worked the same.

Code:
		int neededForWhileLoopToWork = 0;

	while (neededForWhileLoopToWork != -1)
	{
		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			neededForWhileLoopToWork = count;
			inputFile >> empId[count];
		}
		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			inputFile >> hours[count];
		}
		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			inputFile >> payRate[count];
		}
		if (neededForWhileLoopToWork == -1)
			return -1;
	}
 
It would not matter, as count will never be negative one. Count is starting from zero and moving UP, so mathematically, count will never be negative one.

Oh yeah, and another thing, if I remember how while loops work, it will go through one last time after -1 is reached, ASSUMING that it will reach negative one. You will come up with an error because you will be trying to store something in the negative one index of the array, which is impossible, because arrays are zero based.
 
Last edited:
Okay, I removed the while loop and added in the binary search function + the selection sort function. I am coming up with 2 error(s) in the main body of the code.
Code:
const long int ARRAY_SIZE = 20;
int empId[ARRAY_SIZE];
int empinc;
double hours[ARRAY_SIZE];
double payRate[ARRAY_SIZE];
double wages[ARRAY_SIZE];
int sortId(long int[], int);
int binarysearch(long int [], int, int);
int result;


int main()
{
	cout << setprecision(2) << fixed << showpoint;
	cout << "\tPAYROLL PROCESSING" << endl << endl;
	
	char fileName[50];
	cout << "Enter the name of the input file ";
	cin >> fileName;
	inputFile.open(fileName);

		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			inputFile >> empId[count];
		}
		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			inputFile >> hours[count];
		}
		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			inputFile >> payRate[count];
		}

	char outputFileName[50];
	cout << "\nEnter the name of the output file ";
	cin >> outputFileName;
	outputFile.open(outputFileName);
	char empnum[50];
	cout << "\nEnter an employee number for your search: ";
	cin >> empnum;

	sortId(empId, ARRAY_SIZE);

	[COLOR="Red"]result = binarysearch(sortId, ARRAY_SIZE, empId);[/COLOR]
	
	if (result == -1)
		cout << "That number you entered is not valid.";
	else
	{
		cout << "The number you entered is valid.";
	}
		
		outputFile << "\tPAYROLL PROCESSING" << endl;
		outputFile << "Employee number\t\tHours Worked\tPayrate per Hour\tWages" << endl;

		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			wages[count] = hours[count] * payRate[count];
			outputFile << empId[count];
			outputFile << hours[count];
			outputFile << payRate[count];
			outputFile << wages[count] << endl;
		}
		
		outputFile << "Employees sorted on identification numbers: " << endl << endl;

		return 0;

}
/**********************************************************************************************
//Function definition of selection sort. Sorts the array empId.
**********************************************************************************************/
int sortId( int empId[], const long int ARRAY_SIZE)
{
	int sortfile, minIndex, minv;

	for(sortfile = 0; sortfile < (ARRAY_SIZE - 1); sortfile++)
	{
		minIndex = sortfile;
		minv = empId[sortfile];
		for(int index = sortfile + 1; index < ARRAY_SIZE; index++)
		{
			if(empId[index] < minv)
			{
				minv = empId[index];
				minIndex = index;
			}
		}
	}
	return 0;
}

/****************************************************************************************************
//Function definition of binary search.
*****************************************************************************************************/
int binarysearch( int sortId[], int ARRAY_SIZE, int value)
{
	int first = 0;
	int last = ARRAY_SIZE - 1;
	int middle;
	int position = -1;
	bool found = false;

	while(!found && first <= last)
	{
		middle = (first + last) / 2;
		if (sortId[middle] == value)
		{
			found = true;
			position = middle;
		}
		else if (empId[middle] > value)
			last =  middle - 1;
		else
			first = middle + 1;
	}
	return position;
}
Sorry, it is a bit long. The text in red is the line that has problems. The error reads " error C2664: 'binarysearch' : cannot convert parameter 1 from 'int (__cdecl *)(long [],int)' to 'long []'
1> There is no context in which this conversion is possible"
 
You're passing sortID (a function) into the binary search without paramters, which would be a pointer to the function.
 
Hey, I will be gone for a couple of hours, so I just wrote a program that does what you want.

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	int tempId, tempHours, tempWages;
	double tempPay;
	string inputFile, outputFile;
	vector<int> employeeId, hours, wages;
	vector<double> payRate; 
	fstream inputOutput;

	cout << "Enter In Input File And Put .txt At The End:  ";
	cin >> inputFile;

	inputOutput.open(inputFile, ios::in);

	if(inputOutput.fail())
	{
		cout << "This File Either Does Not Exist, Or Is Not In This Directory!" << endl;
		cout << "This Program Will Now END!";
		exit(0);
	}

	while(! inputOutput.eof())
	{
		inputOutput >> tempId >> tempHours >> tempPay >> tempWages;
		employeeId.push_back(tempId);
		hours.push_back(tempHours);
		payRate.push_back(tempPay);
		wages.push_back(tempWages);
	}

	inputOutput.close();
	
	inputOutput.clear();

	cout << "Enter In Output File And Put .txt At The End:  ";
	cin >> outputFile;

	inputOutput.open(outputFile, ios::out);

	if(inputOutput.fail())
	{
		cout << "This File Can Not Be Created Here!" << endl;
		cout << "This Program Will Now END!";
		exit(0);
	}

	for(int counter = 0; counter < employeeId.size(); counter++)
	{
		inputOutput << employeeId[counter] << " " << hours[counter] << " "
			        << payRate[counter] << " " << wages[counter] << endl;
	}

	inputOutput.close();
	
	return 0;
}

Here is the sample.txt file I used for the input:

123 10 8.62 123478
456 5 45.45 147560
789 67 34.67 214562
159 56 21.02 48952

Here is the output.txt file I used that took everything in the sample.txt file and stored it into this file:

123 10 8.62 123478
456 5 45.45 147560
789 67 34.67 214562
159 56 21.02 48952

Here is a picture of the program when it is done processing:

Untitled073.png
 
Thank you! I will use the program as a model, I'll post back if I have any other questions.
 
So I am having trouble again, and this time it has something to do with my functions.. here are the errors:
1>Pro8.obj : error LNK2019: unresolved external symbol "int __cdecl binarysearch(int * const,int * const,int)" (?binarysearch@@YAHQAH0H@Z) referenced in function _main
1>Pro8.obj : error LNK2019: unresolved external symbol "int __cdecl sortId(int * const,int)" (?sortId@@YAHQAHH@Z) referenced in function _main

I have looked into my functions and function prototypes, but do not know whats wrong. Maybe my function prototype is somehow wrong?
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <ostream>
using namespace std;

ofstream outputFile;
ifstream inputFile;

const long int ARRAY_SIZE = 20;
int empId[ARRAY_SIZE];
int empinc;
int sortedId[ARRAY_SIZE];
double hours[ARRAY_SIZE];
double payRate[ARRAY_SIZE];
double wages[ARRAY_SIZE];
int sortId(int[], int);
int binarysearch(int[], int[], int);
int result;


int main()
{
	cout << setprecision(2) << fixed << showpoint;
	cout << "\tPAYROLL PROCESSING" << endl << endl;
	
	char fileName[50];
	cout << "Enter the name of the input file ";
	cin >> fileName;
	inputFile.open(fileName);

		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			inputFile >> empId[count];
		}
		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			inputFile >> hours[count];
		}
		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			inputFile >> payRate[count];
		}

	char outputFileName[50];
	cout << "\nEnter the name of the output file ";
	cin >> outputFileName;
	outputFile.open(outputFileName);
	char empnum[50];
	cout << "\nEnter an employee number for your search: ";
	cin >> empnum;

	sortId(empId, ARRAY_SIZE);

	result = binarysearch(sortedId, empId, ARRAY_SIZE);
	
	if (result == -1)
		cout << "That number you entered is not valid.";
	else
	{
		cout << "The number you entered is valid.";
	}
		
		outputFile << "\tPAYROLL PROCESSING" << endl;
		outputFile << "Employee number\t\tHours Worked\tPayrate per Hour\tWages" << endl;

		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			wages[count] = hours[count] * payRate[count];
			outputFile << empId[count];
			outputFile << hours[count];
			outputFile << payRate[count];
			outputFile << wages[count] << endl;
		}
		
		outputFile << "Employees sorted on identification numbers: " << endl << endl;

		return 0;

}
/**********************************************************************************************
//Function definition of selection sort. Sorts the array empId.
**********************************************************************************************/
int sortId(int empId[], const long int ARRAY_SIZE)
{
	int sortfile, minIndex, minv;

	for(sortfile = 0; sortfile < (ARRAY_SIZE - 1); sortfile++)
	{
		minIndex = sortfile;
		minv = empId[sortfile];
		for(int index = sortfile + 1; index < ARRAY_SIZE; index++)
		{
			if(empId[index] < minv)
			{
				minv = empId[index];
				minIndex = index;
			}
		}
	}
	return 0;
}

/****************************************************************************************************
//Function definition of binary search.
*****************************************************************************************************/
int binarysearch(int sortedId[], int value, int ARRAY_SIZE )
{
	int first = 0;
	int last = ARRAY_SIZE - 1;
	int middle;
	int position = -1;
	bool found = false;

	while(!found && first <= last)
	{
		middle = (first + last) / 2;
		if (sortedId[middle] == value)
		{
			found = true;
			position = middle;
		}
		else if (sortedId[middle] > value)
			last =  middle - 1;
		else
			first = middle + 1;
	}
	return position;
}
 
Your function prototypes are a bit off. I changed the colors of everything I changed.

Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <ostream>
using namespace std;

ofstream outputFile;
ifstream inputFile;

const long int ARRAY_SIZE = 20;
int empId[ARRAY_SIZE];
int empinc;
int sortedId[ARRAY_SIZE];
double hours[ARRAY_SIZE];
double payRate[ARRAY_SIZE];
double wages[ARRAY_SIZE];

[COLOR="Red"]// Try it with const long in front of your last int[/COLOR]
int sortId(int[], [COLOR="Blue"]const long[/COLOR] int);

[COLOR="Red"]// You only have one array passing into this function[/COLOR]
int binarysearch(int[], [COLOR="Blue"]int[/COLOR], int);


int result;


int main()
{
	cout << setprecision(2) << fixed << showpoint;
	cout << "\tPAYROLL PROCESSING" << endl << endl;
	
	char fileName[50];
	cout << "Enter the name of the input file ";
	cin >> fileName;
	inputFile.open(fileName);

		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			inputFile >> empId[count];
		}
		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			inputFile >> hours[count];
		}
		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			inputFile >> payRate[count];
		}

	char outputFileName[50];
	cout << "\nEnter the name of the output file ";
	cin >> outputFileName;
	outputFile.open(outputFileName);
	char empnum[50];
	cout << "\nEnter an employee number for your search: ";
	cin >> empnum;

	sortId(empId, ARRAY_SIZE);

	result = binarysearch(sortedId, empId, ARRAY_SIZE);
	
	if (result == -1)
		cout << "That number you entered is not valid.";
	else
	{
		cout << "The number you entered is valid.";
	}
		
		outputFile << "\tPAYROLL PROCESSING" << endl;
		outputFile << "Employee number\t\tHours Worked\tPayrate per Hour\tWages" << endl;

		for(int count = 0; count < ARRAY_SIZE; count++)
		{
			wages[count] = hours[count] * payRate[count];
			outputFile << empId[count];
			outputFile << hours[count];
			outputFile << payRate[count];
			outputFile << wages[count] << endl;
		}
		
		outputFile << "Employees sorted on identification numbers: " << endl << endl;

		return 0;

}
/**********************************************************************************************
//Function definition of selection sort. Sorts the array empId.
**********************************************************************************************/
int sortId(int empId[], const long int ARRAY_SIZE)
{
	int sortfile, minIndex, minv;

	for(sortfile = 0; sortfile < (ARRAY_SIZE - 1); sortfile++)
	{
		minIndex = sortfile;
		minv = empId[sortfile];
		for(int index = sortfile + 1; index < ARRAY_SIZE; index++)
		{
			if(empId[index] < minv)
			{
				minv = empId[index];
				minIndex = index;
			}
		}
	}
	return 0;
}

/****************************************************************************************************
//Function definition of binary search.
*****************************************************************************************************/
int binarysearch(int sortedId[], int value, int ARRAY_SIZE )
{
	int first = 0;
	int last = ARRAY_SIZE - 1;
	int middle;
	int position = -1;
	bool found = false;

	while(!found && first <= last)
	{
		middle = (first + last) / 2;
		if (sortedId[middle] == value)
		{
			found = true;
			position = middle;
		}
		else if (sortedId[middle] > value)
			last =  middle - 1;
		else
			first = middle + 1;
	}
	return position;
}
 
Back
Top