Need help with array program for C++ class!

Oct 9, 2012 at 8:12pm
Deleted for privacy.
Last edited on Oct 9, 2012 at 11:26pm
Oct 9, 2012 at 9:09pm
Give us the code that actually causes these build errors. I have no idea what "putting size in the for loop results in the loop not executing" means.
Oct 9, 2012 at 10:16pm
Deleted for privacy.
Last edited on Oct 9, 2012 at 11:27pm
Oct 9, 2012 at 10:40pm
1
2
3
if (col == 0){
  cout << name[col] << " ";
} 


You output name[0] each time.
Last edited on Oct 9, 2012 at 10:40pm
Oct 9, 2012 at 10:46pm
So how would I change that? Because I want to scan for the first column of each row, yet output a different string for each row.
Oct 9, 2012 at 11:16pm
You're doing the same thing with the input.

When the col is zero, put the string into/read the name from name[row]
Oct 9, 2012 at 11:26pm
Ok thanks!
Oct 9, 2012 at 11:38pm
Restated for reference.

Ok, so I'm working on a lab for my C++ class and am frustrated that I'm getting a couple build errors. I listed those errors at the top of the code.

Anyway, the goal is to input company names and quarterly sales from a file (data.txt into a string (for the names) and double (for the data) array. Then the purpose is to allow the user to search for a company and display the sales. I haven't even gotten to the latter yet, just trying to focus on fixing these errors.

Here's the content of my data file:
Company1 40000 50000 60000 70000
Company2 50000 40000 45000 43000
Company3 60000 90000 60000 45000
Company4 64000 55000 70000 80000
Company5 80000 30000 24000 70000



And here's my code. What am I doing wrong?:

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/******************
Errors:
putting size in the for loop results in the loop not executing
enabling findCompanyIndex returns a compiler error
showData only shows company5 for the name (data is correct)

******************/

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

//function prototypes
void getData(string [], double [][4], int);
void showData(string [], double [][4], int size);
int findCompanyIndex(string, string[], int);


int main(){

//initialize variables
string name[4], userName;
double data[4][4];
int counter = 0;
int size = 5;

//gets and shows data
getData(name, data, counter);
showData(name, data, counter);

cout << "Enter the name of a company: ";
cin >> userName;

//findCompanyIndex(userName, name, size);

//system stuff
system("pause");
return 0;
}

void getData(string name[], double data[][4], int names){

ifstream inputFile;

//opening file
inputFile.open("Data.txt");

for (int row = 0; row < 5; row++){
	for (int col = 0; col < 5; col++){
	if (col == 0){
	inputFile >> name[col];

	//debugging
        cout << setw(4) << name[col] << " ";
	} 
	else{
	inputFile >> data[col][row];

        //debugging
	cout << setw(4) << data[col][row] << " ";
		}
	}
	//new line before each iteration, creating a clean look
	cout << endl;
}
cout << endl;

inputFile.close();

}
void showData(string name[], double data [][4], int size){
for (int row = 0; row < size; row++){
	for (int col = 0; col < size; col++){
	if (col == 0){
	cout << name[col] << " ";
	} 
	else{
	cout << data[col][row] << " ";
		}
	}
	cout << endl;
}}

int findCompanyIndex(string userName, string name, int names2){
	int total = 5;

	for (int col = 0; col < 5; col ++){

	if (userName == name){
	name[col];
	cout << "Yes!";
	} 
	else{}
	
	
	}
	return total;
}

Topic archived. No new replies allowed.