Link to home
Start Free TrialLog in
Avatar of bergertime
bergertime

asked on

case label not within a switch statement

I am trying to code a program that switches a user inputed statement into morse code but i keep getting that error. this is a segment of my code.
main()
{
  char input;
  int i=0,n;

  printf("Enter a phrase to translate into morse code: ");
  scanf("%c",&input);
  printf("Enter the number of letters in that phrase: ");
  scanf("%d",&n);


  while(i=0, n<i, ++i)
  {
    switch(input)
        case 'A':
        case 'a': printf(".-");
              break;
        case 'B':
        case 'b': printf("-...");
              break;
        case 'C':
        case 'c': printf("-.-.");
              break;
        case 'D':
        case 'd': printf("-..");
              break;
}
}
ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of grg99
grg99

You need to declare a string, not a character:

char  input[1000];

then input a string:    gets( input );

then yo don't need to input the array length at all.



then :  while( i = 0; i < strlen( input ); i++ ) {

   switch( input[ i ] )

------------

also if somebody types in  a character not in your cases, add an "else" case to catch all the other values:

   case default:  printf( "?"); break;

----

or a less code-intensive way would use an array of strings:

char * LettersToCode[] = { ".-'. "-...", "-.-." }

then you just need one print statement and no case.

Did any of that solve it?

If so it is now time to select an answer(s) and grade them.

If not, perhaps a clarifying question would help.
main()
{
  char input[100];
  int i=0,n;

  printf("Enter a phrase to translate into morse code: ");
  scanf("%s",&input);
  printf("Enter the number of letters in that phrase: ");
  scanf("%d",&n);


  for(i=0; i<n; ++i)
  {
    switch(input[i])
    {    case 'A':
        case 'a': printf(".-");
              break;
        case 'B':
        case 'b': printf("-...");
              break;
        case 'C':
        case 'c': printf("-.-.");
              break;
        case 'D':
        case 'd': printf("-..");
              break;
    }
}
}

Use for loop instead of while.. and take input as array... and use it for each character..

Thanks,
Sreenath