-5

For my computer science class, we were told to try to read from a text document and have it replace every letter of C to C++

#include <iostream> // allows for cin and couts.
#include <fstream> // allows for the program to be able to read files.
using namespace std;

int main()
{
char str[1000000]; // used to store each letter.
int i = 0; // was supposed to be used as a counter to count the places of each letter within the array.
ifstream getExtraCredit(str); // opening file.

getExtraCredit.open("extracredit.txt");

char c;
if (!getExtraCredit) // If the read file isn't in the proper folder.
{
        cout << "Error opening data file. " << endl;    
}
else
{
    while (getExtraCredit.get(c))
    { 
        cout << c; // displays each character.
    }
}
getExtraCredit.close(); // ends file reading.
system("PAUSE");
return 0;

}

inside the extracredit.txt file is

"C is one of the world's most modern programming languages. There is no language as versatile as C, and C is fun to use."

I am very confused on how to do this. Several post on the site says to use a "replace" function, but it seems that only works with strings and my instructor's specific instructions were to keep it as a char array.

From my understanding, would it not make more sense to use string instead of char? What I am confused on is how would I do this with using chars. If I was using strings, my assumption on how it would be done would be having the project read the file and store it within string arrays. From there, the array would be read and if it contains C's, it would be replaced by a C++ string.

His instructors are

Create a program proj5_3.cpp that would read the content of an input file and write it back to an output file with all the letters "C" changed into "C++". The names of input/output files are entirely up to you. For example the following file content:

"C is one of the world's most modern programming languages. There is no language as versatile as C, and C is fun to use."

should be changed and saved into another file with the following content:

"C++ is one of the world's most modern programming languages. There is no language as versatile as C++, and C++ is fun to use."

To read one character from the file you should use the get() function:

ifstream inputFile; char next; ... inputFile.get(next);

first edit revision:

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

using namespace std;

int main()
{
string txt;
string temp;
ifstream getExtraCredit(txt); // opening file.

getExtraCredit.open("extracredit.txt");

if (!getExtraCredit)
{
        cout << "Error opening data file. " << endl;    
}
else
{
    { 
        getline(getExtraCredit, temp); // displays each character.
        txt.append(temp);
        replace(txt.begin(), txt.end(), 'c', 'c++');
        cout << txt;
    }
}
getExtraCredit.close();
system("PAUSE");
return 0;

}

I am now having issues with using the replace function. Any insight on what I am doing wrong?

Tony Do
  • 11
  • 4
  • 4
    *"i believe my instructor's specific instructions were to keep it as a char array"* Sigh. You may want to supplement your course with a good C++ book, stuff like that or `using namespace std;` are strong hints you are taught some questionable practices. – Baum mit Augen Jul 30 '17 at 23:37
  • 2
    So, you believe, or you know? If it's C++ course, why would you not want to learn about C++ features, instead of using C strings? – Algirdas Preidžius Jul 30 '17 at 23:37
  • https://stackoverflow.com/questions/1494399/how-do-i-search-find-and-replace-in-a-standard-string – Rivasa Jul 30 '17 at 23:38
  • It's not clear what you're asking. Is there a problem with the code, or does it do what you want but you're confused about your instructor's instructions? If you're simply confused about their instructions, you will need to ask them. If there is a problem with your code or output, you should [edit] your question to point out the issue and what you've tried. – Tas Jul 30 '17 at 23:41
  • I have edited the wording and questioning to make more sense. – Tony Do Jul 30 '17 at 23:50
  • The instructions don't appear to say anything about using a char array, so I would just go ahead and use `std::string` and then you can use the replace like you wanted to. Otherwise, use the `char[]`, read one character at a time and convert that into a `std::string`, then replace. – Tas Jul 31 '17 at 00:06
  • You're reading a char at a time. Check whether the character you read is "C" and respond appropriately. – Pete Becker Jul 31 '17 at 00:07
  • updated with my new code. Still confused on the return function. – Tony Do Jul 31 '17 at 00:33

1 Answers1

0

Correct me if I'm wrong, but I think it should be:

txt.replace(txt.begin(), txt.end(), 'c', 'c++');
Devin L.
  • 437
  • 2
  • 12