You are here:

C++/C++ HW help

Advertisement


Question
I have to do a program that converts telephone numbers into letters. If the user enters more than seven letters, then process only the first seven letters. My program compiles and works somewhat properly but it is processing more than 7 numbers. I tried to use a counter but I cannot figure out how to stop it from processing numbers past 7. My code is below. Please help me.

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
  char letter;                          //Line 1

  cout << "Program to convert "
      << "letters to their corresponding "
      << "telephone digits." << endl;        //Line 2

 do {
  cout << "To stop the program enter #."
      << endl;                         //Line 3

  cout << "Enter letters used for a phone number and press Enter:";               //Line 4
  cin >> letter;                        //Line 5

  // WE USE THIS TO ITERATE THROUGH THE CODE
  int counter = 0;
  while (letter != '\n')
  {
     if (counter == 3 && counter != 0)
        cout << "-";
     if ( letter == '#')  break;
     if ((letter >= 'A' && letter <= 'Z')      //Line 10
       || (letter >= 'a' && letter <= 'z' ) )
        switch (letter)                  //Line 11
        {
        case 'A':
        case 'B':
        case 'C':
        case 'a':
        case 'b':
        case 'c':
        cout << "2";            //Line 12
        break;      
                          //Line 13
        case 'D':
        case 'E':
        case 'F':
        case 'd':
        case 'e':
        case 'f':
        cout << "3";           //Line 14
        break;      
                          //Line 15
        case 'G':
        case 'H':
        case 'I':
        case 'g':
        case 'h':
        case 'i':
        cout << "4";           //Line 16
        break;   
                             //Line 17
        case 'J':
        case 'K':
        case 'L':
        case 'j':
        case 'k':
        case 'l':
        cout << "5";           //Line 18
        break;   
                             //Line 19
        case 'M':
        case 'N':
        case 'O':
        case 'm':
        case 'n':
        case 'o':
        cout << "6";           //Line 20
        break;   
                             //Line 21
        case 'P':
        case 'Q':
        case 'R':
        case 'S':
        case 'p':
        case 'q':
        case 'r':
        case 's':
        cout << "7";           //Line 22
        break;   
                             //Line 23
        case 'T':
        case 'U':
        case 'V':
        case 't':
        case 'u':
        case 'v':
        cout << "8";           //Line 24
        break;
                                //Line 25
        case 'W':
        case 'X':
        case 'Y':
        case 'Z':
        case 'w':
        case 'x':
        case 'y':
        case 'z':
        cout << "9";           //Line 26
           
        case ' ':
        case '\t':
          break;
     }  // end of switch statement
     else                          //Line 27
        cout << "Invalid input." << endl;    //Line 28

     cin >> letter;                     //Line 32
     counter ++; // INCREMENT OUR COUNTER
    }  //end of while loop for letters
    cout << endl;

  } while (letter != '#');//no more input

  system ("Pause");
  return 0;
}


Answer
Change the while statement from

      while (letter != '\n') to

      while( counter < 7 )

and you should be ok.

Note:

#include <stdlib.h>

There is no such header in ISO C++. Use #include <cstdlib> instead.   

system("pause") represents a vulnerability (an unsafe programming practice) which can be exploited by an attacker. A malicious attacker could replace the "pause" program on the machine with a program that does some kind of damage, and use it to cause trouble. Spoofing a legitimate and harmless program is a common practice among malware.

In addition, it also has the drawback that the argument string is not portable across different implementations. Your code may work on windows, but may do something completely different (or not work at all) on unix.

A portable and less risky way is to write:

 std::cin >> std::ws ; // throw away white spaces remaining in the input buffer
 std::cin.get() ; // wait for the user to enter a new line  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++09. i would not answer questions about gui and web programming.

Experience

over 15 years

Education/Credentials
post graduate engineer

©2012 About.com, a part of The New York Times Company. All rights reserved.