Printing Table (w/ strings) From User Entered Array ERROR

Hey everyone,

So I have been working on this little extra credit for my college class and have run into a beginner problem. I am able to compile and execute my code up to a specific point: when the PrintData function is called in displaying user data in this format:

Name Title Current_income New_income
Goofy Executive_Chief $xx,xxx.xx $xx,xxx.xx
Mickey Mouse VP_of_Entertainment $xx,xxx.xx $xx,xxx.xx
Donald Duck CFO $xx,xxx.xx $xx,xxx.xx
Cinderella VP of Customer Service $xx,xxx.xx $xx,xxx.xx

This is the output problem. An error occurs while printing the table and stops after the first line saying the string subscript is out of range. Ive been stuck on this for quite a while if anyone would be kind enough to help a noobie out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <string>
#include <iostream>
#include <iomanip>

using namespace std; 

void GetData(string, string, float[],float[]);
void CalcData(int, int, float[],float[],float[], float[]);
void PrintData(string,string,float[],float[]);

int main()
{

int const hours = 40;
int const weeks = 52;

string ename;
string etitle;

float ewage[4]; 
float inc[4]; 
float curr_year[4];
float new_year[4];
GetData(ename,etitle, ewage, inc);
CalcData(hours, weeks, ewage, inc,curr_year, new_year);
PrintData(ename, etitle, curr_year, new_year);

cout<<endl; system("PAUSE");
return 0;

}

/* Function Definitions */
void GetData(string fename, string fetitle, float fewage[],float finc[])
{
	for (int i = 0; i< 4; i++)
	{
			cout<<"Please enter Employee Name: "<<endl;
			getline(cin,fename);
			cout<<"Please enter Employee Title: "<<endl;
			getline(cin,fetitle);
			cout<<"Please enter Employee Hourly Wage: $"<<endl;
			cin>>fewage[i];
			cout<<"Please enter Employee Wage Increase: % "<<endl;
			cin>>finc[i];
			cin.ignore(80,'\n');
	}
}
void CalcData(int hours, int weeks, float fewage[],float finc[],float fcurr_year[], float fnew_year[])
{
	for (int i=0; i<4; i++)
	{
			int hours = 40;
			int weeks = 52;
			fcurr_year[i] = fewage[i] * hours * weeks;
			fnew_year[i] = fcurr_year[i] * finc[i];
	}
}
void PrintData(string fename, string fetitle, float fcurr_year[], float fnew_year[])
{		
	cout << left << setw(23)<<"Name";
		cout << right << setw(28)<<"Title";
		cout << right << setw(18)<<"Current Income";
		cout << right << setw(13)<<"New Income";
		cout << "\n" << endl;

	for (int i=0; i <4; i++)
	{
		cout << fixed;
		cout << setprecision(2);
		cout << left << setw(23) << fename[i];
		cout << right << setw(28) << fetitle[i];
		cout << right << setw(18) << fcurr_year[i];
		cout << right << setw(13) << fnew_year[i];
		cout << "\n" << endl;
	}
}



Please enter Employee Name:
Goofy
Please enter Employee Title:
Executive Chef
Please enter Employee Hourly Wage: $
15.85
Please enter Employee Wage Increase: %
2.8
Please enter Employee Name:
Mickey Mouse
Please enter Employee Title:
VP of Entertainment
Please enter Employee Hourly Wage: $
26.75
Please enter Employee Wage Increase: %
3.2
Please enter Employee Name:
Donald Duck
Please enter Employee Title:
CFO
Please enter Employee Hourly Wage: $
20.15
Please enter Employee Wage Increase: %
2.6
Please enter Employee Name:
Cinderella
Please enter Employee Title:
VP of Customer Service
Please enter Employee Hourly Wage: $
23.45
Please enter Employee Wage Increase: %
2.7
Name Title Current Income New Inco
me

32968.00 92310.
40

■ ■ 55640.00 178048.
00

= = 41912.00 108971.
20

48776.00 131695.
20


Press any key to continue . . .
Press any key to continue . . .


Debug Assertion Failed! Expression: string subscript out of range


Last edited on
Hi ! Code tags plz :)

Your problem is that the strings are declared as such:
1
2
string ename; //to contain 1 name
string etitle;  //to contain 1 title 

But at the end you're trying access an element from an array of strings
cout << left << setw(23) << fename[i]; //there's only room for 1 name, not i names!

Just a comment on the wage % increase. You type in 2.8% and the Goofy's wage goes up 280%. Also imo Goffy does a bad job and should probably get a wage reduction :p
Last edited on
;P

Thanks so much for your help! I will take this into consideration as I sit down to program tomorrow morning once I have a computer available to me again!(Currently on mobile) I'm so excited haha! I'll post once I fix all of that! Thanks again!
Hey,
If your still on could you please take a look at this thread? http://www.cplusplus.com/forum/beginner/79784/

same program last step!
code tags and all hehe :D

Topic archived. No new replies allowed.