getline C ++ o cin no aceptar una cadena con espacios, he buscado Google y todavía estoy perplejo!

StackOverflow https://stackoverflow.com/questions/3146351

  •  01-10-2019
  •  | 
  •  

Pregunta

En primer lugar, gracias a todos los que me ayuda, es muy apreciada!

Estoy intentando almacenar una cadena con espacios y caracteres especiales intactas en MessageToAdd.

Estoy utilizando getline (cin,MessageToAdd); y también he intentado cin >> MessageToAdd;.

Estoy tan perplejo! Cuando entro en la entrada de la muestra

  

Prueba

Todo funciona según lo previsto. Sin embargo, si tuviera que utilizar

  

Prueba Prueba Prueba

Toda la consola apenas parpadeaba hasta que yo presioné Ctrl C .

Mi estilo de poner las variables en la parte superior que me han dicho es obsoleto. Por favor, perdóname ya que todavía estoy enseñando a mí mismo y es simplemente la fuerza de la costumbre. Voy a cambiar mi estilo poco después de obtener esta resuelto:)

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);
}
¿Fue útil?

Solución

Me voy a decir una cosa que es inmediatamente mal con su código, no su problema específico, sino un peludo, no obstante.

Estoy suponiendo que su función mainMenu() está llamando a éste. En ese caso, usted parece ser bajo la idea errónea de que:

if (str == "911") //go back to the main menu
{
       system("cls");
       mainMenu();
}

volverá a su menú. Será no hacer eso. Lo que hará es llamar a su código de menú principal de nuevo y finalmente se quedará sin espacio de pila.

Sospecho que lo que debe hacer es tener un bucle en mainMenu() y que el código de seguridad solo debería utilizar return; en lugar de llamar mainMenu() de forma recursiva.

Eso y el hecho de que creo que debería estar comparando MessageToAdd contra "911" en lugar de str.


Otra cosa que haría sería poner algo de código de depuración temporal en:

cout << "DEBUG A\n";
i=0;
while (i < 10)
{
    cout << "DEBUG B " << i << "\n";
    if (myMessages[i].empty())
    {
        cout << "DEBUG C\n";
        myMessages[i]=MessageToAdd;
        break;
    }
    else
    {
        i++;
        cout << "DEBUG D " << i << "\n";
    }
    cout << "DEBUG E\n";
}
cout << "DEBUG F\n";

y ver lo que se imprime. Por supuesto, se puede rastrear la ejecución en un depurador, pero que requeriría que usted pueda hacer el trabajo usted mismo. Si acaba de publicar la salida (primeras 100 líneas si es enorme), entonces podemos probablemente le dirá lo que está mal con facilidad.


En realidad, creo que tu problema es la cin.ignore. Cuando ejecuto el código, nada funciona, ni tampoco Test Test Test Test. Eso es porque está haciendo caso omiso de los primeros 1080 caracteres que estoy tratando de entrada. La prueba puede ser visto cuando se cambia a esas declaraciones:

cin.ignore(1);
getline (cin,MessageToAdd);
cout << MessageToAdd << "\n";

y se obtiene la salida est cuando se introduce test.

Tome la línea ignore y vuelve a intentarlo. No estoy seguro de esto ya que parece indicar que las obras Test pero no puedo ver esto como correcto.


Así que esto es lo que hay que hacer (en un mínimo):

  • deshacerse de la cin.ignore por completo.
  • return uso en lugar de mainMenu().
  • if (MessageToAdd == "911") uso en lugar de if (str == "911").
  • háganos saber cómo va a continuación.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top