Ok I just need an idea or a source code to start my project.

I need to read a text file with fstream or any other thing.

This will be my txt file (nothing the same, just an example)

a = 6 
b = 3
c = 7
a - b
b * c
c % b
a + c

So, when I read this txt file, make an output of the result value

Example:

a - b = 3
b * c = 21

>>>ETC,,,,,,,,,

Recommended Answers

All 12 Replies

Start out your program very simply, just create a program that does nothing more than read each line of the file and display them on the console screen. Once you have that compiled and working correctly you can add additional code to make the program behave as you have described. Just do it a little bit at a time and pretty soon you will have the entire project finished.

Start out your program very simply

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main() {
	//char sum = 0;
	char x;
	ifstream inFile("1.txt");
	if (!inFile) {
		cout << "Unable to open file";
		exit(1);
	}
	while (x != '\n') {
		while (inFile >> x) {

			cout<<"x = "<<x<<endl;

		}
	}
	inFile.close();
	getchar();
	return 0;
}
My Text File Example( 1.txt) 

    a = 6
    b = 3
    c = 7
    a - b
    b * c
    c % b
    a + c

I want the results of this math operations in Screen???
b * c
c % b
a + c

Please ='(

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main() {
	//char sum = 0;
	char x;
	ifstream inFile("1.txt");
	if (!inFile) {
		cout << "Unable to open file";
		exit(1);
	}
	while (x != '\n') {
		while (inFile >> x) {

			cout<<"x = "<<x<<endl;

		}
	}
	inFile.close();
	getchar();
	return 0;
}

Does this read in a line at a time? I don't think it's going to do what you want. You should try using std::string instead of char , in combination with getline()

You now have to look at each character and decide if it's a
1) letter
2) digit
3) operator


And set values accordingly. For example, you could do something like:

string variable[10];  // hold 10 variable names (a, b, c, etc)
int    varindex;      // the index of the variable name from the line
float  values[10];    // the value of each variable above
char   operator;      // the operator on the line being read

Then when you get to the end of the line, call a function to process the data you read.

why dont you use fopen instead?

why dont you use fopen instead?

What would that accomplish?

fopen is simpler?

fopen is simpler?

Are you asking me or telling me? If you're asking me then I'll say no. If you're telling me then I'll say you're wrong. Please explain how this:

FILE *inFile = fopen("1.txt", "r");

is any simpler than this:

ifstream inFile("1.txt");

At best they're equivalent. Of course, I'm fairly confident that by fopen() you mean the stdio library rather than the iostream library, but that's a larger comparison that will require examples and a clear definition of "simpler".

#include<iostream>
#include<cmath>
#include<fstream>
#include<cstdlib>

using namespace std;

int main(){
	char filename[1000];
	ifstream help; 

	cout<<"\nEnter filename you want to read: ";
	cin.getline(filename, 1000); 
	help.open(filename);

	if(!help.is_open()){
		exit(EXIT_FAILURE); 
		cout<<"Sorry Error Occured!"; 
	}
	cout<<endl;
	cout<<"File sucessfully read\n";

	char word[1000];
	help >> word; 

	while(help.good()){ 

		cout << word << " "; 
		help >> word; 
		cout<<endl;
	}
}

Now How I Read the operators +, -, *, %, / ,, etc.....and apply these the get the result of two numbers

a+b = 9 ... etc...

Since each line of the file contains three words (symbol operator symbol) then I'd use three different variables to read in the line

std::string symbol, operator, result;
while( help >> symbol >> operator >> result)
{
   switch(operator[0])
   {
     case '=':
       // do something here
       break;
       // do something here
       break;
     case '+':
       // do something here
       break;
     case '-':
       // do something here
       break;
     case '*':
       // do something here
       break;
     case '/':
       // do something here
       break;
     case '%':
       // do something here
       break;
   }
}

Narue, forgive me, I dont know how to quote posts lol

well, because fopen has more then one argument to open a file, maybe it is more advanced then ifstream? I never used ifstream, so if you can tell me the differences it would be nice. wouldnt ifstream be some C method when fopen is c++?

well, because fopen has more then one argument to open a file, maybe it is more advanced then ifstream?

Darn, and here I was hoping someone might actually debate with me the merits of stdio vs. iostream from a position of knowledge and understanding. Since you clearly have no clue that ifstream's constructor takes a second default argument for the open mode, I'd suggest learning the iostream library. You might like it.

so if you can tell me the differences it would be nice

That's a broad topic, and while I'm well versed in both the stdio and iostream libraries, I'd much prefer answering pointed questions to fielding vague requests about the "differences". For example, you can ask what the equivalent iostream method would be for a piece of code using stdio, or which library would be better for <insert task here> and why.

Though if you do have further questions after doing some research, please start a new thread so we can avoid sidetracking this one even further.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.