yeah sorry i shouldve made it clearer, so i search 1 word, replace ALL the instances of that word with another words. Im sorry, I am counting the words and characters for the finished file.
EDIT: runs great! so now i just need to save the finished one into a file to replace the original?
If that is what you need to do, you have the buffer set up and its just iterate through the container writing the line out to a file. I am not sure if you want to overwrite your original until you are making sure you are getting the expected results to the file.
I hope you understand my logic I used in that code situation. I didn't code everything but I tried to give you the logic you were working toward.
I glad to hear it runs great. I try to put up code that should work to some extent after the typos are removed.
ALMOST THERE, an early thanks to everyone for your help!!!! You all gave me the guidance and motivation i needed to get this thing done, im very grateful!!!
so what i have is a program that inputs a file, searches for a word, replaces all instances of it, counts the words, and character, but i have an ofstream outputting the results as a separate file. How do I overwrite the original one?
Make sure you close it from the original passes after the first main loop that does the replacements and counting, which has the eof at the top. Reopening it for overwrite should replace it, I think those are in the flags of c++. You might have to delete the original but you should be fine with the overwrite flag if it exists.
ok, got that issue solved, but checking the specifics i found 2 more problems. 1. character counting also counts the spaces
2. if there is a new line (and therefore no space) that last word isnt counted
1 2 3 4
{
if(*pos == ' ') words++;
else characters++;
}
this is an example
and just a test
it tells me 33 characters, and 6 words
there are actually 27 characters, and 8 words.
Ive even changed the if statement to if((*pos == ' ')||(*pos == '\n')) in an attempt to fix the word count, but it still gives me 6
Remember The count of words is zeroed out, not set to one to assume the count of the first word. It isn't counting the last word when the loops end or exits. Its math is correct given those two cases aren't accounted for. When the loop ends it hits eof not a new line marker or space.
1 2
// your open of the file to write should look like
out_stream.open ("done.txt", ios::out|ios::trunc);
After I thought about it a little bit, I will try to explain what is happening.
Reset the word count to 0 not 1.
Make the following changes
1 2 3 4 5 6 7 8 9 10 11 12
#include <cctype> //might be needed to be added to the includes at the start of the program
// in this loop I was reading up how to do something similar
// the following changes might apply...
for(str.begin(); str.end(); ++pos)
{
if(isalpha(*pos))
characters++
else
word++
}
The count is wrong because of the end of that loop isn't hitting a new line or space. It terminates without counting the last word, so two lines would see a two word deficit and 3 lines would see a 3 word deficit. I don't know if the current version I have there would fix that or not. It was something I was reading about ++pos and pos++ differences in stl containers and what pos is actually pointing too when we hit the end of a run.
I am trying to teach myself C++ and thought this was a pretty cool beginner program. I am trying to see if I can make it work.
When I try to compile I get the following error on line 54:
C:\Users\Administrator\Desktop\wordswap\main.cpp|54|error: could not convert 'str.std::basic_string<_CharT, _Traits, _Alloc>::end [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]()' to 'bool'|
this is the code around line 54:
// in this loop I was reading up how to do something similar
// the following changes might apply...
for(str.begin(); str.end(); ++pos) <- line 54
{
if(isalpha(*pos))
characters++
else
word++
}
also if I remove the above section, it asks me for the text file and the word to search for, the word to replace it with, and then it goes into an infinite loop of couting "found in line X at index X"
finally from what I understand by looking at the code, it is finding the word but it isnt actually replacing it. the variable replace is only used when the user is typing what to replace, but i dont see the variable again on the part where the program finds the original word, so how does it know what to replace with?
i think i understand it, but i dont quite get everything, if someone could clarify or post the code that works without the errors that would be cool
sorry if im too noobish, my highschool doesnt offer these classes so im trying to learn on my own.
hmm well ive been working on this in my free time and i have the word find working as well as the word count and character count, but the word replace is still giving me trouble.
Sonicrobby, it seems you got it working, do you or anyone else have any tips for getting the replace part to work?
EDIT: Woohoo got it all working 100% and even added in some improvements like menu , saving the file, reverting to previous save, etc.