c++ - Class member function at() and switch statements with a loop -


this first time posting here, apologies in advance if i'm doing wrong.i'm working on project part of program needs translate phone word (ie "rad-code") corresponding phone digits (ie 723-2633). i'm trying use switch statement along at() , length() class member functions. i've tried switching order of code section, keeps giving me error stating: "enter phone word: rad-code 723-2633terminate called after throwing instance of 'std::out_of_range' what(): basic_string::at: __n (which 8) >= this->size() (which 8)"

here's code in question:

else if (choice == phone_word) {         cout << "\nenter phone word: ";         cin >> phoneword;         /*while (phoneword.length() != 8)         {             cout << "please enter valid phone number length: ";             getline(cin, phoneword);         }*/          (int = 0; < phoneword.length(); i++)         switch (phoneword.at(i))         {             case 'a':             case 'a':             case 'b':             case 'b':             case 'c':             case 'c':                 cout << "2";                 break;             case 'd':             case 'd':             case 'e':             case 'e':             case 'f':             case 'f':                 cout << "3";                 break;             case 'g':             case 'g':             case 'h':             case 'h':             case 'i':             case 'i':                 cout << "4";                 break;             case 'j':             case 'j':             case 'k':             case 'k':             case 'l':             case 'l':                 cout << "5";                 break;             case 'm':             case 'm':             case 'n':             case 'n':             case 'o':             case 'o':                 cout << "6";                 break;             case 'p':             case 'p':             case 'q':             case 'q':             case 'r':             case 'r':             case 's':             case 's':                 cout << "7";                 break;             case 't':             case 't':             case 'u':             case 'u':             case 'v':             case 'v':                 cout << "8";                 break;             case 'w':             case 'w':             case 'x':             case 'x':             case 'y':             case 'y':             case 'z':             case 'z':                 cout << "9";                 break;             case '-':                 cout << "-";                 break;             default:                 cout << "please enter valid input: ";                 break;         }          cout << phoneword << " translates " << phoneword.at(0) << phoneword.at(1) << phoneword.at(2) << phoneword.at(3) << phoneword.at(4) << phoneword.at(5) << phoneword.at(6) << phoneword.at(8) << "."; } 

this problem: phoneword.at(8). should `phoneword.at(7).

here's more concise way of achieving intent:

// digit_map static map of characters digits, 'a' -> 0, 'q' -> 7 etc. (auto = phoneword.cbegin(); != phoneword.cend(); ++it)    cout << digit_map[tolower(*it)]; 

no need use at, iterators for.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -