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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

#include <iostream>
#include <iomanip>
#include <fstream>

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
[
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <fstream>
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?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

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.
1
2
3
4
5
6
7
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.