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
Solved

Need to restructure my logic or rewrite a getline function

Posted on 2004-04-30
6
224 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

791 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