Solved

Need to restructure my logic or rewrite a getline function

Posted on 2004-04-30
6
221 Views
Last Modified: 2008-03-17
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
0
Comment
Question by:Stella Pauley
  • 2
  • 2
  • 2
6 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 10960779
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.
0
 
LVL 1

Author Comment

by:Stella Pauley
ID: 10960819
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;
}
0
 
LVL 13

Expert Comment

by:SteH
ID: 10960979
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.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:Stella Pauley
ID: 10961305
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.
0
 
LVL 6

Accepted Solution

by:
Mafalda earned 125 total points
ID: 10961469
My suggestions for the following part

while(!ifile.fail())
 {
      ifile>>line;
       cout<<line<<" ";


use a

while(!ifile.eof())
 {

to test for the end of file when no more data is available.

If the file is a textfile built of lines use a getline to read the line

string s;
getline(ifile, s);

in any case I do not recommend using operator>> as it would read  until the blank and if you read integers or chars as many bytes as needed and although it is ok it might confuse you.

Then find in the string s the separated values (find or find_first_of etc.).

This will help you go over the first obstacle of reading multiple lines.

0
 
LVL 6

Expert Comment

by:Mafalda
ID: 10961498
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
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now