Solved

case label not within a switch statement

Posted on 2006-11-24
4
5,130 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
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 500 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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now