Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6194
  • Last Modified:

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;
}
}
0
bergertime
Asked:
bergertime
  • 2
1 Solution
 
imladrisCommented:
A switch takes a compound statement so you need braces:

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;
    }
}
}
0
 
grg99Commented:
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.

0
 
imladrisCommented:
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.
0
 
sreenathkCommented:
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now