Using Classes

I am not exactly sure as to why the program is not taking in the input I give the program and put any numbers that were given into an array.

What I have to do:
Have a while statement that will abort after 10 inputs
Accumulate all numbers entered
display total of all numbers
display average
display titles for user to understand what is going on onscreen
anything that is displayed or typed in must be placed in the txt file

code for improper inputs

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
152
153
154
  #include <stdio.h>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <ctime>
#include <string>
#include <stdlib.h>
#include <ctype.h>
#include <sstream>
#pragma warning (disable:4996)

using namespace std;


std::ofstream ofs("prog12.txt", std::ofstream::out);

class programming
{
private:

	int variable;
	int temp;
	int bypass;
	int data_array[10];
	int count;
	int num_counter = 0;
	int total = 0;

public:
	int program_counter = 0;
	int program_counterSHOWN = 0;

	void input_value()
	{
		cout << "Please enter a number: ";
		ofs << "Please enter a number (To end input type 'N') : ";


		while (!(cin >> variable))
		{
			cout << "Must be a number : ";
			ofs << "Must be a number : ";
			ofs << endl;
			cin.clear();
			cin.ignore(100, '\n');
		}

		ofs << endl;
		cout << "You inputed: " << variable;
		ofs << "You inputed: " << variable;
		///////////////////////////////////////////////////////////
		//COUNTING HOW MANY TIMES THE USER HAS INPUTED A NUMBER///
		/////////////////////////////////////////////////////////
		program_counterSHOWN++;
		data_array[num_counter] = variable;
		num_counter++;
		ofs << endl;
		ofs << endl;
		//////////////////////////////////////////////////////
	}

	/////////////////////////////////////////////
	//SHOWING HOW MANY NUMBERS ARE IN THE DATA//
	///////////////////////////////////////////
	void output_value()
	{
		ofs << "You have " << program_counterSHOWN << " number(s) in your data, your maximum is 10.";
		ofs << endl;
		ofs << endl;
		ofs << "--------------------------------------------------------------------------------------";
		ofs << endl;
		ofs << endl;
	}


	/////////////////////////////////////////
	//SORTING ALL THE NUMBERS IN THE ARRAY//
	///////////////////////////////////////
	void sorting_array()
	{
		for (int count2 = 0; count2 < num_counter; count2++)
		{
			for (int count = 0; count < 5 - count2 - 1; count++)
			{
				if (data_array[count]>data_array[count + 1])
				{
					temp = data_array[count];
					data_array[count] = data_array[count + 1];
					data_array[count + 1] = temp;
				}
			}
		}
		ofs << endl;
		ofs << "Here is the numbers in order : ";
		for (int count4 = 0; count4 < num_counter; count4++)
		{
			ofs << endl;
			ofs << data_array[count4];
		}
	}

	void average()
	{
		for (int count3 = 0; count3 < num_counter; count3++)
		{
			total = data_array[count3] + total;
		}
		total = (total / num_counter);
		ofs << "The average number of the data is : " << total;
		ofs << endl;
		ofs << endl;
	}


};


void today()
{
	time_t t = time(0); //get time now
	struct tm * now = localtime(&t);
	ofs << (now->tm_mon + 1) << '/' << now->tm_mday << '/' << (now->tm_year + 1900) << " ";
	ofs << ", Cheers.";
}

int main()
{
	programming object;
	char retry = 'y';



	while (object.program_counter < 10 && retry == 'Y' || retry == 'y')
	{
		object.input_value();
		ofs << endl;
		object.output_value();
		ofs << endl;
		cout << endl;
		cout << "Want to add more? (Type 'Y' for yes, any other input will be no): ";
		cin >> retry;
	}

	object.sorting_array();
	ofs << endl;
	object.average();

	today();
	ofs.close();

	return 0;
}
Last edited on
Here is the focus I guess, also the program keeps running even though I passed 10 inputs
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

using namespace std;


std::ofstream ofs("prog12.txt", std::ofstream::out);

class programming
{
private:

	int variable;
	int temp;
	int bypass;
	int data_array[10];
	int count;
	int num_counter = 0;
	int total = 0;

public:
	int program_counter = 0;
	int program_counterSHOWN = 0;

	void input_value()
	{
		ofs << "Please enter a number : ";


		while (!(cin >> variable))
		{
			ofs << "Must be a number : ";
			ofs << endl;
			cin.clear();
			cin.ignore(100, '\n');
		}

		ofs << endl;
		ofs << "You inputed: " << variable;
		///////////////////////////////////////////////////////////
		//COUNTING HOW MANY TIMES THE USER HAS INPUTED A NUMBER///
		/////////////////////////////////////////////////////////
		program_counterSHOWN++;
		data_array[num_counter] = variable;
		num_counter++;
		ofs << endl;
		ofs << endl;
		//////////////////////////////////////////////////////
	}

	/////////////////////////////////////////////
	//SHOWING HOW MANY NUMBERS ARE IN THE DATA//
	///////////////////////////////////////////
	void output_value()
	{
		ofs << "You have " << program_counterSHOWN << " number(s) in your data, your maximum is 10.";
		ofs << endl;
		ofs << endl;
		ofs << "--------------------------------------------------------------------------------------";
		ofs << endl;
		ofs << endl;
	}

	void average()
	{
		for (int count3 = 0; count3 < num_counter; count3++)
		{
			total = data_array[count3] + total;
		}
		total = (total / num_counter);
		ofs << "The average number of the data is : " << total;
		ofs << endl;
		ofs << endl;
	}


};


int main()
{
	programming object;
	char retry = 'y';



	while (object.program_counter < 10 && retry == 'Y' || retry == 'y')
	{
		object.input_value();
		ofs << endl;
		object.output_value();
		ofs << endl;
		cout << endl;
		cout << "Want to add more? (Type 'Y' for yes, any other input will be no): ";
		cin >> retry;
	}

	object.sorting_array();
	ofs << endl;
	object.average();

}
Last edited on
Topic archived. No new replies allowed.