Hey guys there seems to be an error with my code.
My main problem is with the section where I want to change any occurrence of 'p' followed by a 'h'. In my testing it changes it to a 'q' for some reason, when I want it to change to 'f'.
For convenience the method with the problem is the void change(), in the second for loop.
Can someone please help?
#include <iostream>
#include <list>
#include <ctype.h>
#include <fstream>
using namespace std;
void printList(const list<char> &myList);
void fillList(list<char> &myList);
void change(list <char> &myList);
void printList(const list<char> &myList)
{
list<char>::const_iterator itr;
for (itr = myList.begin(); itr != myList.end(); itr++ ) {
cout <<*itr;
}
cout << '\n' << endl;
}
void fillList(list<char> &myList)
{
ifstream file("test.txt");
string print;
while(file >> print){
for (int i = 0; i<print.length(); i++) {
myList.push_back(print[i]);
}
myList.push_back(' ');
}
}
void change(list <char> &myList)
{
list<char>::iterator itr;
//rules are as follows
for (itr = myList.begin(); itr != myList.end(); itr++ ) {
if (*itr == 'w' ){
*itr = 'v';
}
}
for (itr = myList.begin(); itr != myList.end(); itr++ ) {
if((*itr == 'p' && ++*itr == 'h')){// rule incomplete ask!
*itr = 'f';
}
}
}
int main ()
{
list<char> myList;
ifstream file("test.txt");
const string print;
fillList(myList);
printList(myList);
change(myList);
printList(myList);
return 0;
}