How to output that the stack is full?

i need to make the user input a number then ask if the user wants to input more into the stack and then it'll keep looping until it reaches the limit or when the user wants to stop then it'll output all the numbers that the user has inputted. The problem is that even though i set the loop limit at 10, it goes on til 11 or 12 and skips the else if function to output that the max input has been reached. i couldnt think of any other way to fix this so any advice is appreciated.

This is my code

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
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<conio.h>
#include<stack>
using namespace std;

int main ()
{
const int size =10;
    stack <int> s;
    int i;
    char yn;
    cout<<"enter a number :"; cin>>i;
    s.push(i);
    for (int x = 0; x<10; x++)//it goes over the expected loop
    {
        cout<<"do you want to add more? y/n "<<endl;
    cin>>yn;
    if (yn == 'y')
    {
        cout<<"enter another number :"; cin>>i;
        s.push(i++);
    }
    else if(i==size)//this doesnt ouput after limit is reached
    {
        cout<<"you reach the maximum input of stacks. Sorry!\n";
        goto out;
    }
    else
    {
        goto out;
    }


    }
out:
    system("cls");
    cout<<"output of values : \n";
    while (!s.empty())
    {
        cout<<s.top()<<endl;
        s.pop();
    }

}
is probably the else.
your program says:
if they typed in a 'y'
push more stuff onto the stack
else (if they did not type y)
now check the size and such.

I think removal of the word else will do the trick, then it would be
if they typed in a y
put stuff on stack
if its full, stop!

c++ programmers only use goto in very special circumstances. It would be best if you did not try to use it as a loop replacement; there are several good loops in c++ that do a better job than goto jumping.
also, <cstdio> is stdio.h prefer the first one as it makes proper use of namespaces.
finally, system() is also frowned upon, but for now, just be aware of this, its handy to use in small programs but trouble in professional software.
Last edited on
thank you so much!! it really was the else function lol
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
#include<iostream>
#include<stack>

int main()
{
   const int SIZE { 10 };
   std::stack<int> s;
   char more { 'y' };

   while (more != 'n')
   {
      if (s.size() < SIZE)
      {
         std::cout << "Enter a number: ";
         int input;
         std::cin >> input;

         s.push(input);

         std::cout << "Do you want to add more? (y/n): ";
         std::cin >> more;
      }
      else
      {
         std::cout << "You've reached the maximum input of stacks. Sorry!\n";
         break;
      }
   }

   std::cout << "\nOutput of values:\n";

   while (!s.empty())
   {
      std::cout << s.top() << ' ';
      s.pop();
   }
   std::cout << '\n';
}

Enter a number: 1
Do you want to add more? (y/n): y
Enter a number: 2
Do you want to add more? (y/n): y
Enter a number: 3
Do you want to add more? (y/n): y
Enter a number: 4
Do you want to add more? (y/n): y
Enter a number: 5
Do you want to add more? (y/n): n

Output of values:
5 4 3 2 1

Another run:
Enter a number: 10
Do you want to add more? (y/n): y
Enter a number: 9
Do you want to add more? (y/n): y
Enter a number: 8
Do you want to add more? (y/n): y
Enter a number: 7
Do you want to add more? (y/n): y
Enter a number: 6
Do you want to add more? (y/n): y
Enter a number: 5
Do you want to add more? (y/n): y
Enter a number: 4
Do you want to add more? (y/n): y
Enter a number: 3
Do you want to add more? (y/n): y
Enter a number: 2
Do you want to add more? (y/n): y
Enter a number: 1
Do you want to add more? (y/n): y
You've reached the maximum input of stacks. Sorry!

Output of values:
1 2 3 4 5 6 7 8 9 10

Accidentally entering a letter when the user should input a number can cause problems, the input stream goes into an error state:
Enter a number: 1
Do you want to add more? (y/n): y
Enter a number: 2
Do you want to add more? (y/n): y
Enter a number: y
Do you want to add more? (y/n): Enter a number: Do you want to add more? (y/n): Enter a number: Do you want to add more? (y/n): Enter a number: Do you want to add more? (y/n): Enter a number: Do you want to add more? (y/n): Enter a number: Do you want to add more? (y/n): Enter a number: Do you want to add more? (y/n): Enter a number: Do you want to add more? (y/n): You reach the maximum input of stacks. Sorry!

Output of values:
0 0 0 0 0 0 0 0 2 1

There are ways to prevent bad input from messing up program logic.
Topic archived. No new replies allowed.