Program Assistance

I am working on a program for class and I'm having trouble with it. I am really bad at C++ so I don't really see what I did wrong. The programs opens but just sits there. I can noly guess that I left out something important. It's important that I understand what I did wrong so I am better prepared for our last project. Thanks for your help.

JD Potts

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
  // PottsProj4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

ofstream outfile;
ifstream infile;

void read_and_add(int a[], int b[], int c[]);
void compute_and_print(int a[], int b[], int c[]);


int _tmain(int argc, _TCHAR* argv[])
{int hits[20]={0}, walks[20]={0}, outs[20]={0};
outfile.open("d:\\BatStat.txt");
infile.open("d:\\baseball.txt");

read_and_add(hits, walks, outs);
compute_and_print(hits, walks, outs);

outfile.close();
infile.close();

return 0;
}

void read_and_add(int a[], int b[], int c[])
{int p1=0, h=0, w=0, o=0;
infile >> p1 >> h >> w >> o;
while(!infile.eof())
{int i=p1-1;
a[i]=a[i]+h;
b[i]=b[i]+w;
c[i]=c[i]+o;
}
return;
}

void compute_and_print(int a[], int b[], int c[])
{int bat1=0;
outfile << "Player   " << "Average   " << "Walks" << endl;
for(int i=0; i<20; i++)
{bat1=a[i]/(a[i]+b[i]);
outfile << i+1 << "     " << bat1 << "     " << c[i] << endl;
}
return;
}
Whenever dealing with files, you should make sure the file actually opens correctly. This can be done in several different ways.

Now your program in general is kind of hard to read, but that's mainly a style issue. Don't use global variables unless absolutely needed. Use some indention, and don't place code on the same line as a brace, or at least not right up against it. And use meaningful variable names. Arrays with names a, b, and c don't do much to tell what they are used for. Functions should be performing a single task each, if you have to use the word "and" to describe what they do, then your design is likely not awesome. But I wouldn't worry about that for this program. Just something for the future.

Check to make sure your files are actually opening, fix the formatting issues, and then we'll see where we're out.
Sorry, guess it was a hard read. The first function only performs one task. The first reads in information from a file to add it into the arrays. The second function computes the Batting average and then outputs it into a file, so I thinks thats one activity as well. It's suppose to show that we know how to pass an array into a function. Should the infile.open be in the main or is it being in the function acceptable?

Altered Code:
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
 // PottsProj4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

ofstream outfile;
ifstream infile;

void input(int a[], int b[], int c[]);
void output(int a[], int b[], int c[]);


int _tmain(int argc, _TCHAR* argv[])
{
int hits[20]={0}, walks[20]={0}, outs[20]={0};

	input(hits, walks, outs);
	output(hits, walks, outs);
	
return 0;
}

void input(int hits[], int outs[], int walks[])
{
int p1=0, h=0, w=0, o=0;

infile.open("d:\\baseball.txt");
infile >> p1 >> h >> w >> o;
	while(!infile.eof())
{
	int i=p1-1;
		hits[i]=hits[i]+h;
		outs[i]=outs[i]+w;
		walks[i]=walks[i]+o;
}
infile.close();
return;
}

void output(int hits[], int outs[], int walks[])
{
	int bat1=0;
		outfile.open("d:\\BatStat.txt");
		outfile << "Player   " << "Average   " << "Walks" << endl;
	
	for(int i=0; i<20; i++)
		{bat1=hits[i]/(hits[i]+outs[i]);
	
	outfile << i+1 << "     " << bat1 << "     " << walks[i] << endl;
}
outfile.close();
return;
}
Ok, figured out why the program was not running the whole list, I didn't have the "infile >> p1 >> h >> w >> o;" inside the While loop. So it ran the first and then sat there. Now it fully runs the list and correctly computes the "walks" for each player. BUT, now I find that it's not computing the Batting Average stat in Function 2 (output). It shows all zeros. Any suggestions?
No suggestions?
Topic archived. No new replies allowed.