You are using a version without Ads of this website. Please, consider donating:

### ifstream and outside file in a function

The whole basis of this program is to do basic math operations using functions (add, sub, etc). What I'm having issues with is trying to set up the function to get the number from the math.txt file and plug it into the program.

Code:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` `````` #include #include #include using namespace std; //Function Prototypes void doSubtraction( ifstream, int ); void doAddition( ifstream, int ); void doMultiplication( ifstream, int ); void doDivision( ifstream, int ); void doExponent( ifstream, int ); void doFactorial( ifstream, int ); int main(){ int add1, add2, sum; ifstream infile; infile.open ("math.txt", ifstream::in); int ch = infile.get(); while (infile.good()) { cout << (char) ch; ch = infile.get(); } infile.close(); return 0; { } { } /*********************************************** Function: doAddition Use: This function calculates adds two numbers. Arguements: Add1 - The first number. Add2 - The second number. Sum - The two numbers added together. Returns: The sum of the two numbers. ***********************************************/ void doAddition( + int add1, add2 ) { add1 + add2 = sum; return sum; } }``````

And this is the text file I'm using (Notepad file)

+ 23 34
- 9 8
+ 100 1
* 8 7
^ 2 5
/ 45 8
! 4
a 12 -9
! 0
^ 7 0
* -9 2
- -50 324
* -7 -3
/ 10 2
/ 1 2
+ 240 360
! 5
S -999 -1
f 8
^ 16 3
/ 9 5

The second value is usually performed (Ex 240 + 360 = whatever) is the order. For the '^', the second value is the raised on the base (ex 16 is raised to the 3rd). For !, it multiplies by how many factors (Ex ! 5 = 1*2*3*4*5).

I'm pretty sure (and tell me if I'm wrong) that once the issue is solved, it basically is the same coding for the rest of the functions, just changing the numbers and operations.

Thank you for any help
math.txt =

[number 1] [operator] [number 2]

main.cpp:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879`` ``````[ #include #include #include #include using namespace std; void power(int n1,int n2) { int n = n1; for(int i = 0;i < n2;i++) { n = n * n1; } cout << n << endl; } void factorial(int n1,int n2) { int n = 1; for(int i = 1;i < n1 + 1;i++) { n = i * n; } cout << n << endl; } void adder(char ch,int n1,int n2) { switch(ch) { case '+': cout << n1 << " + " << n2 << " = " << n1 + n2 << endl; break; case '-': cout << n1 << " - " << n2 << " = " << n1 - n2 << endl; break; case '*': cout << n1 << " * " << n2 << " = " << n1 * n2 << endl; break; case '/': cout << n1 << " / " << n2 << " = " << n1 / n2 << endl; break; case '%': cout << n1 << " % " << n2 << " = " << n1 % n2 << endl; break; case '^': cout << n1 << " ^ " << n2 << " = "; power(n1,n2); break; case '!': cout << "! " << n1 << " = "; factorial(n1,n2); break; default: cout << ch << " is not recognizable..." << endl; break; } } int main(int nNumberofArgs,char* pszArgs[]) { int n1,n2; char ch; ifstream data("math.txt"); for(;;) { data >> n1 >> ch >> n2; if(data.fail()) { cout << "End of file!" << endl; system("PAUSE"); return 0; } adder(ch,n1,n2); } return 0; } ``````
Last edited on
This helps out a little bit. Where does the math.txt file input itself into the program or do the functions I make input the numbers in?

Plus the else statements seems to put everything under one function, where I just want to put each operation into it's own separate functions
Last edited on
I can't figure this out either. Greenleaf, what exactly do you mean?
I tried different approaches but I still cannot get the math.txt to input into the functions
ifstream data("math.txt");

ifstream = input from file

data = object data

("math.txt") = file name

you can manually input file name, I have a program that can do that, tell me
if you want it.

for the calculation function, I use a switch statement.

You only need to pass on numbers and operator to function, you can simplify the program by only extracting the numbers once in the main().
Greenleaf, could you possibly post this with the above program from OP? I have the same assignment and would like to see it implemented in the program. Thanks!
I would like to see this program. I understand the concept behind it, but I'd like to visually see the program as well
ET21, if you do figure it out, please post back here as I am still confused myself
Um, idk how to put pics on reply...

but you can copy and paste onto your compiler
What do you mean? All I get is "end of file" and that's it. It doesn't plug in any of the numbers
Same here
Well I got this far but I'm getting a couple compilation errors. One is error: expected ';' before 'namespace' and the other error is error: expected '}' at end of input. Any advice?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103`` ``````#include #include #include #include using namespace std; int num1, num2, tempNum, result; int remain; void doAddition (int, int); void doSubtraction (int, int); void doDivision (int, int); void doMultiplication (int, int); void doExponent (int, int); void doFactorial (int); int main() { ifstream inFile; char ch; inFile.open("math.txt"); if(inFile.fail()) { cout << "The math.txt file failed to open"; exit(-1); } inFile >> ch; while(inFile) { switch(ch) { case '+':doAddition (num1, num2); break; case '-':doSubtraction (num1, num2); break; case '*':doMultiplication (num1, num2); break; case '^':doExponent (num1, num2); break; case '/':doDivision (num1, num2); break; case '!':doFactorial (num1); break; default:inFile.ignore (100, '\n' ); break; } } inFile.close(); } void doAddition ( int num1, int num2 ) { //This function will calculate and display a sum. It takes one argument: a reference to an input file stream, and returns nothing. result=num1+num2; cout << "Addition\t" << num1 << "\t" << num2 << "\tsum:" << result; } void doSubtraction (int num1, int num2 ) { //This function will calculate and display the difference of two numbers. It takes one argument: a reference to an input file stream, and returns nothing. result=num1-num2; cout<< "Subtraction\t" << num1 << "\t" << num2 << "\tdifference:" << result; } void doMultiplication (int num1, int num2 ) { //This function will calculate and display a product. It takes one argument: a reference to an input file stream, and returns nothing. result=num1*num2; cout << "Multiplication\t" << num1<< "\t" << num2 << "\tproduct:" << result; } void doExponent ( int num1, int num2) { //This function will calculate and display the product of raising a number to a power. It takes one argument: a reference to an input file stream, and returns nothing. result=powl(num1,num2); cout << "Exponent\t" << num1 << "\t" << num2 << "\tproduct:" << result; } void doDivision ( int num1, int num2) { //This function will calculate and display the quotient and remainder of a division operation between two numbers. It takes one argument: a reference to an input file stream, and returns nothing. result= num1/ num2; remainder=num1%num2; cout << "Division\t" << num1 <<"\t" << num2 << "\tQuo:" << result; cout << "Remain:" << remain; } void doFactorial (int num1) { //This function will calculate and display the factorial of a number. It takes one argument: a reference to an input file stream, and returns nothing. result = 1; for(tempNum=num1,tempNum >= 1, tempNum-- ) result = result * tempNum; cout << "Factorial\t" << num1 <<"\t\t\t\tproduct:" << result << endl; } ``````
You need semicolons after your for statement in the Factorial function.
 ``1234567`` ``````void doFactorial (int n1) { result = 1; for(tn = n1; tn >=1; tn--); result = result * tn; cout << "Factorial\t" << n1 << "\t\t\t\tproduct:" << result << endl; }``````
Last edited on
I'm still getting the same errors even after changing that. I don't know what it could be
Last edited on
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: