Functions u

I am trying to get the number of times the function validegrades is used to be printed.
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;

// prototypes for functions
float average(float, float, float);
float lettergrade(float);
float validgrades(float, float, float);

// OUTFILE CALL
ofstream outfile("hwfour.out");
int i;

int main()

{
	float num1, num2, num3, avg;
	int p, i, v;
	char grade;

	avg = 1;
	p = 0;
	i = 0;
	v = 0;
	ifstream infile("hw4.dat");




	{
		outfile << setiosflags(ios::left) << setw(10) << "GRADE 1" << setiosflags(ios::left) << setw(10)
			<< "GRADE 2" << setiosflags(ios::left) << setw(10)
			<< "GRADE 3" << setiosflags(ios::left) << setw(10) << "AVERAGE" << setiosflags(ios::left) << setw(10) << "GRADE"
			<< setiosflags(ios::left) << setw(10) << "Groups" << setiosflags(ios::left) << setw(10) << "Invlid "
			<< setiosflags(ios::left) << setw(10) << "Valid ";
	}


	while (avg != 101)

	{
		ifstream input;
		input.open("hw4.dat");
		for (p = 0; p < 15; p++)
		{
			{ cout << " Please enter your 3 grades seperated by spaces " << endl;
			infile >> num1;
			infile >> num2;
			infile >> num3;
			}


			
			validgrades(num1, num2, num3);// function call 


			avg = average(num1, num2, num3);// function call
			std::cout << "Your Average is:" << endl << avg << endl;

			grade = lettergrade(avg); //function call
			std::cout << "Your Grade is:" << endl << grade << endl;

		}
		std::cout << "The Number of groups Processed is" << endl << p << endl;
		std::cout << "Invalid Groups" << endl << i << endl;
		std::cout << "Valid Groups" << endl << v << endl;

		outfile << setiosflags(ios::fixed) << setprecision(2) << setiosflags(ios::left) << setw(10) << endl
			<< num1 << setiosflags(ios::left) << setw(10) << num2 << setiosflags(ios::left) << setw(10)
			<< num3 << setiosflags(ios::left) << setw(10) << avg << setiosflags(ios::left) << setw(10) << grade
			<< setiosflags(ios::left) << setw(10) << p << setiosflags(ios::left) << setw(10) << i << setiosflags(ios::left) << setw(10) << v;

	}

	{

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

		return 0;
	}
}

// this function calculates the average of 3 numbers

float average(float a, float b, float c) //a,b and c are justpalce holders

{
	return(a + b + c) / 3;
}

// this function validates the grades inputed

float validgrades(float a, float b, float c)


{
	{
		if (a > 100)
		std::cout << "Your number is invalide, it is too big";

		return 1;

		if (a < 0)
			std::cout << "Your number is invalid, it is too small";
		return 1;

		if (b > 100)
			std::cout << "Your number is invalide, it is too big";

		return 1;

		if (b < 0)
			std::cout << "Your number is invalid, it is too small";
		return 1;

		if (c > 100)
			std::cout << "Your number is invalide, it is too big";

		return 1;

		if (c < 0)
			std::cout << "Your number is invalid, it is too small";
		return 1;


	}
}

// this function assigns letter grades to the average given

float lettergrade(float a)
{
	if (a >= 90.00)
		return 'A';
	else
	if (a >= 80.00)
		return 'B';

	else
	if (a >= 70.00)
		return 'C';
	else
	if (a >= 60.00)
		return 'D';

	else
		return 'F';
}
If you want to know how many time that function "validgrades" has been called/run then the way i would do that is add a variable either as a global variable and add 1 to it every time that function was run and print it out at the end.

Another way that you could do that is by passing the variable that would be for count the number of times that "validgrades" was run and print it out at the end.

Yet another way if you don't want any passing of variables or making a global variables in the program you can just have a local variable in that function and print it out at the end of the function so that you can see the number of times the that that function was run.

I am sure that there are more way to do what you want, but those are the thing that i can think of right now.

- Hirokachi
Topic archived. No new replies allowed.