fork(1) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main(){
  7. string input, output, mapin ="abcdefghijklmnopqrstuvwxyz",
  8. mapout="22233344455566677778889999";
  9. cout << "Type 'stop' to stop or type your 7 letter message to be converted to numbers: " << endl;
  10.  
  11. bool input_ok=true;
  12. while (cin >> input) {
  13. for (auto &c: input)
  14. c = tolower(c); // lowercase, just to simplify
  15. if (input=="stop" || output.size()==7)
  16. break; // reasons to stop reading
  17.  
  18. for (auto &c:input) {
  19. auto pos=mapin.find(c);
  20. if (pos==string::npos) {
  21. input_ok=false;
  22. output += '?';
  23. }
  24. else output += mapout[pos];
  25. if (output.size()==7)
  26. break;
  27. }
  28. }
  29. if (input_ok) {
  30. cout << output.substr(0,3) ;
  31. if (output.size()>3)
  32. cout << "-" << output.substr(3,7) ;
  33. cout << endl << endl;
  34. }
  35. else {
  36. cout << "incorrect input" << endl <<output<<endl;
  37. }
  38. return 0;
  39. }
Success #stdin #stdout 0s 4856KB
stdin
I am content  stop
stdout
Type 'stop' to stop or type your 7 letter message to be converted to numbers: 
426-2668