Jump to content
  • Advertisement
Sign in to follow this  
E_Lane

Passing an array to a function w/ a pointer.

This topic is 4470 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, I'm trying to learn ANSI standard C Plus Plus, and I'm working through the tutorials on cplusplus.com, and I've come to a bit of a sticky point. Here is the code I have:
#include <iostream>
#include <string>
#include <sstream>

using namespace std;


struct employee{
	int emp_id;
	string employee_name;
	float salary;
	string position;
};

void printemployee(employee emp[], int i);

int main()
{
	int i,n;
	int *p;

	cout << "How many employees do you wish to enter? ";
	cin >> i;

	p = new (nothrow) employee;
	if (p==0)
		cout << "Error: Memory couldn't be allocated.";
	else
	{
		for (n=0; n<i; n++)
		{
			string mystr;
			
			cout << "Please enter the employee's name: ";
			getline(cin, p[n].employee_name);
			
			cout << "Please enter the employee's ID Number: ";
			getline(cin, mystr);
			stringstream(mystr) >> p[n].emp_id;
			
			cout << "Please enter the employee's salary: ";
			getline(cin, mystr);
			stringstream(mystr) >> p[n].salary;

			cout << "Please enter the employee's position: ";
			getline(cin, p[n].position);
		}
		printemployee(p, i);


	}

	return 0;
}

void printemployee(employee emp[], int i)
{
	int n;
	for (n=0; n<i; n++)
	{
		cout << "Name:     " << emp[n].employee_name << endl;
		cout << "ID:       " << emp[n].emp_id << endl;
		cout << "Salary:   " << emp[n].salary << endl;
		cout << "Position: " << emp[n].position << endl << endl;
	}

}


What I was trying to do is to combine what I've learned about structures, dynamic memory and functions to allow for a user generated array of the type "employee", and have the printemployee() function print out all the people entered thus far. Right now I'm getting eight errors, and logically speaking, I'm not sure what I'm doing wrong... Oh... And I forgot the mandatory question for the beginners forum: "Doodz-like I am totalllly l337 and I want to make a WoW killin' MMORPG. I dont no how too program, but that shoudnt be aproblem, right?" :P Thanks! Evan.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by E_Lane
Right now I'm getting eight errors, and logically speaking, I'm not sure what I'm doing wrong...


Are these compiler errors, linker errors or runtime errors? Please post the error messages you are getting.

Share this post


Link to post
Share on other sites
This is what was in the build output box:
They're compiling errors, and I'm using Visual C++ 2005 Express, if that matters.


1>c:\documents and settings\elane\my documents\visual studio 2005\projects\9-13-06-900\9-13-06-900\test.cpp(26) : error C2440: '=' : cannot convert from 'employee *' to 'int *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\elane\my documents\visual studio 2005\projects\9-13-06-900\9-13-06-900\test.cpp(36) : error C2228: left of '.employee_name' must have class/struct/union
1> type is 'int'
1>c:\documents and settings\elane\my documents\visual studio 2005\projects\9-13-06-900\9-13-06-900\test.cpp(36) : error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 2 provided
1> c:\program files\microsoft visual studio 8\vc\include\string(476) : see declaration of 'std::getline'
1>c:\documents and settings\elane\my documents\visual studio 2005\projects\9-13-06-900\9-13-06-900\test.cpp(40) : error C2228: left of '.emp_id' must have class/struct/union
1> type is 'int'
1>c:\documents and settings\elane\my documents\visual studio 2005\projects\9-13-06-900\9-13-06-900\test.cpp(44) : error C2228: left of '.salary' must have class/struct/union
1> type is 'int'
1>c:\documents and settings\elane\my documents\visual studio 2005\projects\9-13-06-900\9-13-06-900\test.cpp(47) : error C2228: left of '.position' must have class/struct/union
1> type is 'int'
1>c:\documents and settings\elane\my documents\visual studio 2005\projects\9-13-06-900\9-13-06-900\test.cpp(47) : error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 2 provided
1> c:\program files\microsoft visual studio 8\vc\include\string(476) : see declaration of 'std::getline'
1>c:\documents and settings\elane\my documents\visual studio 2005\projects\9-13-06-900\9-13-06-900\test.cpp(49) : error C2664: 'printemployee' : cannot convert parameter 1 from 'int' to 'employee []'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

Share this post


Link to post
Share on other sites
Gah! Of course! Thanks!
Now the only problem is that the codes seems to skip the first itiration of "Enter employee name: " and goes directly to Employee Number.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;


struct employee{
int emp_id;
string employee_name;
float salary;
string position;
};

void printemployee(employee emp[], int i);

int main()
{
int i,n;
employee *p;

cout << "How many employees do you wish to enter? ";
cin >> i;

p = new (nothrow) employee;
if (p==0)
cout << "Error: Memory couldn't be allocated.";
else
{
for (n=0; n<i; n++)
{
string mystr;

cout << "Please enter the employee's name: ";
getline(cin, p[n].employee_name);

cout << "Please enter the employee's ID Number: ";
getline(cin, mystr);
stringstream(mystr) >> p[n].emp_id;

cout << "Please enter the employee's salary: ";
getline(cin, mystr);
stringstream(mystr) >> p[n].salary;

cout << "Please enter the employee's position: ";
getline(cin, p[n].position);
}
printemployee(p, i);


}

return 0;
}

void printemployee(employee emp[], int i)
{
int n;
for (n=0; n<i; n++)
{
cout << "Name: " << emp[n].employee_name << endl;
cout << "ID: " << emp[n].emp_id << endl;
cout << "Salary: " << emp[n].salary << endl;
cout << "Position: " << emp[n].position << endl << endl;
}

}




Subsequent times, entering employee name works. Just not the first.

Share this post


Link to post
Share on other sites
Oh one other question:
By making a structure with more than one variable, and creating an array of that structure, does that make the array multidimensional?

Share this post


Link to post
Share on other sites
Quote:
Original post by E_Lane
Oh one other question:
By making a structure with more than one variable, and creating an array of that structure, does that make the array multidimensional?

No. The members of a structure do not constitute an array dimension.

Quote:
Now the only problem is that the codes seems to skip the first itiration of "Enter employee name: " and goes directly to Employee Number.

Are you, by any chance, using Microsoft Visual C++ 6.0? There was a bug in its STL implementation (provided by Dinkumware) regarding std::getline that generated that behavior. I recommend upgrading to Microsoft Visual C++ 2005 Express Edition, for multiple reasons.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Are you, by any chance, using Microsoft Visual C++ 6.0?

His output box had "Visual Studio 2005" in the path, so I assumed not, but that's good advice either way.

This behavior is also common if there's a newline character (\n) in the input stream by the time getline gets to it (as is common in loops). Since it reads up to a newline character, it will just take the first \n left in the stream.

You may want to look up the msdn article on cin.ignore().

Does the cout << "Please enter employee name"; get printed, at least?

Share this post


Link to post
Share on other sites
Quote:
Original post by templewulf
This behavior is also common if there's a newline character (\n) in the input stream by the time getline gets to it.


Which there will be, left over from answering "How many employees do you wish to enter? ", since the answer to that question is read by directly using cin, rather than using getline.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!