-3

It might be because I don't pass the values correctly.Although the code is running.

#include <iostream>
#include <fstream>
#include <stream>
using namespace std;
    
  
int main()
{
    //Files
    ifstream Myfile;
    Myfile.open("Myfile.dat");
    ofstream ReverseEx;
    ReverseEx.open("ReverseEx.txt");
    string str;
    //Getting the text from Myfile.dat and then passing it to Reverse.txt
    while (Myfile>>str)
    {
        Myfile >> str;
        reverse(str.begin(), str.end());
        cout << "\n";
        ReverseEx << str;
        
    }

    return 0;
}
    
//Input
//Hello everybody!!!
//STRINGS ARE COOL.
//Output
//!!! ydobyreve olleH
//.LOOC ERA SGNIRTS

The result should be like this example. But instead i got this

//!!!ydobyreveERA.LOOC
Coder777
  • 59
  • 5
  • alright let me try fixing it – Coder777 Feb 24 '21 at 21:22
  • so I tried it this way but the result is different from what I expected. – Coder777 Feb 24 '21 at 21:36
  • 2
    Does this answer your question? [How do you reverse a string in place in C or C++?](https://stackoverflow.com/questions/198199/how-do-you-reverse-a-string-in-place-in-c-or-c) – knowads Feb 24 '21 at 22:08
  • @Ted, where do you see `j` or `255`? Looks like it's been edited to remove the one piece of code that would be used to help resolve the problem. – WaitingForGuacamole Feb 25 '21 at 12:59
  • @WaitingForGuacamole Indeed. In the original question OP had a user defined `reverse` function with the `j` in it and also used `while(! eof())` ... but both were fixed. I'll remove both my comments. – Ted Lyngmo Feb 25 '21 at 13:00
  • Nah, you're good, @Ted. I'm just SMH at how someone asking a question would actually REMOVE information to help answer it. – WaitingForGuacamole Feb 25 '21 at 13:05

1 Answers1

1

You need to read line by line (using std::getline) instead of word by word to get the result you expect. Your current loop also reads two words and just throws one of them away.

while(std::getline(Myfile, str)) {
    reverse(str.begin(), str.end());
    ReverseEx << str << '\n';
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108