Converting letters to numbers

I'm new to C++ and one of my first assignments is to write a program that ask for a telephone number entered in letters and convert it to numbers. Below is my code. I seem to be stuck in an infinite loop and it is displaying all numbers 1-8 and then all 8's. I've worked on this for a few days, but can't figure it out. I also have to have a hyphen after the 3rd number. Any guidance is appreciated. Thanks.

#include "stdafx.h"
#include<iostream>

using namespace std;


int main()
{
char letter; // Letters entered
int num = 1;
char again;


cout << "Enter a telephone number using letters: "<< endl;
cin >> letter;

while (num <= 7)
{
cout << num;
num++;
}

while (letter)
cout << num;

switch (letter)
{
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;
}
//default: cout << "this code works" << endl;
// while (count < 8)

// if (counter == 3)
// {
// cout << "-";
// }


return 0;


}
Well, your main problem is the "while(letter)" which will always evaluate to true (since there is always at least something inside it)...Also, for your first while loop, I would suggest using a for loop, it is made to the type of thing you are trying to do (loop while a variable is a certain condition and do something to the variable each loop.) And next time when you post code, put [code][code] tags around it. :)
Topic archived. No new replies allowed.