Solved

Need to restructure my logic or rewrite a getline function

Posted on 2004-04-30
6
226 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
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

685 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