need help changing stuff in an input file and outputting it to an output file

In function ‘void get_line(std::ifstream&, std::ofstream&, std::__cxx11::string, std::__cxx11::string)’:
lab10.cc:49:10: error: no match for ‘operator<<’ (operand types are ‘std::ofstream {aka std::basic_ofstream<char>}’ and ‘<unresolved overloaded function type>’)
out << toupper;
is the error i get which i think its saying my function is overloaded but ive never dealt with overloaded functions before.

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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <cstdlib>

using namespace std;

int get_file (ifstream& in, ofstream& out, string filename, string output);
void get_line (ifstream& in, ofstream& out, string filename, string output);

int main () {
ifstream in;
ofstream out;
string filename, output;
get_file (in, out, filename, output);
get_line (in, out, filename, output);
}
int get_file (ifstream& in, ofstream& out, string filename, string output) {
        cout << "enter the file you would like to open" << endl;
        cin >> filename;
        in.open (filename.c_str());
        if (in.fail()) {
               cout << "that is not a file" << endl;
               return 0;
        }
        cout << "Input the name of the output file" << endl;
        cin >> output;
        out.open (output.c_str());
        if (in.fail()) {
                cout << "that is not a file" << endl;
                return 0;
        }
}
void get_line (ifstream& in, ofstream& out, string filename, string output) {
        string line;
        char words [10000];
        while (!in.eof()){
                for (int i = 0; i < 10000; i++) {
                        while (getline (in, line)) {
                                if (isdigit(words [i])) {
                                        out << '*';
                                }
                                else if (islower (words [i])) {
                                        out << toupper;
                                }
                                else if (isupper (words [i])) {
                                        out << toupper;
                                }
                        }
                }
        }
}
Last edited on
What exactly are you trying to do in your get_line() function? The code you posted doesn't make much sense.

Why are you passing the two strings into this function? You don't seem to be using those parameters in the function.

Why does a "get" function require a output stream?

Do you know the difference between a string and an array of char?

Do you realize that your array of char is not initialized, and contains garbage values?



in the code i am reading in a file to a string and then using getline to put each character into an index in the array and then the if loop is suppose to go through the array and if the index is a digit it outputs to the output file a * if its a lowercase letter it gets outputted as a uppercase if its already uppercase just output it
and i flipped my
1
2
for (int i = 0; i < 10000; i++) {
                        while (getline (in, line)) {

because of the garbage array values
Last edited on
No let's look closer at that function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// First what is the purpose of the last two parameters? Where are you using them?
void get_line (ifstream& in, ofstream& out, string filename, string output) {
        string line;
        char words [10000];   // What is this? 
        while (!in.eof()){
                for (int i = 0; i < 10000; i++) {  // What is the purpose of this loop?
                        while (getline (in, line)) {  // Here you read a single line from the file into the line variable.
                                if (isdigit(words [i])) {  // What is this? Where have you assigned any values to this array of char? 
                                        out << '*';
                                }
                                else if (islower (words [i])) {  // Again you're using an uninitialized variable here.
                                        out << toupper;  // What do you think this is doing? Hint: It's causing a compile error because the compiler doesn't know what your trying to print.
                                }
                                else if (isupper (words [i])) {
                                        out << toupper;
                                }
                        }
                }
        }
}


It looks to me that you don't need that huge character array, nor that huge loop.


While I'm not sure what you're trying to do, this is my guess:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void get_line (ifstream& in, ofstream& out) {
    string line;
    // Read the complete file one line at a time.
    while (getline (in, line)) {
        // if the character is a digit change it to an '*'.
        if (isdigit(line[i])) {
            line[i] = '*';
        } // Change the character to upper case.
        else{
            line[i] = toupper(line[i];
        }

        // Now print the line to the output file.
        out << line << endl;
    }
}


Last edited on
all that does is output the input file
i think thats the reason i was trying to feed each character into an array so that i could re output each character and change them
Actually the code I supplied shouldn't even compile since it is using a variable that is not defined.

This should do the trick:

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
void get_line(istream& in, ostream& out)
{
    string line;
    // Read the complete file one line at a time.

    while(getline(in, line))
    {
        std::cout << "Input: " << line << std::endl;
        // Now loop through the string to upper case the letters and '*' the digits.
        for(auto& ch : line)
        {
            // if the character is a digit change it to an '*'.
            if(isdigit(ch))
            {
                ch = '*';
            } // If the letter is lower case change it to upper case.
            else
            {
                ch = toupper(ch);
            } // If the letter is upper case change it to lower case.
        }
        // Now print the line to the output file.
        out << "Output: " << line << endl;

    }
}


Output:
1
2
Input: This is my String number 1.
Output: THIS IS MY STRING NUMBER *.
Last edited on
To address the error quoted in the OP the line being complained about is this:

out << toupper; toupper is a function. What does it mean to insert a function into an output stream? One usually calls a function and perhaps the resulting value would be appropriate to feed to an output stream.
when i compile that jlb it gives me this error
1
2
3
4
5
6
 In function ‘void get_line(std::ifstream&, std::ofstream&, std::__cxx1                                                                                                                                                             1::string, std::__cxx11::string)’:
lab10.cc:43:14: error: ISO C++ forbids declaration of ‘ch’ with no type [-fpermi                                                                                                                                                             ssive]
   for (auto& ch : line) {
              ^
lab10.cc:43:19: warning: range-based ‘for’ loops only available with -std=c++11                                                                                                                                                              or -std=gnu++11
   for (auto& ch : line) {

and thank you all for the help
Last edited on
So why aren't you using a C++11 compatible compiler?

To "fix" the issue use an old fashioned for() loop if you're unable to use a C++11 compiler.

1
2
3
4
5
6
7
for(size_t i = 0; i < line.length(); ++i)
{
    if(isdigit(line[i])
        line[i] = '*';
    else
        line[i] = toupper(line[i]);
}


But you really should be using a C++11 or higher compatible compiler. What compiler are you using, and what is the version of that compiler?

Topic archived. No new replies allowed.