Encoding Program

Hello Everyone,

Im new to the board but use it quite a bit for reference. I am attempting to write a program that reads an input file, converts each line to a c-string, adds the value of 4 to each character and then outputs it into a new .txt file called Encoded. I seem to be getting a compiler error and am stuck on how to correct it.

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{


int index;
string line;


//Open the file
ifstream inputFile;
inputFile.open("plain.txt");

//Create output file
ofstream outputFile;
outputFile.open("Encoded.txt");



while (inputFile.is_open())
{
for (index = 0; index < strlen(line); index++)
{
line[index] = +4;
outputFile << line[index];
getline(inputFile, line);
}
}
inputFile.close();
outputFile.close();

return 0;

}
line(27): error C2664: 'strlen' : cannot convert parameter 1 from 'std::string' to 'const char *'

The function strlen expects a const char *, but you pass it a std::string. It's better to use line.length().

Also you should read sth into line before you use it.
for (index = 0; index < strlen(line); index++)

Thomas,
After changing my strlen to line.length and moving my getline to be read before passing to the for loop I got a lnk1120 and 2019 error.
The text that accompanies those errors would be useful.
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>

using namespace std;

int main()
{


int index;
string line;

//Open the file
ifstream inputFile;
inputFile.open("plain.txt");

//Create output file
ofstream outputFile;
outputFile.open("Encoded.txt");



while (inputFile.is_open())
{
getline(inputFile, line);

for (index = 0; index < line.length(); index++)
{
line[index] = +4;
outputFile << line[index];
}
}
inputFile.close();
outputFile.close();

return 0;

}
And by the text that accompanies those errors, I mean the actual text generated by the compiler that accompanies the error numbers you have given us, not the abbreviated version you've supplied.
cire sorry misread what was asked. The LNK 1120 is an unresolved external and LNK2019 unresolved external symbol_WinMain@16 reference in function__tmainCRTStartup
You chose to create a windows project.

Sounds like you're using VS so, go to your project properties and navigate to:

Configuration Properties -> Linker -> System

From there you can set the SubSystem to Console (/SUBSYSTEM:CONSOLE) or simply unset the SubSystem. Don't forget to hit the Apply button.
Last edited on
Cire,

That helped resolve the linker errors. Thanks! The other issue Im having now is that nothing is wiring to my output file. When I go to open it in notepad it appears to be blank and then goes into not responding and nothing shows up.
Ok, Im making progress.....I got one line to display and stop after that. I guess my next step is to figure out to read all 8 lines.

//Lab 10 -- Writing an encrypted Message by adding the value of ASCII by 4
//By Adam Ruggeberg

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
int index;
string line;

//Open the file
ifstream inputFile;
inputFile.open("plain.txt");

//Create output file
ofstream outputFile;
outputFile.open("Encoded.txt");



while (inputFile.is_open())
{
getline(inputFile, line);

for (index = 0; index < line.length(); index++)
{
line[index] += 4;
outputFile << line[index];
}
inputFile.close();
}

outputFile.close();

return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
    while (inputFile.is_open())
    while (getline(inputFile, line))
    {
        getline(inputFile, line);

        for (index = 0; index < line.length(); index++)
        {
            line[index] += 4;
            outputFile << line[index];
        }
        inputFile.close();
    }


Don't close the input file before you're done reading. Loop on input extraction. Your code strips newlines from the input but doesn't add them to the output. I'll let you figure that out.
Cire,

I greatly appreciate your helping me here Im still learning. I got the last part figured out!

Thanks again everyone.
Topic archived. No new replies allowed.