switch/case problem

This is a discussion on switch/case problem within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> main() { int day; clrscr (); printf("enter the integer;"); scanf("%d",&day); switch (day) { case 1: printf("monday"); printf("\n enter ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    1

    switch/case problem

    Code:
    #include<stdio.h>
    main()
    {
    int day;
    clrscr ();
    printf("enter the integer;");
    scanf("%d",&day);
    switch (day)
    {
    case 1:
    printf("monday");
    printf("\n enter an integer:");
    scanf("%d",&day);
    break;
    case 2:
    printf("tuesday");
    printf("\n enter an integer;");
    scanf("%d",&day);
    break;
    default:
    printf("\n day is not available");
    printf("/n enter an integer:");
    }
    getche ();
    }
    when i input a integer in the 1st, the answer will show but when i input an integer again the answer didnt show

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    The first time you enter an integer, it's through the scanf. The second, it's through the call to getch() at the end - to end the program. If you want to backup and go through the switch/case block again, you need to put it inside a loop, that loops until some "exit condition" is reached.

    Also - indent your code properly - it'll make it a lot easier for other people to read and debug.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by maxilog1009 View Post
    Code:
    getche ();
    Too late, the Bolivians beat you to him a long time ago
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    49
    The answer is to set up a while loop and use a variable (like a bool). If the user enters something that is invalid, the variable changes values to meet the while loops conditions (say true?) and exit the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch/case problem?
    By Wiiplayer12 in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2008, 11:13 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21