Array Stack Problem

This is a discussion on Array Stack Problem within the C++ Programming forums, part of the General Programming Boards category; Here is what I am trying to do: I am inputting this file with this expression: XYZ+A*B/+CX*- I already have ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Array Stack Problem

    Here is what I am trying to do:
    I am inputting this file with this expression: XYZ+A*B/+CX*-
    I already have a character arrays with these entered into it:
    A=2
    B=10
    C=5
    X=8
    Y=6
    Z=4
    When I input a character it's corresponding number goes on the stack
    and then when I come to a operator I pop the two numbers from the
    stack and add them if the operator is + or multiply if the operator is
    a *, or subtract if the operator is a -, or divide it the operator
    is a /. The sum is just applied to the next mathmatical operation.

    The alogorithm should go like this:
    while(Infile)
    if (inputchar == alphabet)
    got to the table array
    find the match on the table arrya
    Then pust the value to the stack
    else if(inputchar == operators)
    go to stack
    pop out two items
    then apply the operators
    the output result should be pushed to the stack

    I get a output of 2 when I should get a output of -30. I ran the debugger
    and at this line inFile2>>cInpChar; I am getting 44 ',' and at this line if(inFile2.eof()){
    I am getting 88 'x'.
    I can't understand why I am getting those values for my input number when
    it should be inputting the value that is stored in the array. My header
    and implementation file are correct.

    [ccode]
    #include<iostream.h>
    #include<fstream>

    #include<iomanip>
    #include"cs.cpp"
    using namespace std;

    int main()
    {

    // Data Arrays
    numberstack os;
    const int CNUM_MaxCnt= 50;
    const int CNUM_MaxVarNameLen= 32;

    char szVarName[CNUM_MaxCnt][CNUM_MaxVarNameLen];
    char szEqualSign[CNUM_MaxCnt][2];
    int nVarVal[CNUM_MaxCnt];
    int Topitem;
    int firstitem = 0;

    ifstream inFile; inFile.open( "input1.txt");
    ofstream outFile; outFile.open("output3.txt");
    ifstream inFile2; inFile2.open( "input2.txt");
    //ofstream outFile2; outFile.open("output3.txt");
    // X+(Y+Z)*A/B-C*X:A=2,B=10,C=5,X=8,Y=6,Z=4

    char cInpChar;
    while( !inFile.eof() ) {
    inFile >> cInpChar;
    if ( cInpChar == ':') {
    break; // all done
    }
    outFile << cInpChar;
    }
    outFile.close();
    // now at A=2,B=10,C=5,X=8,Y=6,Z=4
    int nCur= 0;
    while( !inFile.eof() ) {
    inFile.get( szVarName[nCur],CNUM_MaxVarNameLen,'=');

    inFile >> szEqualSign[nCur][0]; // get the = sign
    szEqualSign[nCur][1]= '\0'; // terminate the strung

    inFile >> nVarVal[nCur]; // read an int from the stream
    inFile >> cInpChar; // skip the comma

    nCur++;
    }
    for (int j=0; j< nCur; j++ ) {
    cout << szVarName[j] << szEqualSign[j] << nVarVal[j] << endl;
    }
    int nResult = 0;
    int sum = 0;
    int nVal = 0;

    while(true){
    inFile2>>cInpChar;

    if(inFile2.eof()){
    break;
    }
    bool fVarFound=false;
    for(int i = 0; i < CNUM_MaxCnt; i++)
    {
    if(szVarName[i][0]=cInpChar)
    {
    nVal=nVarVal[i];
    fVarFound = true;
    break;
    }
    if(szVarName[i][0]==0){
    break;
    }
    }
    if(fVarFound){
    os.Push(nVal);
    continue;
    }
    if(cInpChar=='+'||cInpChar=='-'||cInpChar=='*'||cInpChar=='/')
    {
    int nValTop=os.top(); os.pop();
    int nValNxt=os.top(); os.pop();
    int nResult = 0;
    switch(cInpChar)
    {
    case '+': nResult=nValTop+nValNxt;
    break;
    case '-': nResult=nValNxt-nValTop;
    break;
    case '*': nResult=nValTop*nValNxt;
    break;
    case '/': nResult=nValNxt/nValTop;
    break;
    }

    os.Push(nResult);

    }

    else
    {
    cout<<"Invalid variable"<<endl;
    exit(1);
    }

    }

    int nFinal=os.top();
    os.pop();

    cout<<nFinal<<endl;


    return(0);
    }

    #include<iostream.h>
    #include<iomanip>

    #include"cs.h"
    numberstack::numberstack()
    {
    TopIndex = -1;
    }

    //--------------------------------------------------------------------
    //Function to check if the stack is empty.
    bool numberstack::Isstackempty() const
    {
    return (TopIndex == -1);
    }
    //--------------------------------------------------------------------
    //Function to check if the stack is full.
    bool numberstack::Isstackfull() const
    {
    return (TopIndex == NumberArray - 1);
    }
    //--------------------------------------------------------------------
    //Push function to push the characters and operators on the stack.
    void numberstack::Push(int a)
    {
    if( !Isstackfull() )
    arr[++TopIndex] = a;
    else
    {
    cout<<"Stack is full." << endl;
    exit(1);
    }
    }
    //Returns top element/
    //--------------------------------------------------------------------
    char numberstack::top() const
    {
    return arr[TopIndex];

    }
    //--------------------------------------------------------------------
    //Pop function to pop the characters and operators on the stack.
    void numberstack:op()
    {
    /*char result=arr[TopIndex];*/
    if( !Isstackempty() )
    TopIndex--;
    else
    {
    cout<<"Stack is empty.";
    exit(1);
    }
    //return result;
    }

    //Global variable to set the array to 26
    const int NumberArray = 26;

    //Class Structure
    class numberstack
    {
    //Public functions in the class and the constructure.
    public:


    bool Isstackempty() const;
    bool Isstackfull() const;

    void Push(int a);
    char top() const;
    void pop();

    numberstack();
    //Private variables in the class stack.
    private:
    int TopIndex;
    int arr[NumberArray];



    };

  2. #2
    zoo
    Guest
    Just one little typo. You need an == instead of an = (see below).

    Code:
    int nResult = 0; 
    int sum = 0;
    int nVal = 0; 
    
    while(true){ 
       inFile2>>cInpChar; 
    
       if(inFile2.eof()){ 
          break; 
       } 
       bool fVarFound=false; 
       for(int i = 0; i < CNUM_MaxCnt; i++) 
       { 
          if(szVarName[i][0]==cInpChar)  //CHANGE THIS
          { 
             nVal=nVarVal[i]; 
             fVarFound = true; 
             break; 
          } 
          if(szVarName[i][0]==0){ 
             break; 
          } 
    }

  3. #3
    zoo
    Guest
    Your code does give the correct answer of -30.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    Man!!! Thanks!!! How could I have missed that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Stack problem - I've hit a wall!
    By miniwhip in forum C Programming
    Replies: 7
    Last Post: 11-14-2007, 03:05 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. Finished Stack
    By the pooper in forum C Programming
    Replies: 11
    Last Post: 02-02-2005, 10:52 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21