-
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
-
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...)
-
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?
-
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;
}
-
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.
-
Please use code tags when posting code.
-
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.