Any chance I could get some quick C++ help?

SilentPen

I'm a GIRL, damnit.
Jul 28, 2008
236
6
0
Minnesota
www.silentpen.com
So I'm taking a basic C++ class, and the programming assignment this week is to write a program that converts a telephone number in letters (example: GET-LOANS) to numbers. Only take 7 letters, ignore spaces, put a hyphen after the third digit on output.Most of my code is working right, but I've got something wrong, because it's not converting them right. What's the problem with my code? Might could trade a free banner or some other small design freebie in exchange for help, if ya want.

HTML:
#include <iostream>

using namespace std;

int main ()
{
    char doContinue, tempLetter;
    int tempNum, numOne, numTwo, numThree, numFour, numFive, numSix, numSeven;
    int numCount;
        
    numCount = 1;
    tempNum = 0;
    
    cout << "Enter Y/y to convert a telephone number from letters to digits." ;
    cout << endl;
    cout << "Enter any other letter to terminate the program." << endl;
    cin >> doContinue;
    
    while (doContinue == 'Y' || doContinue == 'y')
        {
          cout << endl << "Enter a telephone number using letters: ";
         
          while (numCount <= 7)
              {
                 cin >> tempLetter;
                                     
                      switch (tempLetter)                       
                         {
                            case 'A': 
                            case 'B': 
                            case 'C': 
                            case 'a': 
                            case 'b': 
                            case 'c':
                                tempNum = tempNum + 2;               
                                break;                            
                            case 'D': 
                            case 'E': 
                            case 'F':
                            case 'd': 
                            case 'e': 
                            case 'f':
                                tempNum = tempNum + 3;            
                                break;                         
                            case 'G': 
                            case 'H': 
                            case 'I':
                            case 'g': 
                            case 'h': 
                            case 'i':
                                tempNum = tempNum + 4;             
                                break;                            
                            case 'J': 
                            case 'K': 
                            case 'L':            
                            case 'j': 
                            case 'k': 
                            case 'l': 
                                tempNum = tempNum + 5;            
                                break;                          
                            case 'M': 
                            case 'N': 
                            case 'O':
                            case 'm': 
                            case 'n': 
                            case 'o':
                                tempNum = tempNum + 6;             
                                break;                           
                            case 'P': 
                            case 'Q': 
                            case 'R': 
                            case 'S': 
                            case 'p': 
                            case 'q': 
                            case 'r': 
                            case 's': 
                                tempNum = tempNum + 7;             
                                break;                            
                            case 'T': 
                            case 'U': 
                            case 'V':
                            case 't': 
                            case 'u': 
                            case 'v':
                                tempNum = tempNum + 8;             
                                break;                          
                            case 'W': 
                            case 'X': 
                            case 'Y':
                            case 'Z': 
                            case 'w': 
                            case 'x': 
                            case 'y': 
                            case 'z': 
                                tempNum = tempNum + 9;           
                                break;
                            case 1:
                                tempNum = tempNum + 1;
                                break;
                            case 2:
                                tempNum = tempNum + 2;
                                break;
                            case 3:
                                tempNum = tempNum + 3;
                                break;
                            case 4:
                                tempNum = tempNum + 4;
                                break;
                            case 5:
                                tempNum = tempNum + 5;
                                break;
                            case 6:
                                tempNum = tempNum + 6;
                                break;
                            case 7:
                                tempNum = tempNum + 7;
                                break;
                            case 8:
                                tempNum = tempNum + 8;
                                break;
                            case 9:
                                tempNum = tempNum + 9;
                                break;
                            case 0:
                                tempNum = tempNum - 1;
                                break;
                            default:
                                cout << "Invalid Entry. Please Try Again.";
                                break;
                    }
                    
                    if (numCount == 1)
                       numOne = tempNum;
                    else if (numCount == 2)
                         numTwo = tempNum;
                    else if (numCount == 3)
                         numThree = tempNum;
                    else if (numCount == 4)
                         numFour = tempNum;
                    else if (numCount == 5)
                         numFive = tempNum;
                    else if (numCount == 6)
                         numSix = tempNum;
                    else if (numCount == 7)
                         numSeven = tempNum;
                         
                    numCount = ++numCount;
               }    
                  
         cout << "The corresponding telephone number is:" << endl;         
         cout << numOne << numTwo << numThree << "-";
         cout << numFour << numFive << numSix << numSeven;
         cout << endl << endl;
               
         cout << "To process another telephone number, enter Y/y." << endl;
         cout << "Enter any other letter to terminate the program." << endl;
         cin >> doContinue;
         }
         
         return 0;
         
}
 


tempLetter is declared as a char; if you have switch(tempLetter) it's gonna check the cases for chars

So if you have in the switch(tempLetter)

case 'a':

it's gonna check if tempLetter contains a char with the char value of 'a' (int value of 97)

and if you have
case 1:

it's gonna check if tempLetter contains a char with the int value of 1
 
This is a really weird way of doing what you want to do, but to fix your problem, right after you have while (numCount <= 7) put tempNum = 0; as it requires resetting for each loop.