A: 

I would change a number of things, but as a intro CS program, it's not too bad. A few things:

  1. Whenever EOF occurs, your cout is going to try to display that EOF and will probably not work correctly. You should at least put an if check on the cout after the while loop checking against cCharacter != EOF to prevent that.

  2. You have no default case. If an input occurs that you don't expect, you cout will display its intialized 0 or the previous entry for the number.

  3. To store each number and print them in sequnce, the easiest way would be to use a std::vector. These won't be taught for a while though, so don't worry about that too much. You could do it unsafely with a large array, but that isn't a good method as you surmised.

  4. You could also use an array table to replace your switch statement, but again that's probably a but further along than your class.

  5. I'm sure there's a getChar method for cin. This would grab each character as it's typed and would be much cleaner to use than a straight cin.

I hope this helps you a bit. Without further info from you on how the program is failing, I can only take educated guesses at what is wrong besides the above list.

Michael Dorgan
Thanks for your help. I ended up using what I think is a member function of the iostream header with this code: "while (!cin.eof())" You're right about the defualt case, which I'll probally take a hit on. As for the getchar method, I've seen that around on the web, but right now I'm more comfortable with the cin method, but as I progress I will probably start using it as well. Thanks for your reply and consideration.
Kula Dhad
A: 

The problem is that you're expecting a magic "EOF" character and you're not going to get it. The character to end the program needs to be something the user can actually enter. "x" and "0" have potential, as does a blank string (technically, you'd test for a newline in that case, i.e. '\n').

That said, I noticed a few oddities about your code...

First, why are you asking for input one character at a time? I would expect a phone number "translator" to take an entire phone number in "letter form" and turn it into a numeric number which I can dial; otherwise, it's nothing I can't do by just looking at a table, or, for that matter, the phone itself. Did your instructor say that you had to go about it this way?

Your loop is redundant; its conditions tell it to only run while cCharacter is not equal to EOF, but you also break when it's EOF using an if in the loop. Only one of these is necessary. A popular way to do it is to get the first character before the loop and at the very end of it, but you could also just keep the if-break section and change the while conditions to 1.

Why is cNumber a char? Admittedly it doesn't have any real effect on the execution of the program, but the types are provided for a reason; code is clearer and easier to maintain, as a general rule, if it's semantically correct (meaning, in this case, that you use char for characters and int for integers). It's meaningless for this program, but it's a good habit to get into.

As for storing the numbers in an array, yes, it's very possible. You can dynamically define an array of a given length using the calloc() function, or you can use vectors. However, both are probably well beyond the current scope of your class.

macamatic
A: 

The standard input can be made to emultate "End of file" by hitting a specific control sequence on the input device. This is OS specific though:

On Unix/Linux (and thus Mac OS-X)
Ctrl-D

On Windows
Ctrl-Z (I think).

Though the EOF character EOF is a hangover from C's file stream so it would be better to test the stream itself to see if it has reached the end of file.

 while (!cin.good()) {/* Stream still readable ie not EOF */}

The lucky thing is; if you use a stream in a boolean context it automatically converts itself into an object that is convertible to bool with a value of true if the stream is still usable or false if the string has gone into a bad state (like EOF).

 while (cin) { /* Stream converted to a bool that is true if stream is still readable*/ }

The problem here is that EOF is not actually set until after you try and read past the end of file. So the test here is done before you read. Now when you try and read it may fail so technically you need another test after the read:

while(cin)
{
    char x;
    cin >> x;
    if (cin)  { /* The read worked */ }
}

But the result of the operator>> returns a reference to a stream so we can do this all in one go:

char x;
while(cin>> x)   // read a character into x and return cin.
                 // cin then used as the test to see if the stream is OK
{
    /* cin is OK so the read into x must have worked. */
}
Martin York
A: 

Hey everyone,

Thanks for your replies and help. I am posting my final solution for anyone else who may have the same problem and comes across this page. If my research is correct the eof () is a macro (or member function) of the stdio.h header file and is a holdover from C programming. My final code is below along with a test case posted below in comments:

//Explanation of Program //This program converts inputted characters into numbers based on the telephone digit scheme

//**************************************Program begins here*********************
#include <iostream>        //Includes library file for input / output functionality
using namespace std;

int main()
{
    //Declaration of variables
    char cCharacter = 0;        // User inputed character
    char cNumber    = 0;        // Converted numbers from inputed characters

    //Beginning of main program
    cout << "This program converts inputted characters into numbers based on the telephone digit scheme";  //Explanation of program

      while (!cin.eof())            // While condition runs unless 'eof' key is hit
    {  
    cout << endl <<"Please hit '<ctrl> z' to exit, or enter a word on character at a time: ";
    cin >> cCharacter;      //User inputs a character value
    cCharacter = toupper (cCharacter);  // Converts lowercase values to uppercase

     //Switch case function to select numbers from characters inputted
    switch (cCharacter)                                                                               
    {
        case 'A':
        case 'B':
        case 'C': 
                        cNumber = '2';  // Letters A, B, or C converted to Number 2
        break;

        case 'D':
        case 'E':
        case 'F':
            cNumber = '3';  // Letters D, E, or F converted to Number 3
        break;

        case 'G':
        case 'H':
        case 'I':
            cNumber = '4';  // Letters G, H, or I converted to Number 4
        break;

        case 'J':
        case 'K':
        case 'L':
            cNumber = '5';  // Letters J, K, or L converted to Number 5
        break;

        case 'M':
        case 'N':
        case 'O':
            cNumber = '6';  // Letters M, N, or O converted to Number 6
        break;

        case 'P':
        case 'Q':
        case 'R':
        case 'S':
            cNumber = '7';  // Letters P, Q, R, or S converted to Number 7
        break;

        case 'T':
        case 'U':
        case 'V':
            cNumber = '8';  // Letters T, U, or V converted to Number 8
        break;

        case 'W':
        case 'X':
        case 'Y':
        case 'Z':
            cNumber = '9';  // Letters W, X, Y, or Z converted to Number 9
        break;

        // defualt adds newline
        default:
            cout <<endl;
        break;

    }
    //Output of character inputted and number associated with it
    cout << "The number associated with the letter "<<cCharacter <<" is: "<<cNumber <<endl;
}//End while loop
return 0;       //indicates program success 
} //End Main () function
/* Testing of variables

    Test 1 - TOAST ******************************************************************************************

    This program converts inputted characters into numbers based on the telephone di
    git scheme
    Please hit '<ctrl> z' to exit, or enter a word on character at a time: t
    The number associated with the letter T is: 8

    Please hit '<ctrl> z' to exit, or enter a word on character at a time: o
    The number associated with the letter O is: 6

    Please hit '<ctrl> z' to exit, or enter a word on character at a time: a
    The number associated with the letter A is: 2

    Please hit '<ctrl> z' to exit, or enter a word on character at a time: s
    The number associated with the letter S is: 7

    Please hit '<ctrl> z' to exit, or enter a word on character at a time: t
    The number associated with the letter T is: 8

    Please hit '<ctrl> z' to exit, or enter a word on character at a time: ^Z
    The number associated with the letter T is: 8
    Press any key to continue . . .

    TOAST
    86278

The only thing I would have liked to do in addition to my solution was place the numbers and characters into an array (or two arrays perhaps) and display them in sequence, but this was beyond the scope of my problem and after fighting both with this problem and my other lab for this chapter I was happy to have them working properly and turned in.

Again, thanks to everyone for their input and consideration.

Kula Dhad