First of all, thanks to everyone who helps me, it is much appreciated!
I am trying to store a string with spaces and special characters intact into MessageToAdd
.
I am using getline (cin,MessageToAdd);
and I have also tried cin >> MessageToAdd;
.
I am so stumped! When I enter the sample input
Test
Everything works as intended. However if I were to use
Test Test Test
The whole console would just blink fast until I pressed CtrlC.
My style of putting variables at the top I've been told is obsolete. Please forgive me as I am still teaching myself and it's simply force of habit. I will be changing my style shortly after I get this solved :)
void AddMessage() {
ifstream myReadFile;
string str;
string MessageToAdd;
string myMessages[10];
int i; // of course my famous i
static string rowHtmlCloseTags;
static string rowHtmlOpenTags;
string replacement;
myReadFile.open("C:\\Users\\Andrews\\Documents\\Visual Studio 2010\\Projects\\computerclass\\Debug\\outages.htm",ios::in);
i = 0; //the start of my array
rowHtmlCloseTags = "</b></td>"; // value that I want to replace with nothing
rowHtmlOpenTags = "<td><b>";
if(!myReadFile) // is there any error?
{
cout << "Error opening the file! Aborting…\n";
exit(1);
}
if (myReadFile.is_open())
{
cout << endl;
while (!myReadFile.eof())
{
getline(myReadFile, str);
if (str == "<tr>")
{
getline(myReadFile, str); //get the next line cause thats where the <td><b>Outage Message</b></td> is.
size_t foundIndex = str.find(rowHtmlCloseTags); //does the sought string exist in this this line?
if (foundIndex != str.npos) //if not no position
str.replace(foundIndex, rowHtmlCloseTags.size(), replacement); //replace the string
else
std::cout << "Oops.. didn't find " << rowHtmlCloseTags << std::endl; //else throw a bitch
foundIndex = str.find(rowHtmlOpenTags); //does the sought string exist in this this line?
if (foundIndex != str.npos) //if not no position
str.replace(foundIndex, rowHtmlOpenTags.size(), replacement); //replace the string
else
std::cout << "Oops.. didn't find " << rowHtmlOpenTags << std::endl; //else throw a bitch
myMessages[i]=str;
i++;
}
}
}
system("cls");
i=0;
while (i < 10)
{
cout << i << ") " << myMessages[i] << endl;
i++;
if (myMessages[i]=="")
{
break;
}
}
myReadFile.close();
cout << endl;
cout << endl;
cout << "Enter the message you would like to see on the reader board.\n";
cout << "Or enter 911 to go back to the main menu: ";
cin.ignore(1080);
getline (cin,MessageToAdd);
if (str == "911") //go back to the main menu
{
system("cls");
mainMenu();
}
else //insert the message into a blank spot in the array
{
i=0;
while (i < 10)
{
if (myMessages[i].empty())
{
myMessages[i]=MessageToAdd;
break;
}
else
{
i++;
}
}
}
//now rebuild the htm file with the new array
CreateHtmlFile(myMessages);
}