functions using a structure - homework help

Hi everyone i am new to the forums and was having a hard time with a homework assignment. I can't seem to understand how to use functions properly and figure out how to format what goes in "()" when calling them.

For this assignment we are supposed to create a structure to keep track of character statistics in an input file. I called this the "Counts" structure. Then we are supposed to prompt the user for an input file name. In this case it is a text file. Then another function is needed to determine what a character is and increase the appropriate counter. The characters are a combination of X's, Y's, Z's and some other characters that are referred to as "illegal". This is where we pass the Counts structure to the function. Then lastly we need a function to print the final report using a Counts structure passed to it.

What I am having trouble with is the "increase" function and its parameters. I'm sure there are other problems but for now this is what I am trying to deal with. Can anyone help me with this?

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

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


	struct Counts
	{
		int numXs; 
		int numYs; 
		int numZs; 
		int numIllegal;

		Counts()
		{
		numXs=0;
		numYs=0;
		numZs=0;
		numIllegal=0;
		}
	};

void openFile(ifstream &fin);
void increase(x);
void Print(Counts);


int main()
{
	
	Counts x;
    	char ch;
	string File_name;   
	ifstream fin;				
	
	int numXs = 0,  numYs = 0,  numZs = 0, numIllegal = 0; 
	
openFile(fin);
increase(Counts x);
//Print(struct x);

}

void openFile(ifstream &fin){

cout << "Enter input file name : ";	
	string File_name;
	
	cin >> File_name;					
	fin.open(File_name.c_str()) ;		
	if ( fin.fail())		
		cout <<"\nBad input file name, program terminated.\nHave a nice day.\n\n\n";
	
}
void increase(Counts x, char ch){

ifstream fin;				
	
	int numXs = 0,  numYs = 0,  numZs = 0, numIllegal = 0; 
	fin >> ch;
		switch(ch)  // Check to see which character was entered.
			{
				case 'X':
				case 'x' :		// Count Xs
					numXs++;
					break;
					
				case 'Y':
				case 'y' :		//Count Ys
					numYs++;
					break;
					
				case 'Z':
				case 'z' : 
					numZs++;	// Count Zs
					break;
				default:
					numIllegal++;
					break;	
        	}


void Print(Counts){

int numXs, numYs, numZs, numIllegal;
		cout << left ;
		cout << "===================================================" << endl;
		cout << setw(25)<<"The number of Xs was " << setw(10)<< numXs << endl;
		cout << setw(25)<<"The number of Ys was " << setw(10)<< numYs << endl;
		cout << setw(25) <<"The number of Zs was " << setw(10)<< numZs << endl;
		cout << setw(25) <<"The number of illegal characters was "<< setw(10) << numIllegal << endl;
		cout << "---------------------------------------------------" << endl;
		cout << "		PROGRAM TERMINATED after reaching the EOF." << endl;
	

	
}
The first thing I notice here, is that your prototype and function definition do match in the parameters list. (i.e. you have (counts x, char ch) in the definition, but only an (x) with no data type in the prototype.)

What I mean here is that your prototype should always match your function definition in terms of the data type of your parameters.
For instance if I had this function:
1
2
3
4
 void blah(int x, char y){
           cout << "X: " << x << endl;
           cout << "Y: " << y << endl;
}

Then my prototype would need to look like this:
void blah(int, char);
Last edited on
@Baelix, thanks. So in this case would the data type be int or struct? The only data type in the structure is int.
At the moment, your increase function doesn't seem to be making use of anything in the Counts structure you're passing to it. When you write, for example:

 
numXs++;


you are incrementing the local variable

int numXs = 0;


that you declared at the start of your function.

Did you actually want to increment the member of your structure? If so, you access a structure member by putting the name of the structure object, a dot, and then the name of the member, like so:

 
x.numXs++;

Last edited on
Topic archived. No new replies allowed.