Hi there

Just started an IT course recently, and although flying with HTML and other disciplines, I'm having trouble with some C++ syntax.

I have a problem with a short piece of code I have to write, which has to read input from the keyboard for 5 fictional employees, each of which has an ID number, name and salary. I have to use an array of structures, but the gets() piece of the code does not seem to work.

Looking at the code below, is it just my syntax that is wrong? I'm using C++ in Visual Studio 2008.

Many thanks in anticipation of some much-needed help!

#include <iostream>
#include <cstring>

	using namespace std;

	struct employees{
		int id;	/* employee ID */
		char name[30]; /* employee name */
		double salary;	/* employee salary */
	}employee[5];


	int main(void)
	{	
		int counter;
		double salarytotal=0;

		for(counter=0;counter<5;counter++)
			{cout<<"Enter Employee ID number "<<counter+1<<": ";
			cin>>employee[counter].id;
			cout<<"Enter Salary for this Employee: ";
			cin>>employee[counter].salary;
			cout<<"Enter Name for this Employee (Max 30 chars): ";
			gets(employee[counter].name);
			salarytotal=salarytotal+employee[counter].salary;}
			

		cout<<"Combined Salary for these five employees is: "<<static_cast<char>(156)<<salarytotal<<endl;

		return 0;

	}

Recommended Answers

All 8 Replies

Why ruin a good thing you already got goin'?

//this
gets(employee[counter].name);
//should be this
cin >> employee[counter].name;

I am really not sure where you got "gets()" from.. I think it's a C function... and if I'm not mistaken it's use can be somewhat dangerous.

In my opinion, it is time to remove the gets() function from your c++ repertoire as I have never seen it's use in any code thus far in this forum... or in any other c++ code I have ever seen.

Why ruin a good thing you already got goin'?

//this
gets(employee[counter].name);
//should be this
cin >> employee[counter].name;

I am really not sure where you got "gets()" from.. I think it's a C function... and if I'm not mistaken it's use can be somewhat dangerous.

In my opinion, it is time to remove the gets() function from your c++ repertoire as I have never seen it's use in any code thus far in this forum... or in any other c++ code I have ever seen.

Got gets() from my tutor! It was part of a lesson on string handling.

As for your suggestion, and thank you for replying by the way, I did think of that and tried it, but as the purpose of the string is to input a persons's name, it only works when I don't enter spaces, i.e. "Fred" would work, but "Fred Smith" would just cause the loop to kick out to the end (if entered for employee 1, it would just jump to the end as if all 5 had been entered).

So...how would I enter a name string (including a space between name and surname) into employee.name in the above example?

You've brought up a good point; the >> extraction operator is 'white space' delimeted.

If you want to read the entire line, try using the getline() function from <string>

#include<string>

getline(cin, employee[counter].name);

You've brought up a good point; the >> extraction operator is 'white space' delimeted.

If you want to read the entire line, try using the getline() function from <string>

#include<string>

getline(cin, employee[counter].name);

Hmmm...

Thanks for the suggestion, but still doesnt work.

Here's the revised code (I tidied up the screen output), including your suggested change with getline...

#include <iostream>
#include <string>

	using namespace std;

	struct employees{
		int id;	/* employee ID */
		char name[30]; /* employee name */
		double salary;	/* employee salary */
	}employee[5];


	int main(void)
	{	
		int counter;
		double salarytotal=0;

		for(counter=0;counter<5;counter++)
		{
			cout<<"\nEnter Employee ID number "<<counter+1<<": ";
			cin>>employee[counter].id;
			cout<<"\nEnter Salary for this Employee: ";
			cin>>employee[counter].salary;
			cout<<"\nEnter Name for this Employee (Max 30 chars): ";
			getline(cin,employee[counter].name);
			salarytotal=salarytotal+employee[counter].salary;
			system("cls");
				
		}
			

		cout<<"Combined Salary for these five employees is: "<<static_cast<char>(156)<<salarytotal<<endl;

		return 0;

	}

Oh, my. Don't expect to learn C or C++ in the course you are taking.

User Input: Strings and Numbers [C++]
User Input: Strings and Numbers [C]

Thanks for the reply.

I clicked on the link for C++ you noted above and revised my code as follows:

#include <iostream>
#include <string>

	using namespace std;

	struct employees{
		int id;	/* employee ID */
		string name; /* employee name */
		double salary;	/* employee salary */
	}employee[5];


	int main(void)
	{	
		int counter;
		double salarytotal=0;

		for(counter=0;counter<5;counter++)
		{
			cout<<"\nEnter Employee ID number "<<counter+1<<": ";
			cin>>employee[counter].id;
			cout<<"\nEnter Salary for this Employee: ";
			cin>>employee[counter].salary;
			cout<<"\nEnter Name for this Employee (Max 30 chars): ";
			getline(cin, employee[counter].name);
			salarytotal=salarytotal+employee[counter].salary;
			system("cls");
				
		}
			

		cout<<"Combined Salary for these five employees is: "<<static_cast<char>(156)<<salarytotal<<endl;

		return 0;

	}

...however, it STILL doesnt work. This time, it builds without errors in VS2008 but then when running, just skips right past the input for name - doesnt even ask me for it!!!

Has anyone got an idea how I can do this? It's been a long evening trying to get this to work and now (12.19am) it's getting kind of irksome!! I'm sorry if I seem dumb to you experienced people out there, but C++ is new for me...

As per the c++ tutorial for accepting user input, try adding this line after line #24:

cin.ignore(100, '\n');

As per the c++ tutorial for accepting user input, try adding this line after line #24:

cin.ignore(100, '\n');

Brilliant - that appears to have worked!

Many thanks - much appreciated!

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.