Hi guys, quick question for you.

for (i = 0; i < NUM_EMPS; i++)
	{
		cout << "First and Last Name: ";
		cin.getline( names[i], 31 );

			for (index = 0; index < NUM_MONTHS; index++)
			{
				cout << "Hours worked in ";
				cout << months[index];
				cout << ": ";
				cin >> hours[i][index];
			}
	}

The first loop is supposed to prompt the user to enter a first and a last name. Then the second loop displays the name of a month, at which point the user inputs how may hours were worked that month. This is supposed to work for three different people. It works fine the first time through but when it comes time to enter the second name, it is skipped.

Sample Output:

First and Last Name: joe schmo
January: 1
Feb: 4
Mar: 6
Apr: 9
May: 12
June: 5

First and Last Name: January: <-------------
Feb:
Mar:
etc....


That arrow signifies where my issue is. Why is it skipping the input for name there? It doesn't pause at all?

Thanks for your time. I appreciate any help.

I had the same issues with one of my codes. I am no c++ expert so I cannot explain definite reasons but when I used getline() following the use of cin >> variable the getline was skipped, I found that the problem was fixed if I used all getline() functions in place of regular cin >> variable functions. I cannot explain why it works but I would guess it has something to do with the way the input is buffered. May sound like nonsense but thats what I would guess with the knowledge I have atm. Anyway, try replacing cin>>variable with getline() if not, I don't know what to tell you. Hope this helps. I used getline(cin, [string to store to]) then used sstream([string stored to]) >> variable in my codes to fix it. Hope that helps.

Thanks for the reply! I tried your suggestion and I'm getting a compiler error.

cannot convert parameter 1 from 'float [6]' to 'char *'

Any ideas?

Do you mind showing me the rest of the code? I may could help, I may not be able to, but I'm willing to try.

int main()
{
	const int NUM_EMPS = 3;
	const int NUM_MONTHS = 6;
	
	char months[6][9] = {"January", "February", "March", "April", "May", "June"};
	char names[NUM_EMPS][31];
	float hours[NUM_EMPS][NUM_MONTHS];
	int index, i;

	cout << "header";
	cout << "Enter the following information:\n\n";

	// INPUT
	for (i = 0; i < NUM_EMPS; i++)
	{
		cout << "First and Last Name: ";
		cin.getline( names[i], 31 );

			for (index = 0; index < NUM_MONTHS; index++)
			{
				cout << "Hours worked in ";
				cout << months[index];
				cout << ": ";
				cin.getline( hours[i], index );
			}
	}

I appreciate any advice.

After working with it for a while, I think I have found the answer to your problem. If you use string stream to buffer the input for the hours worked then store that to the provided variable I believe it should work, I think this accomplishes your purposes, if not let me know.

Here is what i got:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	const int NUM_EMPS = 3;
	const int NUM_MONTHS = 6;

	char months[6][9] = {"January", "February", "March", "April", "May", "June"};
	char names[NUM_EMPS][31];
	float hours[NUM_EMPS][NUM_MONTHS];
	int index, i;
	string input;

	cout << "header";
	cout << "Enter the following information:\n\n";

	// INPUT
	for (i = 0; i < NUM_EMPS; i++)
	{
		cout << "First and Last Name: ";
		cin.getline( names[i], 31 );

			for (index = 0; index < NUM_MONTHS; index++)
			{
				cout << "Hours worked in ";
				cout << months[index];
				cout << ": ";
				getline (cin, input);
				stringstream(input) >> hours[i][index];
			}
	}
}

After working with it for a while, I think I have found the answer to your problem. If you use string stream to buffer the input for the hours worked then store that to the provided variable I believe it should work, I think this accomplishes your purposes, if not let me know.

Here is what i got:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	const int NUM_EMPS = 3;
	const int NUM_MONTHS = 6;

	char months[6][9] = {"January", "February", "March", "April", "May", "June"};
	char names[NUM_EMPS][31];
	float hours[NUM_EMPS][NUM_MONTHS];
	int index, i;
	string input;

	cout << "header";
	cout << "Enter the following information:\n\n";

	// INPUT
	for (i = 0; i < NUM_EMPS; i++)
	{
		cout << "First and Last Name: ";
		cin.getline( names[i], 31 );

			for (index = 0; index < NUM_MONTHS; index++)
			{
				cout << "Hours worked in ";
				cout << months[index];
				cout << ": ";
				getline (cin, input);
				stringstream(input) >> hours[i][index];
			}
	}
}

Thank you so much! I really do appreciate it. You fixed it.

Only glad I could help. :)

Only glad I could help. :)

Thanks again. :*

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.