Solved

case label not within a switch statement

Posted on 2006-11-24
4
5,512 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 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

Industry Leaders: 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!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 opening and reading files in the C programming language.

730 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