7 Replies - 2619 Views - Last Post: 29 December 2010 - 10:54 AM Rate Topic: -----

#1 zymeth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-December 10

Switch Statement to If Else Statement

Posted 29 December 2010 - 06:23 AM

Hello! My problem is how can i change switch to if-else..

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');
}



Is This A Good Question/Topic? 0
  • +

Replies To: Switch Statement to If Else Statement

#2 newclearner  Icon User is offline

  • D.I.C Regular

Reputation: 103
  • View blog
  • Posts: 302
  • Joined: 29-September 10

Re: Switch Statement to If Else Statement

Posted 29 December 2010 - 06:38 AM

If you know the working of switch case then you must be knowing that in a switch case statement, all the cases following your matched case get executed if you don't put a break statement, so in your switch statements, lets suppose x = 12, so case 12 and all the cases below it would get printed since you have not used break in any case.
But in If else statement, fo x = 12, only if part gets executes and rest is ignored because that comes under else part and in if else, either if will get executed or else.

Makes sense?

Some suggestion:
1
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
are depreciated. Use them as
#include <iostream>

what is stdio and conio doing here? You are using c and C++ header files together. And if I am not wrong conio.h is also depreciated. And clrscr() function also comes in this category.

2. void main(void) is wrong. It should be int main(void). Check out my signatures to know why? main returns int.

This post has been edited by newclearner: 29 December 2010 - 06:40 AM

Was This Post Helpful? 1
  • +
  • -

#3 newclearner  Icon User is offline

  • D.I.C Regular

Reputation: 103
  • View blog
  • Posts: 302
  • Joined: 29-September 10

Re: Switch Statement to If Else Statement

Posted 29 December 2010 - 06:49 AM

IMO this is how you want your output to be printed...
On the 12 day of Christmas
"My True Love Sent to Me"
"Twelve Drummer's Drumming"
On the 11 day of Christmas
"My True Love Sent to Me"
"Eleven Piper's Piping"
On the 10 day of Christmas
"My True Love Sent to Me"
"Ten Lord's a Leaping"
On the 9 day of Christmas
"My True Love Sent to Me"
"Nine Ladies Dancing"
On the 8 day of Christmas
"My True Love Sent to Me"
"Eight Maids a Milking"
On the 7 day of Christmas
"My True Love Sent to Me"
"Seven Swans a Swimming"
On the 6 day of Christmas
"My True Love Sent to Me"
"Six Geese a Laying"
On the 5 day of Christmas
"My True Love Sent to Me"
"Five Golden Rings"
On the 4 day of Christmas
"My True Love Sent to Me"
"Four Calling Birds"
On the 3 day of Christmas
"My True Love Sent to Me"
"Three French Hens"
On the 3 day of Christmas
"My True Love Sent to Me"
"Two Turtle Doves and"
On the 1 day of Christmas
"My True Love Sent to Me"
"A Partridge in a Pear Tree"

Right..? If yes, then I guess your for loop is working right and you need to fix your switch case statement. Have you run your program?

This post has been edited by newclearner: 29 December 2010 - 06:49 AM

Was This Post Helpful? 1
  • +
  • -

#4 zymeth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-December 10

Re: Switch Statement to If Else Statement

Posted 29 December 2010 - 07:09 AM

no.. i want to follow the switch.... the one that prints everything...

1st day
partridge

2nd day
turtle
partridge

3rd day
hens
turtle
partridge

how can i do it with if else.. ohh by the way thank you very much for making clear with the matter of break and use of switch statement really helps... but how can i fix with if-else with my desired output?
Was This Post Helpful? 0
  • +
  • -

#5 newclearner  Icon User is offline

  • D.I.C Regular

Reputation: 103
  • View blog
  • Posts: 302
  • Joined: 29-September 10

Re: Switch Statement to If Else Statement

Posted 29 December 2010 - 07:25 AM

In place of == sign in your if else statements use >=. Let me know if it still doesn't work.

This post has been edited by newclearner: 29 December 2010 - 07:25 AM

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6021
  • View blog
  • Posts: 23,395
  • Joined: 23-August 08

Re: Switch Statement to If Else Statement

Posted 29 December 2010 - 08:16 AM

No matter WHAT your teacher or book says, this:
void main(void)

is ALWAYS WRONG in C++. It's
int main()

if you don't care about command line arguments, or
int main(int argc, char *argv[])

if you do. main MUST return an integer in C++.

But then again you're using a 20-year-old compiler, so why should I be surprised this is what you're being taught?
Was This Post Helpful? 1
  • +
  • -

#7 newclearner  Icon User is offline

  • D.I.C Regular

Reputation: 103
  • View blog
  • Posts: 302
  • Joined: 29-September 10

Re: Switch Statement to If Else Statement

Posted 29 December 2010 - 08:17 AM

Did it solve your problem?
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5746
  • View blog
  • Posts: 12,553
  • Joined: 16-October 07

Re: Switch Statement to If Else Statement

Posted 29 December 2010 - 10:54 AM

Your case works by fall through. Forget the else if and just stick to if.
if (x>=12) { cout << "Twelve Drummer's Drumming" << endl; }
if (x>=11) { cout << "Eleven Piper's Piping" << endl; }
if (x>=10) { cout << "Ten Lord's a Leaping" << endl; }
if (x>= 9) { cout << "Nine Ladies Dancing" << endl; }
if (x>= 8) { cout << "Eight Maids a Milking" << endl; }
if (x>= 7) { cout << "Seven Swans a Swimming" << endl; }
if (x>= 6) { cout << "Six Geese a Laying" << endl; }
if (x>= 5) { cout << "Five Golden Rings" << endl; }
if (x>= 4) { cout << "Four Calling Birds" << endl; }
if (x>= 3) { cout << "Three French Hens" << endl; }
if (x>= 2) { cout << "Two Turtle Doves and" << endl; }
cout << "A Partridge in a Pear Tree" << endl;



Your case works by fall through. Forget the else if and just stick to if.

Of course, using a switch or if then for what is a list of values is kind of sloppy.

I'd do something like this:
void showDays(int day) {
	const int dayCount = 12;
	const char *days[dayCount] = { "A Partridge in a Pear Tree",
		"Two Turtle Doves and", "Three French Hens","Four Calling Birds",
		"Five Golden Rings","Six Geese a Laying","Seven Swans a Swimming",
		"Eight Maids a Milking","Nine Ladies Dancing","Ten Lord's a Leaping",
		"Eleven Piper's Piping","Twelve Drummer's Drumming". };
	while(day-- > 0) { cout << days[day] << endl; }
}



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1