Search Algorithm loading random stuff from memory...code to clear buffer maybe??

Hey ya'll...so i wrote this for my intro Programming with C++ class...Its supposed to display a menu with 6 options. create new file, display numbers, total, and average, display a sort, search for a num and tell yow how many occurances it had, append random numbers, and display largest.

the program probably has several issues, but I want help with why when I press menu option displaySortedNums I get all this random garbage, probably what is laying AROUND IN RAM, but how do I NOT have that in my displayed sort??? any help would be freaking FABuLOUS!! =D -Stacie

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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
 #include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <iomanip>

using namespace std;

//Function Prototypes
void menu();
string createFile();
void displayNumTotalAverage(string myFileName);
void displaySortedNums(string myFileName);
void searchNum(string myFileName);
void displayLargestNum(string myFileName);
void appendRandomNum(string myFileName);

int main()
{
	int choice = 0;
	string myFileName = "";

	do
	{
		//string myFileName = createFile();

		cout << "** MENU **" << endl << endl;
		cout << "Current Data File: ";
		cout << fixed << setprecision(2) << showpoint;
		menu();

		cin >> choice;

		while (choice < 1 || choice > 7)
		{
			cout << "Menu Choice: ";
			cin >> choice;
			cout << endl;
		}
		switch (choice)
		{
		case 1:
			myFileName = createFile();
			break;
		case 2:
			displayNumTotalAverage(myFileName);
			break;
		case 3:
			displaySortedNums(myFileName);
			break;
		case 4:
			searchNum(myFileName);
			break;
		case 5:
			displayLargestNum(myFileName);
			break;
		case 6:
			appendRandomNum(myFileName);
			break;
		}

	} while (choice != 7);

	system("PAUSE");
	return 0;
}
void menu()
{
	cout << "\n\n(1)  Select / create data file(.txt file extension will be added automatically)\n"
		<< "(2)  Display all numbers, total, and average\n(3)  Display all numbers sorted\n(4)  "
		<< "Search for a number and display how many times it occurs\n(5)  Display the largest number\n"
		<< "(6)  Append a random number(s)\n(7)  Exit the program\n\nMenu Choice:";
}
string createFile()
{

	string myFileName;
	ifstream inFile;

	cout << "\nName of data file: ";
	cin >> myFileName;
	myFileName = myFileName + ".txt";
	inFile.open(myFileName);
	if (inFile)
	{
		cout << myFileName;
	}
	else
	{
		cout << "\nFile not found, creating file.\n\n";
		ofstream outFile;
		outFile.open(myFileName + ".txt");
	}

	system("PAUSE");
	//cout << myFileName;
	return myFileName;
}

void displayNumTotalAverage(string myFileName)
{
	ifstream inFile;
	// Specific file path for where my documents are saved on my personal laptop. 
	//Modify before use on alternative machine. 
	//inFile.open("C:\\Users\\Stacie\\Documents\\Visual Studio 2013\\Projects\\Project33\\Project33" + myFileName + ".txt");
	//inFile.open(myFileName + ".txt");
	const int SIZE = 50;
	int num[SIZE];
	int count = 0;
	int total = 0;
	double average = 0;

	 inFile.open(myFileName + ".txt");
	 while (!inFile)
		 cout << "\nData File is empty" << endl << endl;

	while (count < SIZE && inFile >> num[count])
		count++;
	
	inFile.close();
	for (int index = 0; index < count; index++)
	{
		cout << num[index] << endl;
		total += num[index];
	}
	average = (float)total / count;
	cout << endl << "Total  :  " << total << endl << endl;
	cout << "Average: " << average << endl << endl;
	cout << "File Successfully Read" << endl << endl;


	system("PAUSE");
	return;
}


void displaySortedNums(string myFileName)
{
	ifstream inFile;
	inFile.open(myFileName + ".txt");
	const int SIZE = 50;
	int num[SIZE]; int counter = 0;
	while (counter < SIZE && inFile >> num[counter])			//Partially Filled Array
		counter++;
	inFile.close();


	int startScan, 
		minIndex, 
		minValue;

	for (startScan = 0; startScan < (SIZE - 1); startScan++)
	{
		minIndex = startScan;
		minValue = num[startScan];
		for (int index = startScan + 1; index < SIZE; index++)
		{
			if (num[index] < minValue)
			{
				minValue = num[index];
				minIndex = index;
			}
		}
		num[minIndex] = num[startScan];
		num[startScan] = minValue;
	}
	for (int i = 0; i < SIZE; i++)
	{
		cout << num[i] << endl;
	}
	cout << endl;
	system("PAUSE");
	return;
}

void searchNum(string myFileName)
{
	ifstream inFile;
	inFile.open(myFileName + "txt");
	const int SIZE = 50;
	int num[SIZE];
	bool found = false; 
	int position = -1;
	int index = 0;
	int userNum = 0;
	int counter = 0;
	int numCount = 0;
	cout << "Search Number: ";
	cin >> userNum;
	cout << endl << endl;
	while (index < SIZE && !found)
	{
		if (num[index] == userNum)
		{
			found = true;
			position = index;
			numCount++;
		}

		index++;
	}

	cout << userNum << " occurs " << numCount << " times ";
	cout << "File Successfully Read\n\n";
	system("PAUSE");
	return;
}

void displayLargestNum(string myFileName)
{
	ifstream inFile;
	inFile.open(myFileName + ".txt");
	const int SIZE = 50;
	int nums[SIZE];
	int count = 0;
	int highest;
	while (count < SIZE && inFile >> nums[count])
		count++;
	highest = nums[0];
	for (int i = 0; i < SIZE; i++)
	{
		if (nums[i] > highest)
			highest = nums[count];
	}
	cout << "\nLargest Number:  " << highest << endl << "File Successfully Read" << endl << endl;

}

void appendRandomNum(string myFileName)
{
	cout << "i am in the appendRandomNum function - option 6" << endl;
	int num = 0;
	int count = 0;
	ofstream outFile;
	outFile.open(myFileName + ".txt", ios::app);
	cout << "How many random numbers: ";
	cin >> count;
	for (int i = 0; i < count; i++)
		outFile << rand() % 10 << endl;
	outFile.close();
	cout << endl << "Number(s) Added" << endl << endl;

	system("PAUSE");
	return;
}

You are using (myFileName + ".txt") in all your functions, but in createFile() you are already doing this: myFileName = myFileName + ".txt"; so you end up with filename.txt.txt.

Your sort is not really sorting. It looks sort of like a bubble sort, but not really. The easiest way to achieve a bubble sort (tho not the most efficient) is:
1
2
3
4
5
6
7
8
for(int idx1 = 0; idx1 < counter; ++idx1)
  for(int idx2 = 1; idx2 < counter; ++idx2)
    if(num[idx1] > num[idx2])
    {
      int tmp = num[idx1]; // store 1st num
      num[idx1] = num[idx2];  // swap 2nd num to 1st
      num[idx2] =tmp; // swap tmp with 2nd num
    }


Your output function should also probably use for(int i = 0; i < counter; i++) because SIZE is not necessarily the number of items read and will output values outside the bounds of num[].
YAY THAT out of bounds might be where I am getting the crazy numbers from....thanks I will play with it again later after I get my kids from preschool. Thanks a million!
So I tried to incorporate the search above into my code and the results appear with the smallest num first, followed by the largest and then down from there. I need it to be in ascending order, smallest to greatest. How would I do that???

Also thanks for the line about the counter that got rid of all the GARBAGE!! YAY.

Thanks in advance ya'll... =D
*Stacie*
Yeah sorry, this should be better:
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
void displaySortedNums(string myFileName)
{
    ifstream inFile;
    inFile.open(myFileName + ".txt");
    if(inFile.good()) // check if file loaded ok
    {
        const int SIZE = 50; // we can load up to 50 int values from file
        int num[SIZE]; int counter = 0; // counter keeps track of actual array size after loading
        while (counter < SIZE && inFile >> num[counter])			//Partially Filled Array
          counter++;
        inFile.close();
        for(int idx1 = 0; idx1 < counter; ++idx1) // sort whole array till done
          for(int idx2 = idx1; idx2 < counter; ++idx2) // sort pass, larger numbers shifted right
            if(num[idx1] > num[idx2])
            {
               int tmp = num[idx1]; // store 1st num
               num[idx1] = num[idx2];  // swap 2nd num to 1st
               num[idx2] = tmp; // swap tmp with 2nd num
            }
        for (int i = 0; i < counter; i++) // display numbers
          cout << num[i] << endl;
        cout << endl;
    }
    system("PAUSE");
}
Last edited on
Thanks so much! You have really helped me out tremendously.
Topic archived. No new replies allowed.