?
Solved

case label not within a switch statement

Posted on 2006-11-24
4
Medium Priority
?
5,879 Views
Last Modified: 2011-04-14
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
Comment
Question by:bergertime
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 2000 total points
ID: 18008061
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
 
LVL 22

Expert Comment

by:grg99
ID: 18012968
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
 
LVL 16

Expert Comment

by:imladris
ID: 18020279
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
 
LVL 3

Expert Comment

by:sreenathk
ID: 18026871
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question