Solved

Need to restructure my logic or rewrite a getline function

Posted on 2004-04-30
6
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

739 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