The problem in the output was it doesn't give the desired lyrics of twelve days of christmas...
Here is my program. I have a problem in the forloop with if-else. for loop with switch works fine..
Here is the program:
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
void for_loop_switch(void)
{
int x;
for(x = 1; x <= 12; x++)
{
cout << "\nOn the " << x << " day of Christmas" << endl;
cout << "My True Love Sent to Me" << endl;
switch(x)
{
case 12:
cout << "Twelve Drummer's Drumming" << endl;
case 11:
cout << "Eleven Piper's Piping" << endl;
case 10:
cout << "Ten Lord's a Leaping" << endl;
case 9:
cout << "Nine Ladies Dancing" << endl;
case 8:
cout << "Eight Maid's a Milking" << endl;
case 7:
cout << "Seven Swan's a Swimming" << endl;
case 6:
cout << "Six Geese a Laying" << endl;
case 5:
cout << "Five Golden Rings" << endl;
case 4:
cout << "Four Calling Birds" << endl;
case 3:
cout << "Three French Hens" << endl;
case 2:
cout << "Two Turtle Doves and" << endl;
case 1:
cout << "A Partridge in a Pear Tree" << endl;
getch();
}
}
}
void for_loop_if(void)
{
int x;
for(x = 1; x <= 12; x++)
{
cout << "\nOn the " << x << " day of Christmas" << endl;
cout << "My True Love Sent to Me" << endl;
if(x == 12)
cout << "Twelve Drummer's Drumming" << endl;
else if(x == 11)
cout << "Eleven Piper's Piping" << endl;
else if(x == 10)
cout << "Ten Lord's a Leaping" << endl;
else if(x == 9)
cout << "Nine Ladies Dancing" << endl;
else if(x == 8)
cout << "Eight Maids a Milking" << endl;
else if(x == 7)
cout << "Seven Swans a Swimming" << endl;
else if(x == 6)
cout << "Six Geese a Laying" << endl;
else if(x == 5)
cout << "Five Golden Rings" << endl;
else if(x == 4)
cout << "Four Calling Birds" << endl;
else if(x == 3)
cout << "Three French Hens" << endl;
else if(x == 2)
cout << "Two Turtle Doves and" << endl;
else if(x == 1)
cout << "A Partridge in a Pear Tree" << endl;
}
}
void main(void)
{
char choice;
do{
clrscr();
cout << "Menu\n\n";
cout << "A - For Loop Switch" << endl;
cout << "B - For Loop If-Else" << endl;
cout << "X - Exit\n\n";
cout << "Enter your choice: ";
cin >> choice;
switch(toupper(choice))
{
case 'A':
clrscr();
for_loop_switch();
getch();
break;
case 'B':
clrscr();
for_loop_if();
getch();
break;
}
}while(toupper(choice) != 'X');
}

New Topic/Question
Reply




MultiQuote





|