• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 833
  • Last Modified:

Switch problem in C

This is a newbie question, but I can't seem to solve the prob

here is the source

int Str_len(char str1[])
{
      int count=0;
      while(*str1 != '\0')
                {
      *str1++;
      count++;
      }            
return count;
}

      

int main()
{
            char string1[100];
      int choice = 0;
      
      printf("\n\nPlease Enter a String: ");      
      scanf("%s",string1);                  
                printf("\nMENU");
      printf("\n1 - Count Number of Characters in String");
      printf("\n2 - Count Number of Words in String");
      printf("\n3 - Remove spaces in String");
      printf("\n4 - Reverse String");
      printf("\n6 - Look for sub-string in String");
      printf("\n7 - Insert sub-string into String\n\n");
                printf("Please option 1 to 7:");
      scanf("%d",&choice);


      switch (choice)
        {
      case 1:
      {
              
            printf("%s has %d characters",string1, Str_len(string1));
            break;
      }

      default:
      {      printf("illegal selection");
                break;
                }
      }


        return 0;
}

My problem is that everytime I enter a string with spacings, example "C is fun", the result is 'Illegal selection' which is in the default case, But it works if the string is just 1 word without spacing, example "cow"

thanks
Simon
0
simonsng
Asked:
simonsng
  • 3
  • 3
  • 2
  • +3
1 Solution
 
jimmackCommented:
Hi Simon,

I don't think it's the spaces in the string causing the problem ;-)

I assume that the problem occurs when you enter a selection from your menu that is anything other than "1".

This is because your switch statement needs "case"s for each of the other options 2 .. 7.

Copy your "case 1:" statement to create these.  To start with, just have a printf inside each one that outputs the number of the selection you have made (don't forget to include the break statement though) ;-)
0
 
simonsngAuthor Commented:
jimmack,

thats a negatory. Tried that before, but only works when i input a string without spacing, otherwise, same result. wasnt able to select as there was an immediate display of the menu and illegal selection printing out, didnt even get to input a choice for the switch  
0
 
logicTRANCECommented:
Hi!

You are accepting a string from user using the
scanf("%s",string1) function. the scanf() function takes a
' '(space) as the end of string character.
So what happens when u enter a string with multiple words is that the scanf function reads the characters from the keyboard buffer until it reaches the 1st SPACE or ENTER character. It ignores the rest of the line. So actually the programme runs with the 1st word of the string.

But then the menu is displayed again and the next scanf() accepts the choice. Now, the remaining words of the previous string are still there in the keyboard buffer, which are read by the scanf() into the variable 'choice'.
Since the value of choice now doesn't match any of the choices you provided, the default case is executed.

I order to be able to accept multiword strings, use gets(char*) instead of scanf();

i.e.,
     gets(string1);

This function will accept input string in string1 until the enter key is not pressed.

No need for the rest of the cases to be written for this case to run!

Goodluck!
Happy Programming!
0
Independent Software Vendors: 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!

 
shamstarCommented:
How come you only awarded logicTRANCE a grade B??
It seems a perfectly good answer to me.
0
 
logicTRANCECommented:
Hi shamstar!

Thanks a lot for mentioning that!
I didn't cos i thought it would not be appropriate!

I'd be grateful if hta grade is somehow changed to an A.

Thanks!
Happy Programming!
0
 
shamstarCommented:
I know what you mean about not mentioning it yourself.
To me giving a grade of A would make sense especially since you provided a solution which only needed one line of code to be changed, and you also provided a good description of why the problem happened with the old code. (Not to mention you beat me to typing the answer :p )

Dunno if the grade can be changed afterwards, maybe Mods can do it??
0
 
simonsngAuthor Commented:
logictrance,

my apology, didn't realise it till I submitted.. is there anyway to change the grade?
0
 
Naman GoelSoftware engineer 1Commented:
actually u can not enter multiple word string using scanf function in this way.
beacuse sacnf just get the first word.
do try out getch() or scanf("%[^~]",s)
that means it accepts all the character to string s until u enter ~ sign as terminator.
bye
0
 
logicTRANCECommented:
hello simonsng!

I think you can make a request to CS for changing the grade. Just leave a message in the Customer Support(CS) section.
I'd be really grateful!
:-)

Shamstar, thanks for ur appreciation!
:-)

Happy Programming!
0
 
shamstarCommented:
Not a problem logicTRANCE.
Sending a message to CS should take care of it.
0
 
jetstreamCommented:
Also to naman_goel's answer....   I use (sometimes)  scanf("%[^\n]",s);    which will accept until a newline (without the need for a ~ terminator).
Cheers.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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