Reading arithmetic from file

Hi guys, School is out and I just had my semester of C++ in college and I know I need more help so I got more assignments to do over winter break.

I'm reading "simple" arithmetic problems from a file and I'm supposed to output them on the screen with the answer
Ex;(file)
1
2
3
4
5
 5 * 3
7 - 2
955 + 14
54 / 8
&


On screen output:
5 * 3 = 15
7 – 2 = 5
955 + 14 = 969
54 / 8 = 6


I'm having a really hard time doing this... I thought it was easy but I was wrong.
There's spaces and I don't know how to get the information...

My problem:
I've tried
cin.getline method with a delimiter of \n but I'm supposed to stop reading at '&' and I can't hard code inStream.getline(x,5,'\n'); until the last line.

So anyone have any suggestions how I would solve this? Thanks :I
Last edited on
You can use strtok function ( http://www.cplusplus.com/reference/clibrary/cstring/strtok/ ) to split you string into tokens.
Or somthing like this:

1
2
3
4
5
6
7
8
9
10
	inline void tokenizer( std::vector<std::string>& vTokens, const  std::string& inString, const  std::string& inDelim )
	{
		size_t  start = 0, end = 0;
		while ( end != std::string::npos )
		{
			end = inString.find( inDelim, start );
			vTokens.push_back( inString.substr( start, ( end == std::string::npos ) ? std::string::npos : end - start ) );
			start = ( ( end > ( std::string::npos - inDelim.size( ) ) ) ?  std::string::npos : end + inDelim.size( ) );
		}
	}
Last edited on
Alright, I'll try that, but I don't know what you mean by the 2nd one. Don't know how to read that
O: . But we haven't learned vectors x: Thank you :D
1
2
3
4
5
6
7
8
9
std::fstream fileStream;
std::string sBuffer;

...

std::vector<std::string> vTokens;
getline( fileStream, sBuffer );
tokenizer( vTokens, sBuffer, " "  );


vToken[0] - first value
vToken[1] - arithmetic "action"
vToken[2] - second value

something like this...









Last edited on
Topic archived. No new replies allowed.