Hello elvizzz22,
What I have created for now will work. It is 10 numbers on one line.
Not sure what you are think of when you say "stack". It is more of a concept based off the "stack" the computer uses when the program runs and in this case "stack" is an area of memory to use.
The "stack" you are referring to is based on LIFO (Last In First Out) or you can think of it as you push something onto the top of the "stack" and retrieve what is on the top. To do what you want you could use an array, vector or some type of list. I am not sure if there is anything in the STL called "stack". The point is all in how you access what you create.
Once you have opened your file you will need to do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
std::size_t index{};
ifstream file;
file.open("example.txt");
if (!file.is_open())
{
std::cout << "\n File " << "example.txt" << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread".
exit(1); // <--- No reason to continue.
}
while (file >> value)
a[index++] = value;
// Or simply. Notice the ; at the end.
//while (file >> a[index++]);
file.close();
|
Not only is index used for the subscript of the array, but it also becomes a variable to keep track of how many elements of the array are used.
Once you fill the array I had to change the for sort to:
1 2 3 4 5 6 7 8 9 10 11 12
|
for (int i = 0; i < index; i++)
{
for (int j = 0; j < index - 1; j++)
{
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
|
Notice how I used "index" in the for conditions. This way it will not matter how many elements are used it will always work.
In the inner for loop I used "index - 1" because "a[j + 1]" will put you beyond the usable elements of the array, i.e., one past what you can use.
The next two for loops are good for printing out the array and both need fixed to work correctly, but both have nothing to do with a "stack".
Some of the program is worth keeping, but overall it needs to be reworked to be used more like a "stack". I would use a vector over the array. Technically backwards from what you would think of when you say "stack", but it can work the same way.
If this is homework post the requirements so I do not point you in the wrong direction.
I can fix what you have, but what you have is not what you want.
Hope that helps,
Andy
Edit:
Two arrays as in ascending[MAXSIZE] and descending[MAXSIZE] and I would define "MAXSIZE" as
constexpr std::size_t MAXSIZE{ 100 };
. I also said that based on the code I see before I had a better understanding of what you want.