i understand, but now that I'm sort of close, I want to get this right, not to second guess you or anything. your points are valid. ;)
Below is my revised code, I have two problems I need answered:
1.) While running, the program says that it has to close down due to an unexpected warning. I don't know if this is a possibility or not, but am I accessing memory I shouldn't be?! What is the problem spot in my code that is doing this?
2.) When I uncomment the last line (line 55) in the
vector <int> numbers::sort_up |
function, I get an infinite recursion problem, yet I do not know why either. I've tried mapping it out and being my own debugger, but I don't see it. An extra set of eyes would be very helpful.
Thank you for all the help!!! I know most of you don't want to see all of my code, but it could help you to find where the problem is. Thanks again! Much appreciated!
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
|
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class numbers
{
public:
void get(); // Stores input file values to vector <int> number
void add(int n); // Adds a new number to the end of the vector
int remove(int i); // Removes the element at the specified index of the vector
void sort_up(); // Calls the private recursive sort_up function
void sort_down(); // Calls the private recursive sort_down function
void display(); // Prints the vector to the screen
private:
vector <int> numbers;
vector <int> sort_up(vector <int> unsorted_list);
vector <int> sort_down(vector <int> unsorted_list);
};
void numbers::add(int n)
{
if (n > 0)
numbers.push_back(n);
else
{
cout << "Invalid value, must be greater than zero";
exit(1);
}
}
int numbers::remove(int index)
{
if ((index >= 0) && ((index + 1) <= numbers.size()))
numbers.erase(numbers.begin() + index);
else
{
cout << "Invalid index";
//exit(1);
}
}
void numbers::sort_up()
{
numbers = sort_up(numbers);
}
void numbers::sort_down()
{
numbers = sort_down(numbers);
}
vector <int> numbers::sort_up(vector <int> unsorted_list)
{
int i = 1;
if(unsorted_list.size() == 1)
{
cout << unsorted_list.at(0);
exit(1);
}
else
{
int temp;
for (int i = unsorted_list.size() - 1; i >= 0; i--) // Reading the dataset starts backwards.
{
for (int j = 0; j < unsorted_list.size() - 1; j++)
{
if (unsorted_list[j] > unsorted_list[j+1]) //Switching values to maximize convenience.
{
temp = unsorted_list[j];
unsorted_list[j] = unsorted_list[j+1];
unsorted_list[j+1] = temp;
}
}
//cout << unsorted_list[i] << " ";
}
numbers::remove(unsorted_list[unsorted_list.size() - 1]);
//numbers::sort_up(unsorted_list);
}
}
vector <int> numbers::sort_down(vector <int> unsorted_list)
{
if(unsorted_list.size() == 1)
cout << unsorted_list.at(0);
else
{
}
}
void numbers::get()
{
int next;
ifstream in_file;
in_file.open("numbers.dat");
if (in_file.fail())
{
cout << "Failed to open file." << endl;
exit(1);
}
while (in_file >> next)
{
numbers.push_back(next);
next++;
}
in_file.close();
}
void numbers::display()
{
for (int j = 0; j < numbers.size(); j++)
cout << numbers[j] << " ";
cout << endl;
}
int main()
{
int number, input, next;
char answer;
numbers our_numbers;
our_numbers.get();
do {
cout << endl << "1. Add a number to the end of the vector" << endl << "2. Remove a value at a specified index in the vector" << endl << "3. Sort the vector from lowest to highest" << endl << "4. Sort the vector from highest to lowest" << endl << "Please select the option that you would like to complete: ";
cin >> number;
if (number == 1)
{
cout << endl << "Please enter a number to add to the end of the vector: ";
cin >> input;
our_numbers.add(input);
cout << "Unsorted vector: ";
our_numbers.display();
}
else
if (number == 2)
{
cout << endl << "Please enter an index of the vector to remove: ";
cin >> input;
our_numbers.remove(input);
cout << endl << "Unsorted vector: ";
our_numbers.display();
}
else
if (number == 3)
{
cout << endl << "Unsorted vector: ";
our_numbers.display();
cout << "Sorted vector: ";
our_numbers.sort_up();
}
else
if (number == 4)
{
cout << endl << "Unsorted vector: ";
our_numbers.display();
cout << "Sorted vector: ";
our_numbers.sort_down();
}
else
{
cout << endl << "Invalid value. Program exiting...";
exit(1);
}
cout << endl << "Would you like to test another operation? <y/n> : ";
cin >> answer;
} while (answer == 'Y' || answer == 'y');
return 0;
}
| |