title: Input 7 letter word, output 7 digit phone number
body: <p>I am writing code for an assignment. I input 7 letters and it outputs the corresponding digits on a numpad. So far it converts the letters just fine, it ignores everything past the 7th letter which is what I want and stops when you type stop. However, if I include spaces (which is required for the assignment) it throws an error, I wrote something that is supposed to make it ignore them but its not working. Also, if I input anything shorter than 7 letters I also get an error. &quot;terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: _n (which is 1) &gt;= this-&gt;size() (which is 1) Aborted (core dumped)</p>
  <p>I'm pretty sure I get that it is because of the while loop, in the debugger, it always stops there but i don't know how to get around this. I look forward to hearing from anyone. Thank you in advance!</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  int main(){
      //declares variables
      string str1, str2;
      int i;
      //first input
      cout &lt;&lt; &quot;Type 'stop' to stop or type your 7 letter message to be converted to numbers: &quot; &lt;&lt; endl;
      cin &gt;&gt; str1;
      
      //while loop so that you can input multiple numbers one after the other until stop is read
      while (str1 != &quot;stop&quot;){
          //resets i if str1 does not equal stop
          i=0;
          while(str2.length() != 7){
              //determines what number to assign to the charcter at the index of the i variable
              if (str1.at(i) == 'a' || str1.at(i) == 'A' || str1.at(i) == 'b' || str1.at(i) == 'B' || str1.at(i) == 'c' || str1.at(i) == 'C'){
                  str2 = str2 + '2';
                  i=i+1;
              }else if (str1.at(i) == 'd' || str1.at(i) == 'D' || str1.at(i) == 'e' || str1.at(i) == 'E' || str1.at(i) == 'f' || str1.at(i) == 'F'){
                  str2 = str2 + '3';
                  i=i+1;
              }else if (str1.at(i) == 'g' || str1.at(i) == 'G' || str1.at(i) == 'h' || str1.at(i) == 'H' || str1.at(i) == 'i' || str1.at(i) == 'I'){
                  str2 = str2 + '4';
                  i=i+1;
              }else if (str1.at(i) == 'j' || str1.at(i) == 'J' || str1.at(i) == 'k' || str1.at(i) == 'K' || str1.at(i) == 'l' || str1.at(i) == 'L'){
                  str2 = str2 + '5';
                  i=i+1;
              }else if (str1.at(i) == 'm' || str1.at(i) == 'M' || str1.at(i) == 'n' || str1.at(i) == 'N' || str1.at(i) == 'o' || str1.at(i) == 'O'){
                  str2 = str2 + '6';
                  i=i+1;
              }else if (str1.at(i) == 'p' || str1.at(i) == 'P' || str1.at(i) == 'q' || str1.at(i) == 'Q' || str1.at(i) == 'r' || str1.at(i) == 'R'|| str1.at(i) == 's' || str1.at(i) == 'S'){
                  str2 = str2 + '7';
                  i=i+1;
              }else if (str1.at(i) == 't' || str1.at(i) == 'T' || str1.at(i) == 'u' || str1.at(i) == 'U' || str1.at(i) == 'v' || str1.at(i) == 'V'){
                  str2 = str2 + '8';
                  i=i+1;
              }else if (str1.at(i) == 'w' || str1.at(i) == 'W' || str1.at(i) == 'x' || str1.at(i) == 'X' || str1.at(i) == 'y' || str1.at(i) == 'Y'|| str1.at(i) == 'z' || str1.at(i) == 'Z'){
                  str2 = str2 + '9';
                  i=i+1;
              }else if(str1.at(i) == ' '){
                  //if there is a space, it is ignored
                  i=i+1;
              }else{
                  //in case they put somthing weird, sets str2 to length of 7 to end while
                  cout &lt;&lt; &quot;incorrect input&quot;;
                  str2 = &quot;abcdefg&quot;;
              }
          }
          //outputs converted number
          cout &lt;&lt; str2.substr(0,3) &lt;&lt; &quot;-&quot; &lt;&lt; str2.substr(3,7) &lt;&lt; endl &lt;&lt; endl;
          cout &lt;&lt; &quot;Type 'stop' to stop or type your 7 letter message to be converted to numbers: &quot; &lt;&lt; endl;
          cin &gt;&gt; str1;
          //resets str2
          str2 = &quot;&quot;;
          
      }
      return 0;
  }
  </code></pre>