Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7505525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:56:34+00:00 2026-05-29T21:56:34+00:00

Greetings Stack Overflow community. I’m trying to make a function that can pop all

  • 0

Greetings Stack Overflow community. I’m trying to make a function that can pop all the values in a stack, add them and then push them back. I’ve so far been successful in adding, subtracting, dividing and multiplying two values by just popping them twice to local variables, performing the operation and then pushing back the completed value. But to add all I would require to pop from the stack until its empty.

I’ve tried adding a loop to popping two values and pushing the result back. This is what I have in mind

push these numbers 1 , 2 , 3, 4 ,5 
pop 5
pop 4
add 5 + 4 = 9
push 9
stack (1,2,3,4,9)
pop 9
pop 4
add 9+4 = 13
push 13

and so on. I’ve tried using the isEmpty function for the loop and trying to make it that it stops when one of the values is not NULL. But I have no idea how to stop or start the loop so it keeps going until its empty. Here is what I have written.

void MathStack::addAll()
{
    int num = 0,num2 = 0, sum = 0;

    while(!isEmpty())
    {
    //Pop the first two values off the stack.
    pop(num);
    cout << "Popping " << num << "\n";
    pop(num2);
    cout << "Popping " << num2 << "\n";

    //Add the two values, store in sum.
    sum = num + num2;
    cout << "Sum is " << sum;
    //Push sum back onto the stack.
    push(sum);
    num =0;
    num2 = 0;
    }


}

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:56:36+00:00Added an answer on May 29, 2026 at 9:56 pm

    There is no reason to push the intermediate sum back onto the stack. Just keep the sum in a local, and pop the values off of the stack one at a time:

    void MathStack::addAll()
    {
        int num = 0, sum = 0;
    
        while(!isEmpty())
        {
            pop(num);
            sum += num;
        }
    
        push(sum);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, stack overflow In my database, I already have one table, 'contacts' that contains
Greetings to all the compiler designers here on Stack Overflow. I am currently working
Greetings all, I'm trying to localize a .NET/C# project. I'm using string resource files
Greetings. How can I make access to my article or post by their name?
Greetings all, I have some JSON code that looks like this: { playlist: [
Greetings, I'm trying to make use of the Pulseaudio APIs under Fedora Core 12.
Greetings all, In my application,user can open several QWindows which share the same data.(save
Greetings, There are a lot of such questions on stackoverflow, but all of them
Greetings stackoverflow community! I am working on an Android game that uses Box2D for
Greetings! I'm trying to add simple mail functionality to a little web app and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.