Edit vector from a file help

So I'm creating a program that lets me read a list from a file (Filled with Numbers), allow the user to update (erase, add) and change the starting sequence of numbers, finds all the unique combinations of three numbers in a sequence of numbers that add to a requested sum, use data validation for user entries, calculate and display statistics, and write out the final results to a file.
List example: 33,31,14,37,30,11,17,27,22,35,1,19,42
Serach sum = 33 there are 3 unique combinations

Question: How do I let the user edit the file and update it then pass it to the other search sum function.

what I have so far.
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <fstream>
#include <iterator>
using namespace std;

//fucntion prototype
void fileinput(vector<int>&);
void fileoutput(vector<int>&, int, int);
void startingsequence(vector<int>&);
void searchsum(vector<int>&);
void countstats(vector<int>&, int&, int&);
void bargraph(vector<int>&, int, int);
void colgraph(vector<int>&, int, int);
bool isvalidInt(string str);

int main()
{
	int op;  // menu option
	vector<int> nums(0); 
	vector<int> count(0);
	//vector<int>
	cout << "Welcome to Find the 3 \n";
	/*
	create a menu
	*/
		cout << "1: File Input\n";
		cout << "2: Starting Sequence\n";
		cout << "3: Search Sum\n";
		cout << "4: Count Statistics\n";
		cout << "5: Bar Graph\n";
		cout << "6: Column Graph\n";
		cout << "7: File Output\n";
		cout << "8: Exit\n";

	do
	{

		cout << "Please enter an menu item.\n";
		cin >> op;
		cin.ignore();
		
		switch (op)
		{
		case 1:
			cout << "You entered file Input.\n";
			fileinput(nums);
			for (int i = 0; i < nums.size(); i++)
				cout << nums.at(i) << endl;
			break;
		case 2:
			cout << "You entered Starting Sequence.\n";
			break;
		case 3:
			cout << "You entered Start Sum.\n";
			searchsum(nums);
			break;
		
		case 8:
			cout << "You entered Exit.\n";
		default:
			cout << "INCORRECT ENTRY!\n";
		}


	} 
	while (op != 8); 
	cout << "Thanks for playing Find The 3.\n";
	system("pause");
} //end main

//1
void fileinput(vector<int>& n)
{
	// enter declarations
	ifstream inFile;
	string filename;
	int temp;
	string str;

	
	do
	{
	/*filename = "ouput1.txt";*/

	cout << " Please enter a filename\n";
	
	getline(cin, filename);
	filename = filename + ".txt";

	inFile.open(filename);

	if (inFile.fail())
	{
		cout << "\nThe file was not sucessfully opened."
			<< "\nPlease check that the file currently exists."
			<< endl;
	}
	else break;

	} while (1);

	getline(inFile, str);

	while (!inFile.eof())
	{
		if (isvalidInt(str))
		{
			temp = atoi(str.c_str());
			n.push_back(temp);
		}
		getline(inFile, str);
	}

	inFile.close();

	return;

}


bool isvalidInt(string str)
{
	bool valid = true;
	int i;
	char x;
	//more work
	if (str.size() == 0) return false;

	for (i = 0; i < str.size(); i++)
	{
		x = str.at(i);
		if (isdigit(x))
			valid = true;
		else return false;
	}
	return valid;
}

//2
void startingsequence(vector<int>& n)
{}
void searchsum(vector<int>& n)
{

	
	cin >> x;
	cin >> n;

	int a[n];

	cout <<
		for (i = 0; i<n; i++)
		{
			cin >> a[i];
		}
	for (i = 0; i < n - 2; i++)
	{
		for (j = i + 1; j < n - 1; j++)
		{
			for (k = i + 2; k < n; k++)
			{
				sum = a[i] + a[j] + a[k];
				if (sum == x)
				{
					cout << a[i] << " " << a[j] << "" << a[k] << "\n";
					q = q + 1;
				}
			}
		}
	}


}



Last edited on
lice1415 wrote:
So I'm creating a program that lets me ...


So you are. But what's the question?

(And please, please, please use code tags.)
My apologies. I want to know how to let the user edit vector in a loop until they are finished with editing then pass that info to search sum(find the unique combo) but I'm lost on how to to do that.
Last edited on
@lic14, all this stuff would be easier if contained in a class, and then each method would know about that class' private vector or stream variables.

As it is now, if you want more methods to know about something while keeping ownership in main() , then you'd need to add more parameters to those methods.
a) Give them ifstream or ofstream references, for example. An input file could be opened in main() (nothing read in just yet), and passed in to whatever needs it as ifstream& .

Or

b) give methods a (string) file name and allow them to fully control the opening/error handling, reading, and closing of the file.

Personally I'd have the input done only once (file or user input), but of course you can put in logic to re-read a file. If you're expecting lot of re-reads, then it may make sense to build something in the b) style.
Last edited on
Topic archived. No new replies allowed.