Link to home
Start Free TrialLog in
Avatar of Stella Pauley
Stella Pauley

asked on

Need to restructure my logic or rewrite a getline function

Can I email you some code and an external file for your review?

I am having an issue with reading in more than one line from an external file. I tried using variations of the getline function however, my program would bomb. If you delete the second line in the external file my program works. However, if there are 2 or more lines it tries to calculate all lines together. I need to have a delimiter. By default the getline uses \n.

Thanks
Avatar of Member_2_1001466
Member_2_1001466

You can put the code here as well. Perhaps it needs to be stripped to the place where it makes problems. Use of e-mail is discouraged from membership agreement. It doesn't give all experts the same chances.
Avatar of Stella Pauley

ASKER

Here are the entries in the external file postfix.txt:
6 4 + 2 ^ 3 4 % 6 % 2 * -
6 3 % 2 5 ^ +


here is the code:

#include <iostream>
#include<string>
#include <stack>    // defines the stack<T> class template
#include<fstream>
#include<math.h>
using namespace std;


class cstack
  {
  private:
        stack <float> eval_stack;
     
  public:
    void loadStack(char);
        void loadStack(int );
    int unloadStack();
    void displayStack();
      int evaluateStack(int,char, int);

};

bool openfile(ifstream &, char [20]);


int main()
{
    cstack test;
      ifstream ifile;

   

 string line;
 
 int result, right, left, var;
 
 
if (!openfile(ifile,"postfix.txt"))
{
      cout<<"Error opening inputfile.txt "<<endl;
      exit(1);
}
 while(!ifile.fail())
 {
      ifile>>line;
        cout<<line<<" ";
        
      for(int i=0;i<line.length();i++)
        {
             
              if(isdigit(line[i]))
              {  
                    
                    var=line[i]-'0';  //converts character to integer
               
                    test.loadStack(var);
              }
            
              else if(line[i]=='\0')
              {
                    test.loadStack(line[i]);
              }
             
              else
              {
                  right=test.unloadStack();
                  left=test.unloadStack();
                 
                    result=test.evaluateStack(left,line[i],right);
                   test.loadStack(result);
                  
              }
        
        }
   
        
   }
     
     
     cout<<" = "<<test.unloadStack()<<endl; //Pop result off of stack

return 0;
}

void cstack::loadStack(char a)
{
      
      eval_stack.push(a);
   

}
void cstack::loadStack(int a)
{
      
      eval_stack.push(a);
   

}

int cstack::unloadStack()
{
       int var;
       var=eval_stack.top();
       eval_stack.pop();
            return var;
      
}

void cstack::displayStack()
{
      
  while(!eval_stack.empty())
  {
        
        eval_stack.pop();
  }
}

int cstack::evaluateStack(int left,char oper, int right)
{
   int result;
 
 

   switch (oper)
{
    case '+':
      result = left + right;
      break;
    case '-':
      result = left - right;
      break;
    case '*':
      result = left * right;
      break;
    case '/':
      result = left / float(right);
      break;
    case '^':
            result= pow(left,right);
            break;
      case '%':
            result=left % right;
         break;
      default:
            result='\0';   //set result to null for invalid character.
            break;

 }



   return result;
}

  bool openfile(ifstream &file, char filename[20])
{
      bool status;
      file.open(filename);
      if (file.fail())
            status=false;
      else
            status=true;
      return status;
}
streams will treat \n as whitespace and so the program will not recognize it here. You could end each computation with a = sign. Just add a case for it in the evaluateStack function.

Another hint: test first for an operator. If it is not you can convert the string to a number directly; there is no need to do it for each digit seperatly. This will be extremly helpful if you are considering floating points variables.
I am not sure I follow what you mean by ending each computation by a=sign. Each computation should push a result back on the stack.
ASKER CERTIFIED SOLUTION
Avatar of Mafalda
Mafalda

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If the file "lines" end with a different character that '\n' you can use a variation of getline

getline(ifile, s, '|');  // example of using | as the end of the line
getline(ifile, s, ' ');  // example of using blank as the end of the line