Switch in a While Loop

This is a discussion on Switch in a While Loop within the C Programming forums, part of the General Programming Boards category; I have a programme that must include two switched in a while loop, rather i am not sure if they ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    Question Switch in a While Loop

    I have a programme that must include two switched in a while loop, rather i am not sure if they have to be inside or if it doesn't matter. But i have written some coding, but it is incorrect, as i am not getting out the correct data. It should put out the first three verses of "The Twelve Days of Christmas", but it isn't.
    Here is what I have so far:
    #include <stdio.h>
    int main()
    {
    int day;
    char number;
    printf("On the %d of Christman my true love gave to me:\n", day);
    while (day++)
    {
    switch(day)
    {
    case 1:
    day = 1;
    continue;
    case 2:
    day = 2;
    continue;
    case 3:
    day = 3;
    continue;
    }
    switch (number)
    {
    case 1:
    scanf("%d", &day);
    printf("A partrige in a pare tree.");
    continue;
    case 2:
    scanf("%d", &day);
    printf("Two turtle doves.");
    continue;
    case 3:
    scanf("%d", &day);
    printf("Three french hens.");
    continue;
    }
    }
    return 0;
    }
    I am sure that my logic is incorrect. I can run this with out any syntax errors, but, as I stated before, I do not get the correct output.
    ANy help would be greatly appreciated.
    -Sacha

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Some problems/suggestions:
    - you should initialize day
    - There's no end condition in your while loop.
    - why are you using scanf?
    - your first switch statement does nothing, if day = 1, set day = 1?!!
    - you sure you want to use continue statements and not break?

    You sure you have to use a while loop and 2 switches?
    Well if you must, here's some pseudo code:

    Code:
    day = 1
    while( day < = 3)
       print "on the"
       switch day
          case 1: print "1st" break
          case 2: print "2nd" break
          case 3: print "3rd" break
       print "day of christmas my true love gave to me
       switch day
          case 3: print "3 french hens, ... and a partridge..." break
          case 2: print "2 turtle doves ... and a..." break
          case 1: print "a partridge on a ..." break
       increment day
    (there are other ways of doing this...)
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    Talking

    Cheers,
    Ya, I wasn't quite sure why I was using scanf. I was just a little confused: ) I was told that I should use the continue, so as to make it fall through. Is there another way to make this happen?

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    7
    Ok, I have rewritten the programme, but this time it is just a repition of: on the first....over and over again.
    should i just try dropping the continues?
    here is what it looks like now:
    #include <stdio.h>
    int main()
    {
    int day;
    day = 1
    while (day < 4) /* my prof likes it this way*/
    {
    switch(day)
    {
    case 1:
    printf("On the first ");
    continue;
    case 2:
    printf("On the second ");
    continue;
    case 3:
    printf("One the third ");
    continue;
    }
    printf("day of Christmas my true love gave to me:\n");
    switch (day)
    {
    case 1:
    printf("A partrige in a pare tree.");
    continue;
    case 2:
    printf("Two turtle doves, and a ");
    continue;
    case 3:
    printf("Three french hens, ");
    continue;
    }
    }
    return 0;
    }

  5. #5
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    I think you're having a bit of trouble visualizing the concept. Maybe you should step through both versions of the code with a debugger and see how it behaves.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please use code tags when posting code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    7
    Cheers for everyone's help. I have achieved my goal. It came out as it should have. I had a sit down and thought it all through and it worked.
    As to using code tags, when I initially typed it out on here I had all of the spacing in there and such. It went away when I posted, so not quite sure what happened. I will use code tags next time I post.
    thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to loop in switch??
    By pczafer in forum C++ Programming
    Replies: 6
    Last Post: 05-04-2009, 02:53 AM
  2. break statement not within loop or switch
    By Arruba in forum C Programming
    Replies: 3
    Last Post: 11-04-2006, 01:36 AM
  3. Replies: 1
    Last Post: 10-27-2006, 02:21 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 01:46 AM

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