3 Replies - 940 Views - Last Post: 26 February 2011 - 04:53 PM Rate Topic: -----

#1 zour  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-February 11

help with a switch function embedded in a switch function in C

Posted 26 February 2011 - 12:35 PM

Hello , everybody !! I have a question about the switch function in C . I want to embed a switch function in another switch function . For example , I want the program to ask for a letter and if the user types a specific letter then the program asks for another letter and if the user types another specific letter then the program prints a string. Here is the code I wrote :

#include <stdio.h>

int main (int argc, const char * argv[]) {
    char i, j;
	
	printf("type a letter:");
	scanf("%c", &i);
	switch (i) {
		case 'f':
			printf("type a new letter:");
			scanf("%c", &j);
			switch (j) {
				case 'm':
					printf("welldone");
					break;
				case 'n':
					printf("try again");
					break;
			}
			break;
		case 'b':
			printf("bye");
			break;
	}
    return 0;
}


And here is what comes out in the console when I run this program :


/[Session started at 2011-02-26 21:31:00 +0200.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1472) (Wed Jul 21 10:53:12 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
Loading program into debugger…
Program loaded.
run
[Switching to process 335]
Running…
type a letter:f
type a new letter:
Debugger stopped.
Program exited with status value:0./

As you can see , when I type the letter 'f' the program prompts me to type a new letter and then ends without waiting for me to type it and print the string. Why is it happening and how could I make this work ?

Thank you very much !!!

Mod edit - Please :code:

Is This A Good Question/Topic? 0
  • +

Replies To: help with a switch function embedded in a switch function in C

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: help with a switch function embedded in a switch function in C

Posted 26 February 2011 - 01:09 PM

Your program is functioning correctly, in so much as when you enter a character and press return, there are two characters in the buffer. The letter you typed and a newline character. If you modify your code by adding a second %c to your scanf functions, as shown below you will achieve the result you expect.

#include <stdio.h>

int main (int argc, const char * argv[]) {
    char i, j, nl;
	
	printf("type a letter:");
	scanf("%c%c", &i, &nl);
	switch (i) {
		case 'f':
			printf("type a new letter:");
			scanf("%c%c", &j, &nl);
			switch (j) {
				case 'm':
					printf("welldone");
					break;
				case 'n':
					printf("try again");
					break;
			}
			break;
		case 'b':
			printf("bye");
			break;
	}
    return 0;
}


Was This Post Helpful? 2
  • +
  • -

#3 zour  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-February 11

Re: help with a switch function embedded in a switch function in C

Posted 26 February 2011 - 02:19 PM

Thank you very very much !! Now I get it !!
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: help with a switch function embedded in a switch function in C

Posted 26 February 2011 - 04:53 PM

See also my blog post on scanf.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1