KoolKassia
KoolKassia

Reputation: 11

What function do I use to store a single character in a cin statement so that I can process each character individually?

I am working on a program that is suppose to ask the user to enter a word and then translate it in CIAO. However, when my program compiles, it only processes the first letter into my switch statement the number of times the word's length is. What function do I use?

#include<iostream>
using namespace std;

int main()                                                                  
{                                                                           
    //Identify Variables                                                    
    string word;                                                            
    int counter;                                                            
    char letters, word1, word3;                                             

    //Ask user to input a word to be translated into I.C.A.O                
    cout<<" Enter a word: ";                                                
    cin>>word;                                                              

    counter = word.length(); //Counts the number of characters the user inputed
    word1 = word[0]; //Converts a string to a char value                 

    //Use a while statement to repeat until there are zero letters to process
    while(counter != 0)                                                     
    {                                                                       
        //Use a switch statement to translate each letter into I.C.A.O      
        switch(word1)                                                       
        {                                                                   
            case 'a': case 'A': cout<<"Alpha\n"; break;                     
            case 'b': case 'B': cout<<"Bravo\n"; break;                     
            case 'c': case 'C': cout<<"Charlie\n"; break;                   
            case 'd': case 'D': cout<<"Delta\n"; break;                     
            case 'e': case 'E': cout<<"Echo\n"; break;                      
            case 'f': case 'F': cout<<"Foxtrot\n"; break;                   
            case 'g': case 'G': cout<<"Golf\n"; break;                      
            case 'h': case 'H': cout<<"Hotel\n"; break;                     
            case 'i': case 'I': cout<<"India\n"; break;                     
            case 'j': case 'J': cout<<"Juliet\n"; break;                    
            case 'k': case 'K': cout<<"Kilo\n"; break;                      
            case 'l': case 'L': cout<<"Lima\n"; break;                      
            case 'm': case 'M': cout<<"Mike\n"; break;                      
            case 'n': case 'N': cout<<"November\n"; break;                  
            case 'o': case 'O': cout<<"Oscar\n"; break;                     
            case 'p': case 'P': cout<<"Papa\n"; break;                      
            case 'q': case 'Q': cout<<"Quebec\n"; break;                    
            case 'r': case 'R': cout<<"Romeo\n"; break;                     
            case 's': case 'S': cout<<"Sierra\n"; break;                    
            case 't': case 'T': cout<<"Tango\n"; break;                     
            case 'u': case 'U': cout<<"Uniform\n"; break;                   
            case 'v': case 'V': cout<<"Victor\n"; break;                    
            case 'w': case 'W': cout<<"Whiskey\n"; break;                   
            case 'x': case 'X': cout<<"X-Ray\n"; break;                     
            case 'y': case 'Y': cout<<"Yankee\n"; break;                    
            case 'z': case 'Z': cout<<"Zulu\n"; break;                      
        }                                                                   

        counter--;                                                          
    }                                                                       

    return 0;                                                               
}

Upvotes: 1

Views: 72

Answers (2)

David C. Rankin
David C. Rankin

Reputation: 84531

If you are truly limited to nothing but #include <iostream>, then you cannot use string (requires #include <string>) and you are stuck using the good old character array. In this case that isn't a draw-back, it's actually easier.

You don't want to use magic-numbers so declare a constant of sufficient size to for sizing your array to allow you to read words without running out of space. The longest word in the non-medical Unabridged Dictionary is 29-characters (so +1 for the nul-terminating character would require 30-characters total). Since you Never Skimp On Buffer Size!, 256 will suffice, e.g.

#define MAXC 256
...
    char word[MAXC];

Now all you do is read from stdin into word with your cin >> word. However, you really should Validate the read succeeds and the user didn't cancel with Ctrl+d on Linux (or Ctrl+z on windoze), e.g.

    //Ask user to input a word to be translated into I.C.A.O
    cout << " Enter a word: ";
    if (!(cin >> word)) {
        cerr << "(user canceled input)\n";
        return 1;
    }

Now just loop over the characters in word. You can use a pointer or a for loop and array indexing, e.g.

    for (i = 0; word[i]; i++)
    {
        //Use a switch statement to translate each letter into I.C.A.O
        switch (word[i])
        {
            case 'a': case 'A': cout<<"Alpha\n"; break;
            case 'b': case 'B': cout<<"Bravo\n"; break;
            case 'c': case 'C': cout<<"Charlie\n"; break;
            ...

Putting it altogether, you could do:

#include<iostream>

#define MAXC 256

using namespace std;

int main (void)
{
    //Identify Variables
    char word[MAXC];
    int i;

    //Ask user to input a word to be translated into I.C.A.O
    cout << " Enter a word: ";
    if (!(cin >> word)) {
        cerr << "(user canceled input)\n";
        return 1;
    }

    /* Use a for loop to iterate over each character in word */
    for (i = 0; word[i]; i++)
    {
        //Use a switch statement to translate each letter into I.C.A.O
        switch (word[i])
        {
            case 'a': case 'A': cout<<"Alpha\n"; break;
            case 'b': case 'B': cout<<"Bravo\n"; break;
            case 'c': case 'C': cout<<"Charlie\n"; break;
            case 'd': case 'D': cout<<"Delta\n"; break;
            case 'e': case 'E': cout<<"Echo\n"; break;
            case 'f': case 'F': cout<<"Foxtrot\n"; break;
            case 'g': case 'G': cout<<"Golf\n"; break;
            case 'h': case 'H': cout<<"Hotel\n"; break;
            case 'i': case 'I': cout<<"India\n"; break;
            case 'j': case 'J': cout<<"Juliet\n"; break;
            case 'k': case 'K': cout<<"Kilo\n"; break;
            case 'l': case 'L': cout<<"Lima\n"; break;
            case 'm': case 'M': cout<<"Mike\n"; break;
            case 'n': case 'N': cout<<"November\n"; break;
            case 'o': case 'O': cout<<"Oscar\n"; break;
            case 'p': case 'P': cout<<"Papa\n"; break;
            case 'q': case 'Q': cout<<"Quebec\n"; break;
            case 'r': case 'R': cout<<"Romeo\n"; break;
            case 's': case 'S': cout<<"Sierra\n"; break;
            case 't': case 'T': cout<<"Tango\n"; break;
            case 'u': case 'U': cout<<"Uniform\n"; break;
            case 'v': case 'V': cout<<"Victor\n"; break;
            case 'w': case 'W': cout<<"Whiskey\n"; break;
            case 'x': case 'X': cout<<"X-Ray\n"; break;
            case 'y': case 'Y': cout<<"Yankee\n"; break;
            case 'z': case 'Z': cout<<"Zulu\n"; break;
        }
    }

    return 0;
}

Example Use/Output

$ ./bin/char_switch
 Enter a word: abcdefg
Alpha
Bravo
Charlie
Delta
Echo
Foxtrot
Golf

Look things over and let me know if you have any additional questions.

With #include <string> Allowed

The code can be virtually identical using string word instead of char word[256]. The only changes that are required are to delete the line #define MAXC 256 and then:

#include <string>
...
    string word;

The remainder of the code can be exactly the same because you can index a C++ string in the same manner you index a C character array. You also have all the other form of the C++ loop available that we discussed at length in the comments. If you want examples of using them, I'm happy to add them, let me know.

However, you should probably use a proper string::iterator to loop over the characters from word.begin() to the character before word.end() which is just another way of doing the same thing. Here that would be:

    for (string::iterator c = word.begin(); c != word.end(); c++) 
    {
        //Use a switch statement to translate each letter into I.C.A.O
        switch (*c)
        {
            case 'a': case 'A': cout<<"Alpha\n"; break;
            case 'b': case 'B': cout<<"Bravo\n"; break;
            case 'c': case 'C': cout<<"Charlie\n"; break;
            ...

An iterator is actually a pointer to the member of the container (the character in word for your string), so that is why you use the dereference *c in switch (*c) to pass the thing pointed to by the iterator (the character) instead of the iterator itself (a pointer to that character).

(don't forget to delete the int i; declaration if you go this route -- it's no longer needed)

Upvotes: 0

paddy
paddy

Reputation: 63461

You never modify word1 in your loop, so the switch statement will always select the letter first letter, which you initialized it to prior to the loop.

The correct way to do this is by looping through the characters in the word like this:

for (char c : word)
{                                                                       
    switch (c) {
        //...
    }
}

If you are using a compiler that doesn't support C++11 features such as range-based loops (used above), you can go oldskool:

for (size_t i = 0; i < word.size(); i++) {
    switch(word[i]) ...
}

Or:

for (std::string::const_iterator it = word.cbegin(); it != word.cend(); it++) {
    switch(*it) ...
}

Upvotes: 2

Related Questions