c++ - Why are two while() loops causing a crash? -


    #include <iostream>     #include <string>      int main(){      char p;     bool show_cleartext;     std::string input;     std::cout << "would output show original text? [y/n] \n";     while(true){         std::cin >> input;         if(input == "y"){show_cleartext = true; break;}         else if(input == "n"){show_cleartext = false; break;}         else std::cout << "[y/n] ? \n";     }     while(true){         // promts 'input' displays input, reversed         std::getline(std::cin,input);         if(input == "quit")break;         for(int = 0; < (input.length()-1)/2; ++i){             p = input[input.length()-i-1];             input[input.length()-i-1] = input[i];             input[i] = p;         }         std::cout << input+'\n';     }      return 0; } 

each of while loops fine. including both of them causes program crash once proceeds past first 1 i.e. entering 'y' or 'n'.

when ask user if input "quit", input size becomes "" or 0. input empty string , when input.length() , equals 0 wraps around huge huge number , causes crash. got debugger , recommend use 1 too.

to fix remove std::getline(std::cin,input);

that resets input size.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -