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
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.