Hi all - First time posting so please bear with me. I'm in the last week of a C++ class and cannot figure out how to do this assignment.

I need to evaluate each character of a string and process using a switch statement. The CASE processing I have no problem with (been programming various languages for 15+ years now, but this C++ is a doosey to understand!), it's the individual characters I'm having trouble with.

A user inputs a word: something

My question is how do I define/identify each position of that word so I can continue processing?

I'm not asking anyone here to do this for me, I just need some idea of how this is done.

Thanks......any help is appreciated.

The string of characters is accessed just like any other array

std::string word = "Hello";

char oneChar = word[0];

Or in the above replace the word[0] with a loop counter such as word.

Well, I'm not quite sure what you've written. This is an 8 week cram course online class that I'm taking. We haven't covered arrays. I just found in my textbook where I can use the length function to identify how many characters I'll have to use. So, you see......I really don't know the syntax for C++ at all. This is what I have so far:

//********************************************************
#include <iostream>

using namespace std;

int main()
{
    string inpWord;        // User input
    string::size_type len; // Length of input word

    // Read input word
    cout << "Enter any word" << endl;
    cin >> inpWord;
    len = inpWord.length();
    //cout << len << endl;
        
    // Print corresponding alphabet word
    
    system("Pause");
              
    return 0;
}

An array is a sequence of continuous characters. In my Hello example, the first character in the string is 'H', followed by 'e', 'l', ... Each character can be accessed by an index value starting with 0 and ending with the length of the string -1.

std::string word = "Hello";
word[0] = 'H'
word[1] = 'e'
word[2] = 'l'
...
word[4] = 'o'

Now all you have to do in the code you posted is create a for loop and print each character with the loop counter

Thank you! I understand using arrays, just not in C++. But, I also found out that I have to do this without arrays. Since we have not yet covered this in the chapter, I have to use what we've learned so far to do this problem.

I'm going to try to do this using the cin.get function along with a While loop to count. I'll post what I have so far tomorrow and let you know how far I get.

Thank you very much for the help.....this is so frustrating when there is no one to ask and no good examples in the text book.

It's not clear to me what kind of "evaulation" you need to do. Is it looking at each charactor for alpha, numeric, punctuation or spaces or what?

The other problem is we don't know what you are "allowed to use".
For example, there are lots of possibilities in <string> and <cstring>

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
int main()
{
  char myString[100];
  cout<<"Your statement"<<endl;
  if(cin.get(myString,100))
    cout<<a;
   else
    exit(0);
  return 0;
}

Perhaps you could try out this but I have used at least once the concept of char array which is also called c-style string.
// these header files do not exist in latest
version of c++ but runs on my Turbo C++
very well out you could try out the logic
with your setup //

You should use -usingnamespacestd for C++

(exit(0) is a function to terminate the program after your work is over you may use it if you wish and is accessible from process.h)

Actually my college still accepts the old look of C++
Best of luck for your tour in C++

Sorry that a stands for myString forgot to modify it ,now it is better and it prints each charecter on a separate line

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
int main()
{
  char myString[100];
  cout<<"Your statement"<<endl;
  if(cin.get(myString,100))
    cout<<myString<<endl;
   else
    exit(0);
  return 0;
}

Perhaps you could try out this but I have used at least once the concept of char array which is also called c-style string in C++

// these header files do not exist in latest
version of c++ but runs on my Turbo C++
very well out you could try out the logic
with your setup //

(exit(0) is a function to terminate the program after your work is over you may use it if you wish and is accessible from process.h)

Actually my college still accepts the old look of C++
Best of luck for your tour in C++

It's not clear to me what kind of "evaluation" you need to do. Is it looking at each charactor for alpha, numeric, punctuation or spaces or what?

The other problem is we don't know what you are "allowed to use".
For example, there are lots of possibilities in <string> and <cstring>

JRM - Sorry I didn't mean to be ambiguous, just trying to follow the rules regarding homework help. I posted the question on another tek board and they flat-out removed it! So, I'm just trying to be careful here.

The assignment is this: Take a word which a user inputs and display the corresponding flight aviation word codes for it. For instance, if I input "abc", the output to display should read
alpha bravo charlie.

As far as what is allowed, we've covered the absolute basics of <string>. Cstring was mentioned but no functions were discussed. In the looping chapter, the only process discussed was the While statement. Do and For was not covered (strange textbook I have). I did go into details of controlling the loops with sentinel values, using flags and EOF controls though. And it covered how to use iteration counters and event counters. It also covered nesting one While statement into another While statement.

I hope this makes it a little clearer. I'll post what code I have so far later today.

>(exit(0) is a function to terminate the program after your work is over
>you may use it if you wish and is accessible from process.h)
exit is a function in cstdlib, or stdlib.h on your dinosaur of a compiler. The difference is that process.h isn't a standard header, so your code won't compile on many compilers. Also, exit ( 0 ) is identical in function to return 0; from main. So there's really no point in using it here.

It's always a good idea to avoid including headers you aren't actively using, which means that conio.h and stdio.h should be removed.

#include <iostream.h>

int main()
{
  char myString[100];

  cout<<"Your statement\n";

  if ( cin.get ( myString, 100 ) )
    cout<< myString <<'\n';

  return 0;
}

Thank you Narue and hinduengg - I use the Dev-C++ compiler and found the original code posted by hinduengg would not compile without first removing the references to headers I did need. But, I also had to add the using namespace std; at the beginning for either code snippet to compile.

It runs now, but just flashes on the screen after I input a word. What does the [100] signify. Does it designate myString as an array with 100 characters? If so, should I be able to use myString[0], mystring[1], etc.?

>It runs now, but just flashes on the screen after I input a word.
That's because you don't do anything to stop the program from ending. When the program ends, the window that Dev-C++ created to host it is closed. You can fix that by putting in a request for input:

#include <iostream>

using namespace std;

int main()
{
  char myString[100];

  cout<<"Your statement\n";

  if ( cin.get ( myString, 100 ) )
    cout<< myString <<'\n';

  cout<<"Press Enter to continue. . .";
  cin.get();

  return 0;
}

>Does it designate myString as an array with 100 characters?
Yes.

>If so, should I be able to use myString[0], mystring[1], etc.?
I don't see why not.

Whoohoo....this works!

#include <iostream>

using namespace std;

int main()
{
  char myString[100];

  cout<<"Your statement" << endl;

  if ( cin.get ( myString, 100 ) )
    cout<< myString[0] << endl;
    cout<< myString[1] << endl;
    cout<< myString[2] << endl;
  system("Pause");

  return 0;
}

I'm just gonna have to use arrays.....I don't see any way around it. Now, I need to auto-increment the array number to the number of characters in the given word.

I've got to incorporate this into my existing code and see what wall I hit next.

Thanks!!!! to all of you!!!

if ( cin.get ( myString, 100 ) )
  cout<< myString[0] << endl;
  cout<< myString[1] << endl;
  cout<< myString[2] << endl;

If your conditional has more than one statement, wrap it in braces:

if ( cin.get ( myString, 100 ) ) {
  cout<< myString[0] << endl;
  cout<< myString[1] << endl;
  cout<< myString[2] << endl;
}

What you have now is equivalent to this:

if ( cin.get ( myString, 100 ) ) {
  cout<< myString[0] << endl;
}

cout<< myString[1] << endl;
cout<< myString[2] << endl;

Your code only appears to work like you want because with correct input it does what you expect. Try the following and type ctrl+z to signal end-of-file. The condition will fail, and what you would expect is no output, but since you didn't use braces, only the first cout statement is skipped. The actual output is two bangs:

#include <iostream>

using namespace std;

int main()
{
  char myString[100];

  for ( int i = 0; i < 100; i++ )
    myString[i] = '!';

  cout<<"Your statement" << endl;

  if ( cin.get ( myString, 100 ) )
    cout<< myString[0] << endl;
    cout<< myString[1] << endl;
    cout<< myString[2] << endl;
  system("Pause");

  return 0;
}

Thank you very much for the help here. The code that hinduengg provided and Narue expanded on has helped me to better understand what I must accomplish with my own assignment.

Here's what I have now. It takes the input word and outputs each character......next step, convert this to a Switch statement function. I'll let you if I run into any problems with that part.

#include <iostream>
using namespace std;

int main()
{
    string inpWord;        // User input
    string::size_type len; // Length of input word
    int i;

    // Read input word and initialize counter
    cout << "Enter any word" << endl;
    cin >> inpWord;
    len = inpWord.length();
    i = 0;
         
    // Print each character
    while (i <= len)
    {
    cout << inpWord[i] << endl;
    i++;
    } 
       
    system("Pause");
              
    return 0;
}

Does anyone see anything wrong with this. It works, just wondering if there is anything obviously wrong here. I have to convert the processing into a function....first time I tried failed, so was curious whether anything here would cause that.

//
#include <iostream>

using namespace std;

int main()
{
    string inpStr;        // User input
    string::size_type len; // Length of input word
    int i;

    // Read input word and initialize counter
    cout << "Enter any word" << endl;
    cin >> inpStr;
    len = inpStr.length();
    i = 0;
         
    // Print each characters associated ICAO word
    while (i <= len)
    {
        switch (inpStr[i])
        {
        case 'a' : 
        case 'A' : cout << " Alpha";
        break; 
        case 'b' :
        case 'B' : cout << " Bravo";
        break;
        case 'c' :
        case 'C' : cout << " Charlie";
        break;
        case 'd' : 
        case 'D' : cout << " Delta";
        break; 
        case 'e' :
        case 'E' : cout << " Echo";
        break;
        case 'f' :
        case 'F' : cout << " Foxtrot";
        break;
        case 'g' : 
        case 'G' : cout << "Golf";
        break; 
        case 'h' :
        case 'H' : cout << " Hotel";
        break;
        case 'i' :
        case 'I' : cout << " India";
        break;
        case 'j' : 
        case 'K' : cout << " Kilo";
        break; 
        case 'l' :
        case 'L' : cout << " Lima";
        break;
        case 'm' :
        case 'M' : cout << " Mike";
        break;
        case 'n' : 
        case 'N' : cout << "November";
        break; 
        case 'o' :
        case 'O' : cout << " Oscar";
        break;
        case 'p' :
        case 'P' : cout << " Papa";
        break;
        case 'q' : 
        case 'Q' : cout << "Quebec";
        break; 
        case 'r' :
        case 'R' : cout << " Romeo";
        break;
        case 's' :
        case 'S' : cout << " Sierra";
        break;
        case 't' :
        case 'T' : cout << " Tango";
        break;
        case 'u' : 
        case 'U' : cout << "Uniform";
        break; 
        case 'v' :
        case 'V' : cout << " Victor";
        break;
        case 'w' :
        case 'W' : cout << " Whiskey";
        break;
        case 'x' : 
        case 'X' : cout << " X-ray";
        break; 
        case 'y' :
        case 'Y' : cout << " Yankee";
        break;
        case 'z' :
        case 'Z' : cout << " Zebra";
        break;
        }     
    i++;
    } 
       
    system("Pause");
              
    return 0;
}

>>while (i <= len)

index values range from 0 to but not including len. So just use the less-then operator.

a for loop would be better

for(i = 0; i < len; ++i)
{


}

>>first time I tried failed
post the code.

Here's the code using a function. Seems C++ doesn't like to pass variables with array values. Anyone have advice on this?

//*********************************************************
#include <iostream>

using namespace std;

// Function prototypes
void GetICAOWords(string);

int main()
{
    string inpStr;        // User input
    string::size_type len; // Length of input word
    int i;

    // Read input word and initialize counter
    cout << "Enter any word" << endl;
    cin >> inpStr;
    len = inpStr.length();
    i = 0;
         
    // Print each character
    while (i <= len)
    {
        GetICAOWords(inpStr[i]);    
    i++;
    } 
       
    system("Pause");
              
    return 0;
}
//*********************************************************
void GetICAOWords
    (/* inout */ string inpStr[i])//Input word from user 
      
// Finds ICAO word that corresponds to characters from input string
// Precondition:
//     String is input from user, individual characters are passed to function
// Postcondition:
//     Matching ICAO word is output to screen

switch (inpStr[i])
{
    case 'a' : 
    case 'A' : cout << " Alpha";
    break; 
    case 'b' :
    case 'B' : cout << " Bravo";
    break;
    case 'c' :
    case 'C' : cout << " Charlie";
    break;
    case 'd' : 
    case 'D' : cout << " Delta";
    break; 
    case 'e' :
    case 'E' : cout << " Echo";
    break;
    case 'f' :
    case 'F' : cout << " Foxtrot";
    break;
    case 'g' : 
    case 'G' : cout << "Golf";
    break; 
    case 'h' :
    case 'H' : cout << " Hotel";
    break;
    case 'i' :
    case 'I' : cout << " India";
    break;
    case 'j' : 
    case 'K' : cout << " Kilo";
    break; 
    case 'l' :
    case 'L' : cout << " Lima";
    break;
    case 'm' :
    case 'M' : cout << " Mike";
    break;
    case 'n' : 
    case 'N' : cout << "November";
    break; 
    case 'o' :
    case 'O' : cout << " Oscar";
    break;
    case 'p' :
    case 'P' : cout << " Papa";
    break;
    case 'q' : 
    case 'Q' : cout << "Quebec";
    break; 
    case 'r' :
    case 'R' : cout << " Romeo";
    break;
    case 's' :
    case 'S' : cout << " Sierra";
    break;
    case 't' :
    case 'T' : cout << " Tango";
    break;
    case 'u' : 
    case 'U' : cout << "Uniform";
    break; 
    case 'v' :
    case 'V' : cout << " Victor";
    break;
    case 'w' :
    case 'W' : cout << " Whiskey";
    break;
    case 'x' : 
    case 'X' : cout << " X-ray";
    break; 
    case 'y' :
    case 'Y' : cout << " Yankee";
    break;
    case 'z' :
    case 'Z' : cout << " Zebra";
    break;
}

Try this instead:

void GetICAOWords ( const char letter )
{
  switch ( letter ) {
    // All of your cases
  }
}

That way you aren't confused by strings and indexing.

Try this instead:

void GetICAOWords ( const char letter )
{
  switch ( letter ) {
    // All of your cases
  }
}

That way you aren't confused by strings and indexing.

I'm sorry, I don't know what that code means. Where does letter come from? I would have to pass the character of the word being input to that somehow, but I don't know from what you have here.

>I don't know what that code means.
Then you don't know functions well enough to use them yet.

#include <cstdlib> // Required for the system function
#include <iostream>

using namespace std;

void GetICAOWords ( const char letter );

int main()
{
  string inpStr;

  cout<<"Enter any word: ";
  cin>> inpStr;

  for ( string::size_type i = 0; i < inpStr.size(); i++ )
    GetICAOWords ( inpStr[i] );

  system("Pause");
}

void GetICAOWords ( const char letter )
{
  switch ( letter ) {
    // All of your cases
  }
}

You declare a function:

void GetICAOWords ( const char letter );

Define it:

void GetICAOWords ( const char letter )
{
  switch ( letter ) {
    // All of your cases
  }
}

And then call it

GetICAOWords ( inpStr[i] );

The declaration only tells the compiler the name, return type, and parameter types of the function. That's why you end it with a semicolon and not a block of code. The definition is where you actually assign a block of code to the function. The key thing to remember for now is that the definition of a function should be self-contained. That is, everything it does only relies on the parameters.

When you call a function, you give the parameters values by passing arguments. inpStr is an argument that gives a value to letter, which is a parameter. It goes like this if you take the function out of the problem and replace it with inline code:

for ( string::size_type i = 0; i < inpStr.size(); i++ ) {
  // We're basically calling GetICAOWords starting here:
  const char letter = inpStr[i];

  switch ( letter ) {
    // All of your cases
  }
}

Thanks for the clarification.
I don't know how the "switch" function that you mentioned originally would be appropriate for what you're trying to do. As far as I know, it's best suited for menu choices.

I would go with <string> , use the functions do the crunching.
You might think about comparing the captured string to the first letter of a (second) hard-coded string which contains your alpha bravo, etc.
It will be true to print since the first letter matches in all cases.

Just one way to the "skin the cat".

good luck and happy coding!

>I don't know what that code means.
Then you don't know functions well enough to use them yet.

ROFL......You are So Right!!!!

I don't know functions, however I have to try and understand them to complete the assignment. I'm looking at the code you've provided. Thank you for that. I'll be online off and on the rest of the night. I'll ask more questions as I try to understand this.

If you're not allowed to use arrays, use the .at and .length string member functions.

Make sure to #include <string>

#include <iostream>
#include <string>
using namespace std;

int main(int argc, int* argv[]) 
{
string word;
getline(cin,word);

/* then depending on what kind of processing you need, you might use a for loop or a switch statement. If you're using a switch statement, then unless you know the length of the word in advance, you will need some extra code */

int i;
char letter;
switch(i)
{
case 1:
letter = word.at(1);
break;

//etc...

}

// or a loop...
for (int i=0; i<word.length(); i++)
{
cout << word.at(i);
// or do whatever processing..
}

return 0;
}

umm... hope it helped.. strings were annoying to me at first, but when you learn arrays it all starts to sink in..

Thanks to all for your help. I'm submitting the code I have w/o the functions....a few points off but better than no points at all. I couldn't spend any more time on it since I have one more assignment to complete by Friday.

#include <iostream>
#include <cstring>
                           // for strlen() [length of word]

using namespace std;

int main()
{
    char a[100];
     cout<<"enter your word"<<endl;     
    cin.getline(a,100);   // User input
    int l;
    l=strlen(a); // Length of input word
    int i;
    i = 0;
    while (i < len)  // the string always has last index for '\0'
    { 
  switch (a[i])
        {
        case 'a' : 
        case 'A' : cout << " Alpha";
        break; 
        case 'b' :
        case 'B' : cout << " Bravo";
        break;
        case 'c' :
        case 'C' : cout << " Charlie";
        break;
        case 'd' : 
        case 'D' : cout << " Delta";
        break; 
        case 'e' :
        case 'E' : cout << " Echo";
        break;
        case 'f' :
        case 'F' : cout << " Foxtrot";
        break;
        case 'g' : 
        case 'G' : cout << "Golf";
        break; 
        case 'h' :
        case 'H' : cout << " Hotel";
        break;
        case 'i' :
        case 'I' : cout << " India";
        break;
        case 'j' : 
        case 'K' : cout << " Kilo";
        break; 
        case 'l' :
        case 'L' : cout << " Lima";
        break;
        case 'm' :
        case 'M' : cout << " Mike";
        break;
        case 'n' : 
        case 'N' : cout << "November";
        break; 
        case 'o' :
        case 'O' : cout << " Oscar";
        break;
        case 'p' :
        case 'P' : cout << " Papa";
        break;
        case 'q' : 
        case 'Q' : cout << "Quebec";
        break; 
        case 'r' :
        case 'R' : cout << " Romeo";
        break;
        case 's' :
        case 'S' : cout << " Sierra";
        break;
        case 't' :
        case 'T' : cout << " Tango";
        break;
        case 'u' : 
        case 'U' : cout << "Uniform";
        break; 
        case 'v' :
        case 'V' : cout << " Victor";
        break;
        case 'w' :
        case 'W' : cout << " Whiskey";
        break;
        case 'x' : 
        case 'X' : cout << " X-ray";
        break; 
        case 'y' :
        case 'Y' : cout << " Yankee";
        break;
        case 'z' :
        case 'Z' : cout << " Zebra";
        break;
        }     
    i++;
    } 
       
    cin.getch();//to pause             
    return 0;
}

Now tell if it still has problems.

This question has already been answered. Start a new discussion instead.