Files and Functions...

I'm attempting to pass a text file to a function to get input for another function. I'm supposed to then output the results in another text file.
I believe I'm on the right track, but I'm getting a compiler error on line 24.
Stating "Undefined reference to 'getInput (std::etc.....


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
#include <iostream>
#include <bitset>
#include <fstream>

using namespace std;

void getInput (ifstream&, int &, int&, char &);
void operations(ofstream&, int, int, char);
void printBinary(ofstream&, int );


int main()
{
    ifstream inFile;
    ofstream outFile;
    int num1, num2;
    char opChoice;
    inFile.open ("input.txt");
    outFile.open ("output.txt");
    if (!inFile && !outFile)
    {
    while (!inFile)
    {
    getInput (inFile, num1, num2, opChoice);
    operations(outFile, num1, num2, opChoice);
    }
    }
    else
    cout << "One or more files not available.";
    return 0;
}

This is what I'm attempting to pass the file to. The Format of the text file would be bitwise operator space integer space integer. Since some of the bitwise operator are two character long I'm ignoring until the space is encountered. Everything except for the first three lines is commented out from a previous part of the problem.
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
void getInput (fstream &inFile, int &num1, int &num2, char &opChoice)
{




        inFile >> opChoice;
        inFile.ignore(1,' ');
        inFile >> num1 >> num2;

    /*cout << "Please enter the corresponding number for the operation you would like to do.\n"
        <<"1. & AND operation.\n2. | OR operation.\n3. ^ XOR operation.\n4. ~ Inverting.\n5. "
        << "<< Shift left.\n6. >> Shift Right.\n";
    cin >> opChoice;
    if (opChoice >= 1 && opChoice <= 6)
        rangeCheck = true;
    else
        cout << "Please enter 1-6. Try again.\n";
    }
    if (opChoice == 4)
    {
        cout << "Please enter a integer to perform the operation: ";
        cin >> num1;
        cin.ignore (80, '\n');
    }
    else
    {
        cout << "Please enter the first integer to perform the operation: ";
        cin >> num1;
        cin.ignore (80, '\n');
        cout << "Please enter the second integer to perform the operation: ";
        cin >> num2;
        cin.ignore (80, '\n');*/

}

I haven't tested the program yet, so I don't know if this function works correctly, but I'd like to figure that out myself.
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
void operations (ofstream &outFile, int num1, int num2, char opChoice)
{
    int result;
    switch (opChoice){
    case '&': outFile << "Applying the AND operator & to:\n" << num1 << " = ";
            printBinary(outFile, num1);
            outFile << num2 << " = ";
            printBinary(outFile, num2);
            outFile << "\n";
            result = (num1 & num2);
            outFile << "Produces \n";
            outFile << result << " = ";
            printBinary(outFile, result);
            break;
    case '|': outFile << "Applying the OR operator | to:\n" << num1 << " = ";
            printBinary(outFile, num1)
;            outFile << num2 << " = ";
            printBinary(outFile,num2);
            outFile << "\n";
            result = (num1 | num2);
            outFile << "Produces \n";
            outFile << result << " = ";
            printBinary(outFile, result);
            break;
    case '^': outFile << "Applying the XOR operator ^ to:\n" << num1 << " = ";
            printBinary(outFile, num1);
            outFile << num2 << " = ";
            printBinary(outFile, num2);
            outFile << "\n";
            result = (num1 ^ num2);
            outFile << "Produces \n";
            outFile << result << " = ";
            printBinary(outFile, result);
            break;
    case '~': outFile << "Applying the Inverting operator ~ to:\n" << num1 << " = ";
            printBinary(outFile,num1);
            outFile << "\n";
            result = ~num1;
            outFile << "Produces \n";
            outFile << result << " = ";
            printBinary(outFile, result);
            break;
    case '<': outFile << "Applying the Shift Left operator << to:\n" << num1 << " = ";
            printBinary(outFile,num1);
            outFile << num2 << " = ";
            printBinary(outFile,num2);
            outFile << "\n";
            result = (num1 << num2);
            outFile << "Produces \n";
            outFile << result << " = ";
            printBinary(outFile, result);
            break;
    case '>': outFile << "Applying the Shift Right operator >> to:\n" << num1 << " = ";
            printBinary(outFile,num1);
            outFile << num2 << " = ";
            printBinary(outFile,num2);
            outFile << "\n";
            result = (num1 >> num2);
            outFile << "Produces \n";
            outFile << result << " = ";
            printBinary(outFile, result);
            break;
    }
}

I know this works as intended, except for the new ofstream parameter.
1
2
3
4
5
6
void printBinary(ofstream &outFile, int result) {
    bitset<32> binNum (result);
    outFile << (binNum);
    outFile << endl;

}
Last edited on
Seriously I post these then I look at the post and I see what could be the issue. I'll be back.
Just in case you don't see it when you check, the getInput function prototype lists the first parameter as ifstream while the function declaration lists it as fstream. These need to match.
Yup forgot a pesky little i in the ifstream in the function declaration.
So the files just have to be in my project folder correct?
If you are running from an IDE/compiler the project folder should be fine, They need to be in the same folder as the .exe if you are running from that directly.
It was a program error. (!inFile) should have been just (inFile) I did it with outFile too.
There is just one last issue. It is writing the output desired, but twice. I don't see why that should be.
And now I've a tinkered it to submission and it gave up on me. I am now getting an infinite loop and I don't know why. The only thing I can think of is that ifstream is not getting an end of file character. My issues started when I tried to add another line to the input file. It currently looks like this
1
2
3
& 1 1
^ 2 1

Grr...
Last edited on
Topic archived. No new replies allowed.