Having errors in switch statement

Wants to display the days with respective month using switch statement

Page 1 of 1

11 Replies - 1545 Views - Last Post: 21 February 2009 - 07:52 PM Rate Topic: -----

#1 HEDGE.c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 05-December 08

Having errors in switch statement

Post icon  Posted 20 February 2009 - 01:17 PM

// days in a given month.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	char month,january,feburary,march,april,may,june,july,august,september,october,november,december;
	cout<< "\nEnter january,feburary,march,april,may,june,july,august,september,october,november,december : ";
	cin>>month;
	switch(month)
	{
	case january:
		cout<< "31\n";
		break;
	case feburary:
		cout<< "28\n";
		break;
	case march:
		cout<< "31\n";
		break;
	case april:
		cout<< "30\n";
		break;
	case may:
		cout<< "31\n";
		break;
	case june:
		cout<< "30\n";
		break;
	case july:
		cout<< "31\n";
		break;
	case august:
		cout<< "31\n";
		break;
	case september:
		cout<< "30\n";
		break;
	case october:
		cout<< "31\n";
		break;
	case november:
		cout<< "30\n";
		break;
	case december:
		cout<< "31\n";
		break;
	}

	return 0;
}


the thing is that im not getting the output ...can cum1 tell me wht shud i edit or include in order to recieve the out put....

Is This A Good Question/Topic? 0
  • +

Replies To: Having errors in switch statement

#2 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Re: Having errors in switch statement

Posted 20 February 2009 - 01:24 PM

First off, :code:

Second off, why in the world are you initializing that many variables?

char month,january,feburary,march,april,may,june,july,august,september,october,novemb
er,december;



This is absolutely un-necessary.

char month;



Done.


Next problem. Add a default case to your switch statement. If someone has a spelling error, your program is going to fail.


And last but not least, you are not pausing the function before it ends.

When you return 0; you are closing the program... so even if it DOES display the information, your program is going to terminate before you can even see it.

system("pause");
return 0;





I hope this helps!

This post has been edited by pr4y: 20 February 2009 - 01:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 HEDGE.c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 05-December 08

Re: Having errors in switch statement

Posted 20 February 2009 - 01:40 PM

View Postpr4y, on 20 Feb, 2009 - 12:24 PM, said:

First off, :code:

Second off, why in the world are you initializing that many variables?

char month,january,feburary,march,april,may,june,july,august,september,october,novemb
er,december;



This is absolutely un-necessary.

char month;



Done.


Next problem. Add a default case to your switch statement. If someone has a spelling error, your program is going to fail.


And last but not least, you are not pausing the function before it ends.

When you return 0; you are closing the program... so even if it DOES display the information, your program is going to terminate before you can even see it.

system("pause");
return 0;





I hope this helps!

i did wht u said but see these errors
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(16) : error C2065: 'january' : undeclared identifier
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(16) : error C2051: case expression not constant
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(19) : error C2065: 'feburary' : undeclared identifier
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(19) : error C2051: case expression not constant
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(22) : error C2065: 'march' : undeclared identifier
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(22) : error C2051: case expression not constant
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(25) : error C2065: 'april' : undeclared identifier
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(25) : error C2051: case expression not constant
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(28) : error C2065: 'may' : undeclared identifier
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(28) : error C2051: case expression not constant
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(31) : error C2065: 'june' : undeclared identifier
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(31) : error C2051: case expression not constant
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(34) : error C2065: 'july' : undeclared identifier
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(34) : error C2051: case expression not constant
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(37) : error C2065: 'august' : undeclared identifier
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(37) : error C2051: case expression not constant
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(40) : error C2065: 'september' : undeclared identifier
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(40) : error C2051: case expression not constant
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(43) : error C2065: 'october' : undeclared identifier
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(43) : error C2051: case expression not constant
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(46) : error C2065: 'november' : undeclared identifier
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(46) : error C2051: case expression not constant
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(49) : error C2065: 'december' : undeclared identifier
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(49) : error C2051: case expression not constant
1>c:\documents and settings\umer\my documents\visual studio 2008\projects\days in a given month\days in a given month\days in a given month.cpp(52) : warning C4060: switch statement contains no 'case' or 'default' labels
Was This Post Helpful? 0
  • +
  • -

#4 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Having errors in switch statement

Posted 20 February 2009 - 01:45 PM


int main () {
  string month;
  cout << "Enter Name of Month:\n";
  cin >> month;

  switch ( month ) {
	case default:
	  cout << "Invalid Input\n";
	break;

	case "january":
	  cout << "Your stuff\n";
	break;
  }
}


Was This Post Helpful? 0
  • +
  • -

#5 HEDGE.c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 05-December 08

Re: Having errors in switch statement

Posted 20 February 2009 - 02:03 PM

View Poste_barroga, on 20 Feb, 2009 - 12:45 PM, said:


int main () {
  string month;
  cout << "Enter Name of Month:\n";
  cin >> month;

  switch ( month ) {
	case default:
	  cout << "Invalid Input\n";
	break;

	case "january":
	  cout << "Your stuff\n";
	break;
  }
}



it still didnt worked
Was This Post Helpful? 0
  • +
  • -

#6 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Having errors in switch statement

Posted 20 February 2009 - 02:19 PM

What do you have so far?
Was This Post Helpful? 0
  • +
  • -

#7 HEDGE.c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 05-December 08

Re: Having errors in switch statement

Posted 20 February 2009 - 02:30 PM

View Poste_barroga, on 20 Feb, 2009 - 01:19 PM, said:

What do you have so far?



this is the whole thing i tried

#include "stdafx.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	string month;
	cout<< "\nEnter the name of the month : ";
	switch (month)
	{
	case default:
		cout<< "Invalid input\n";
	case january:
		cout<< "31\n";
		break;
	case feburary:
		cout<< "28\n";
		break;
	case march:
		cout<< "31\n";
		break;
	case april:
		cout<< "30\n";
		break;
	case may:
		cout<< "31\n";
		break;
	case june:
		cout<< "30\n";
		break;
	case july:
		cout<< "31\n";
		break;
	case august:
		cout<< "31\n";
		break;
	case september:
		cout<< "30\n";
		break;
	case october:
		cout<< "31\n";
		break;
	case november:
		cout<< "30\n";
		break;
	case december:
		cout<< "31\n";
		break;
	}
	
}

Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: Having errors in switch statement

Posted 20 February 2009 - 02:57 PM

edited for stupidity.

This post has been edited by KYA: 20 February 2009 - 03:57 PM

Was This Post Helpful? 0
  • +
  • -

#9 boblied  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 66
  • Joined: 20-February 09

Re: Having errors in switch statement

Posted 20 February 2009 - 03:44 PM

View PostKYA, on 20 Feb, 2009 - 01:57 PM, said:

You need to do some input before moving into the switch statement. And for a stirng you need to enclose the case in double quotes:

cin >> month; //since its a C++ string with no anticipated spaces we can use cin by itself
switch(month)
{
   case "January":
//etc...
}



You can't switch on a string in C++. The cases in switch statements can only be simple
integer types. Comparing strings should use a series of if-then-else statements.

This post has been edited by boblied: 20 February 2009 - 03:57 PM

Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: Having errors in switch statement

Posted 20 February 2009 - 03:57 PM

I don't know what I was thinking. Please disregard my above post.

string months[12] = {"January", "February", ...};
cin >> monthNum;
cout << months[monthNum-1];


This post has been edited by KYA: 20 February 2009 - 04:01 PM

Was This Post Helpful? 0
  • +
  • -

#11 HEDGE.c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 05-December 08

Re: Having errors in switch statement

Posted 20 February 2009 - 11:58 PM

View Postboblied, on 20 Feb, 2009 - 02:44 PM, said:

View PostKYA, on 20 Feb, 2009 - 01:57 PM, said:

You need to do some input before moving into the switch statement. And for a stirng you need to enclose the case in double quotes:

cin >> month; //since its a C++ string with no anticipated spaces we can use cin by itself
switch(month)
{
   case "January":
//etc...
}



You can't switch on a string in C++. The cases in switch statements can only be simple
integer types. Comparing strings should use a series of if-then-else statements.



this thing still didnt worked...can you write the code that will run???actually i tried t above 10 times but no result in coding ...


View PostKYA, on 20 Feb, 2009 - 02:57 PM, said:

I don't know what I was thinking. Please disregard my above post.

string months[12] = {"January", "February", ...};
cin >> monthNum;
cout << months[monthNum-1];




just use switch statement sir....
Was This Post Helpful? 0
  • +
  • -

#12 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Having errors in switch statement

Posted 21 February 2009 - 07:52 PM

Since switch statements do not support strings, your easiest solution is to resort to if else statements.

#include <cctype> //So we can convert string to all uppercase
#include <iostream>

using namespace std;

int main () {
  string month;

  cin >> month;
  month = toupper( month );//Convert string to all uppercase

  if ( month == "JANUARY" ) { cout << "January has 31 days\n"; }
  else if ( month == "FEBUARY" ) { cout << "Febuary has 28 days\n"; }
  else if ( month == "MARCH" ) { cout << "March has 31 days\n"; }
  //etc. etc. etc.

  else if ( month == "DECEMBER" ) { cout << "December has 31 days\n"; }
  else { cout << "Invalid Input\n"; }
}


This post has been edited by e_barroga: 21 February 2009 - 07:53 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1