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

  • Home
  • SEARCH
  • 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 8932799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:23:28+00:00 2026-06-15T09:23:28+00:00

I am new to posting and relatively new to coding in c++. My question

  • 0

I am new to posting and relatively new to coding in c++.

My question involves:
Handling decimal values in evaluating postfix using stacks in c++. This is a homework assignment, but I have already gotten my code to work for the basic requirement. I am not asking for help with my homework. My professor wants us to brute force the assignment, so I have avoided many shortcuts using special header files such as <stack>.

I have researched the question and was not satisfied with the answers given. No one else seems to have split up their code like mine. I am having an issue passing a value from the inner loop which detects the decimal values to the rest of the main function. The code compiles and runs, but my final result is gibberish. I found a post which states multiple values cannot be passed from a function with 'return', but I do need at least two values returned from the inner decimal function. I do not understand the 'tuple' or 'pair' commands.

I know this is a wordy prelude, but I do want any readers to understand I have read pre-requisite materials and did lost of research. Personally, I hate brute force methods. I understand that a response may not be in time to help with my project, but I have been working on this for weeks. I would like to get the satisfaction of seeing this method work as conceived. Suggestions and pointers are very much welcome.

Here is the link to my code:
link

Here is my code:

//*****************************************************************************
// Postfix expression evaluation
// 11/13/2012
// by DJH
//
// all applicable copyrights apply
//
// this program evaluates an input postfix string
// 
// created using Dev-C++ 5.2.0.3
//*****************************************************************************

// ----------------------------------libraries---------------------------------

#include <iostream>                     // For cin, cout and endl
#include <string>
#include <cctype>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <cmath>
#include <iomanip>

using namespace std;
// ------------------------------ Globals -------------------------------------
string postfix;

// --------------------------- stack class ------------------------------------
class Stack {
public:
    enum {MaxStack = 50};
    void init() {top = -1;}
    void push( double p ) {
        if ( isFull() ) {
            cerr << "Full Stack. DON'T PUSH\n";
            return;
        }
        else {
            arr[ ++top ] = p;
            cout << "Just pushed " << p << endl;
            return;}
    }
    int pop() {
        if (isEmpty() ) {
            cerr << "\tEmpty Stack. Don't Pop\n\n";
            return 1;
        }
        else
        return arr[top--];
    }
    bool isEmpty() {return top < 0 ? 1 : 0;}
    bool isFull() {return top >= MaxStack -1 ? top : 0;}
    void dump_stack() {
        cout << "The Stack contents, from top to bottom, from a stack dump are: " << endl;
        for (int s = top; s >= 0; s--)
        cout << "\t\t" << arr[s] << endl;
    }
private:
    int top;
    double arr[MaxStack];
} pStack;
// ------------------------------ end stack class -----------------------------

// -----------------------------function prototypes----------------------------
void evalPostfix( string);
double decimalEvaluate( string, int, double);
// ----------------------------------------------------------------------------

// -----------------------------------Main()-----------------------------------
int main() {
    cout << "Enter a postfix expression\n\t (without spaces - using '_' for delimiters):\n\t";
    cout << "For example: 8_5_3_+_*_2_/_5_+\n" << endl;
    getline(cin, postfix);

    //  postfix = "7_2_*_5_+_2_*";
    cout << "You entered: " << postfix << endl;

    int c = 0;

    while (postfix[c] != '\0') {
        // this loop counts the characters in the input string including 
        // whitespace
        ++c;
    }

    cout << "\tThe string length is:\t" << c << endl;
    evalPostfix(postfix);
    int result = pStack.pop();
    cout << "The result of the postfix expression is: " << result << endl;

    /*
     stack commands:
     Stack a_stack;                  // creates new stack
     a_stack.init();                 // initializes top element
     a_stack.pop();                  // pops top of stack
     a_stack.push(n);                // push element to top of stack
     a_stack.dump_stack();           // displays the contents of the stack from top to bottom
     */
    return 0;
    cin.get();
}

// --------------------------------end of Main()-------------------------------

// ------------------------------functions follow------------------------------
void evalPostfix( string) {
    double ch = 0, dc = 0, b = 0, a = 0, d = 0;
    double tempDC = 0;
    double tempDCD = 0;
    char op;
    int i = 0, j = 0, k = 0, m = 0, n = 0, q = 0;
    while (postfix[i] != '\0') {

        if (postfix[i] == '_') {
            i++;
        } else if (isdigit(postfix[i])) {
            ch = postfix[i] - '0';         // for numbers only
            j = i + 1;
            while (postfix[j] != '_') {
                if (isdigit(postfix[j])) {
                    ch = ch * 10 + (postfix[j] - '0');
                    k = j + 1;
                    // this accounts for decimals by skipping the '.' and
                    // conducting operations on trailing numbers
                    if (postfix[k] == '.') {
                        dc = 0;
                        decimalEvaluate(postfix, k, dc);
                        dc = tempDC / tempDCD;
                        d = ch + dc;
                        k++;
                    }
                    j = k - 1;
                    j++;
                }
            }
            cout << "Post decimal function k: " << k << endl;
            cout << "Post decimal function dc: " << setprecision(12) << dc
                    << endl;
            cout << "Post decimal function d: " << d << endl;
            pStack.push(d);
            i = j - 1;
            i++;
        } else if (postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*'
                || postfix[i] == '/' || postfix[i] == '^') {
            b = pStack.pop();
            a = pStack.pop();
            op = postfix[i];         // for operators only
            switch (op) {
            case '+':
                pStack.push(a + b);
                break;
            case '-':
                pStack.push(a - b);
                break;
            case '*':
                pStack.push(a * b);
                break;
            case '^':
                pStack.push(pow(a, b));
                break;
            case '/':
                if (b == 0) {
                    cout << "Division by zero not allowed!" << endl;
                    cin.get();
                    exit(0);
                }
                pStack.push(a / b);
            default:
                cout << "Invalid Operation" << endl;
            }
            i++;
        }

    }
}
// ----------------------------------------------------------------------------

double decimalEvaluate(string postfix, int k, double dc) {
    dc = 0;
    double tempDC = 0;
    double tempDCD = 0;
    int n = 0, m = 0, lenDC = 0;
    n = k;

    while (postfix[n] != '_') {
        if ((isdigit(postfix[n])) == false) {
            n++;
        }
        cout << "This step (1) n: " << n << endl;
        if (isdigit(postfix[n])) {
            m = n;
            // assumes characters between a '.' and '_' are all digits
            // (may need to check)
            while (postfix[m] != '_') {
                lenDC++;
                // this loop counts the digits in the input trailing a decimal
                // point
                m++; 
            }

            cout << "This step (2) m: " << m << endl;
            cout << "This step (2) lenDC: " << lenDC << endl;

            while ((postfix[n]) != '_') {
                tempDC = tempDC * 10 + (postfix[n]) - '0';
                n++;
            }

            cout << "This step (3) n: " << n << endl;
            cout << "This step (3) tempDC: " << tempDC << endl;
        }

        k = n;
        tempDCD = pow(10, lenDC);
        dc = tempDC / tempDCD;

        cout << "This step (4) k: " << k << endl;
        cout << "This step (4) tempDCD: " << tempDCD << endl;
        cout << "This step (4) tempDC: " << tempDC << endl;
        cout << "This step (4) dc: " << dc << endl;
    }

    return dc, k, tempDC, tempDCD;
}
  • 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-06-15T09:23:29+00:00Added an answer on June 15, 2026 at 9:23 am

    Pass the variables by reference:

    void decimalEvaluate (string postfix, int& k, double& dc, double& tempDC, double& tempDCD)
    {
        tempDC = 0.0;
        //...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first posting on SO, and I'm relatively new to Haskell, so
Since my other bug got solved, I'm posting a new question for this bug.
I'm relatively new to this, so sorry if I'm posting in the wrong place.
I'm posting a file in Javascript using the new FormData interface. When I send
I'm new to posting on this forum but can't tell you how many times
This is a new question which arose out of this question Due to answers,
DISCLAIMER: I'm relatively new to Linux. I debated putting this on SuperUser or ServerFault
I was really hoping to avoid posting a new question, but I cannot find
I'm relatively new to coding in general, first year CS student and all of
i am pretty new to all this Monodroid stuff. I am porting my Windows

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.