Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
i have have written a code to to take all expressions in an input text file and calculate them. But the problem is, it is only calculating the first expressions. And also if i include --------- in the text file to separate the expression it will give me compiler error.

What I have tried:

C++
int main ()
{
   string inputLine;
   ifstream file ("input.txt");
   {
       while (std::getline (file, inputLine))
       {     
           while(!file.eof())
           {
               spaces
               file.ignore(1,'\n');
               inputLine = trim (removeBackspaces (inputLine));
               Parser p (inputLine);
               double value = p.Evaluate ();
               ofstream file;
               file.open("output.txt");
               file <<std::cout << "Result = " << value << std::endl;
               double abc = p ["abc"];
               file<<std::cout << "abc = " << abc << std::endl;
               file<<std::cout<<"----------------------"<<endl;
           }
       } 
       file.close();
    }
}

And this is the input.txt file
----
sin(0)
----
cos(0)
----
pi = 3.14 radius = 3 sin( pi / 2 ) + pi * radius ^ 2
----
1 + 2
----
10 - 5
----
3 * 4
----
12 / 4
------------
2 ^ 3
Posted
Updated 9-Dec-18 10:25am
v3

You must use the debugger to find your error. For me it looks like you should change the first while loop heads for the start.
 
Share this answer
 
First, you should move the declaration of the output file object outside the while loop. You don't need to construct it and open the file for every line you read from the input file. It should be constructed once and left open while you are reading input.

The line with just "spaces" is unclear. That will likely cause a compile error.

One more thing, this line will likely cause a compile error also :
C++
double abc = p ["abc"];
It looks suspicious but it is hard to know without seeing the definition of the Parser class.
 
Share this answer
 
Comments
Leigh Omar Alpha 10-Dec-18 0:27am    
thank you very much Rick York its read all the lines now . i have also one problem when i introduced --------- to separate the lines it gives me compiler error.please if you can help me to find a way that the compiler will skip this--------- and only recongnizes the expressions.
Rick York 10-Dec-18 11:07am    
That is not a compiler problem. That is a logic problem. Try comparing your input string with "----" - using a length-limited comparison. In the C world the function strncmp would be used. I don't know what the corresponding std::string comparison method would be but that is what you should use. You could use strncmp and pass it inputLine.c_str() if you want to. The code would look like this:
           inputLine = trim (removeBackspaces (inputLine));
           if( strncmp( "----", inputLine.c_str(), 4 ) == 0 )
               continue;     // don't process this one - read another
Rick York 10-Dec-18 13:13pm    
One more thing, the second while statement should be an if. Something like this :
    if( file.eof() )
        break;
Actually, if I were you I would just comment that line off to start. If getline fails that means you are at the end of the file so the additional check is not needed. The fact is, I write file parsers all the time and I have never checked for EoF. I always use fgets (the C equivalent of getline) and loop until it fails for line-parsing. For character-based parsing I read one character at a time until failure. I have never seen a need to check for EoF.
Leigh Omar Alpha 13-Dec-18 12:54pm    
also when i have an expression that has binary and hexadecimal like
----
pi = 3.14
radius = 3
sin( pi / 2 ) + pi * radius ^ 2 + 0x1F - 1100b
it will note calculte because of the binary and hexadecimal values.
i want the input file to recognize binary and hexadecimal numbers and convert them to decimal or integer so my expression will be calculated . that is my main problem righ now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900