Case statement help

Month output is not right for any number over 9

Page 1 of 1

13 Replies - 8370 Views - Last Post: 14 February 2009 - 04:23 AM Rate Topic: -----

#1 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Case statement help

Post icon  Posted 25 October 2006 - 06:40 PM

I am creating a C program that excepts both month and day value. I am working on the 1st part with the month. However if I input a month number 1-9 it out puts the correct month. When I input 10,11, or 12 it gives me January. I did an F5 to see that the message I would receive, and here is what I received, "he thread 0xF94 has exited with code 0 (0x0).
The program 'C:\Documents and Settings\XXXXl\Desktop\assignments\Month&Day\Debug\month_day.exe' has exited with code 0 (0x0).

I commented out the request for "day"

Below is what I have so far. Any help is appreciated.

#include <stdio.h>
#include <conio.h>


int main()

{
	char month;
	
	
	printf("Enter Month:");
	scanf("%c ",&month);
	//printf("Enter Day:");
	//scanf("%c ",&day);

	switch (month)
	{
	case '1':
		printf("\nMonth is January\n");
		break;
	case '2':
		printf("\nMonth is February\n"); 
		break;
	case '3':
		printf("\nMonth is March\n");
		break;
	case '4':
		printf("\nMonth is April\n");
		break;
	case '5':
		printf("\nMonth is May\n");
		break;
	case '6':
		printf("\nMonth is June\n");
		break;
	case '7':
		printf("\nMonth is July\n");
		break;
	case '8':
		printf("\nMonth is August\n");
		break;
	case '9':
		printf("\nMonth is September\n");
		break;
	case '10':
		printf("\nMonth is October\n");
		break;
	case '11':
		printf("\nMonth is November\n");
		break;
	case '12':
		printf("\nMonth is December\n");
		break;
	default:
		printf("\nIncorrect Data Entered!\n");
		break;
	}
	getch ();


return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Case statement help

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Case statement help

Posted 25 October 2006 - 06:52 PM

The problem is that you are using a character data type. This will only allow for ONE character to be entered. So '1' is valid while '12' is not valid.

Change you data type to integer and remove the single quotes from around the values in the cases and you will get your expected result.
Was This Post Helpful? 0
  • +
  • -

#3 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Re: Case statement help

Posted 25 October 2006 - 07:05 PM

it's now saying incorrect data. It's bypassing the case and going right to the default if I put 10, 11, or 12..anything under it will not do anything. All it does is enter down to the next line. I have to either x out or enter any number over 9

I think it's my scanf statement....If I change it to int I either do not get an output, or the months are not the right one (example output 12 is May)..

This post has been edited by jona431: 25 October 2006 - 07:08 PM

Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Case statement help

Posted 25 October 2006 - 07:08 PM

Did you remember to change the format specifier in your scanf statement?
 scanf("%d ",&month);


Was This Post Helpful? 0
  • +
  • -

#5 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Re: Case statement help

Posted 25 October 2006 - 07:12 PM

yep...here is what I have now.





#include <stdio.h>
#include <conio.h>


int main()

{
	int month;
	
	
	printf("Enter Month:");
	scanf("%d ",&month);
	//printf("Enter Day:");
	//scanf("%c ",&day);

	switch (month)
	{
	case 1:
		printf("\nMonth is January\n");
		break;
	case 2:
		printf("\nMonth is February\n"); 
		break;
	case 3:
		printf("\nMonth is March\n");
		break;
	case 4:
		printf("\nMonth is April\n");
		break;
	case 5:
		printf("\nMonth is May\n");
		break;
	case 6:
		printf("\nMonth is June\n");
		break;
	case 7:
		printf("\nMonth is July\n");
		break;
	case 8:
		printf("\nMonth is August\n");
		break;
	case 9:
		printf("\nMonth is September\n");
		break;
	case 10:
		printf("\nMonth is October\n");
		break;
	case 11:
		printf("\nMonth is November\n");
		break;
	case 12:
		printf("\nMonth is December\n");
		break;
	default:
		printf("\nIncorrect Data Entered!\n");
		break;
	}
	getch ();


return 0;
}

Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Case statement help

Posted 25 October 2006 - 07:15 PM

Ah, I see now.

Remove the space from inside the scanf statement, the one after the format specifier.
scanf("%d",&month);


Was This Post Helpful? 0
  • +
  • -

#7 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Re: Case statement help

Posted 25 October 2006 - 07:22 PM

doh.....I completely overlooked that...

:blink: Homer moment...



Thanks Jayman
Was This Post Helpful? 0
  • +
  • -

#8 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Re: Case statement help

Posted 26 October 2006 - 03:42 PM

got another question on this project. I need to have it request both month and day. I already have the statement and scanf for the day, however I was told it is possible to do a if statement in each case. Please let me know if this is possible. I have attempted, but I'm not sure how and no examples in the book. Any suggestions. Thank you
Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Case statement help

Posted 26 October 2006 - 04:12 PM

What is supposed to happen when the day is entered by the user?
Was This Post Helpful? 0
  • +
  • -

#10 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Re: Case statement help

Posted 26 October 2006 - 04:29 PM

to be honest not really sure...here is what it says

" write a C program that request both month and say values. Only month values between 1 and 12 should be accepted. Day values between 1 and 28 for February, and so with each month there after. and it gives a hint to use a case statement which I did. Here is the if statement I wrote within the case statement(just for February so far, and seems to be working)...please let me know if I am on the right track :)

it appears to work, however if I enter 13 as the month it still asks for the day, I want it to go right to the default. Do I need to put that in a statement after the scanf?

#include <stdio.h>
#include <conio.h>


int main()

{
	int month, day;
	
	
	printf("Enter Month:");
	scanf("%d",&month);
	printf("Enter Day:");
	scanf("%d ",&day);

	switch (month)
	{
	case 1:
		printf("\nMonth is January\n");
		break;
	case 2:
		if (day>=29)
		printf("More days entered than in month!",day);
		else
		printf("\nMonth is February\n"); 
		break;
	case 3:
		printf("\nMonth is March\n");
		break;
	case 4:
		printf("\nMonth is April\n");
		break;
	case 5:
		printf("\nMonth is May\n");
		break;
	case 6:
		printf("\nMonth is June\n");
		break;
	case 7:
		printf("\nMonth is July\n");
		break;
	case 8:
		printf("\nMonth is August\n");
		break;
	case 9:
		printf("\nMonth is September\n");
		break;
	case 10:
		printf("\nMonth is October\n");
		break;
	case 11:
		printf("\nMonth is November\n");
		break;
	case 12:
		printf("\nMonth is December\n");
		break;
	default:
		printf("\nIncorrect Data Entered!\n");
		break;
	}
	getch ();


return 0;
}


edit: fixed [code] tags ~ jayman9

This post has been edited by jayman9: 26 October 2006 - 04:48 PM

Was This Post Helpful? 0
  • +
  • -

#11 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Case statement help

Posted 26 October 2006 - 04:44 PM

Based on your assignment, I would say yes your are on the right track.

You can solve that problem by putting your print and scanf statement inside an IF statement right after you get the user input for the month. If user enters incorrect month will bypass next prompt and go straight to default.

printf("Enter Month:");
scanf("%d",&month);
if(month > 12 || month < 1)
{
	printf("Enter Day:");
	scanf("%d ",&day);
}

Was This Post Helpful? 0
  • +
  • -

#12 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Case statement help

Posted 26 October 2006 - 04:58 PM

Just realized I had the <> symbols backwards should be like this:
printf("Enter Month:");
scanf("%d",&month);
if(month > 0 && month < 13)
{
	printf("Enter Day:");
	scanf("%d ",&day);
}

Was This Post Helpful? 0
  • +
  • -

#13 jona431  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 08-April 06

Re: Case statement help

Posted 26 October 2006 - 05:27 PM

np..I didn't catch it right away either...lol

I did not know you could add just the if to the scanf to bypass like this.

works great..thanks again.

This post has been edited by jona431: 26 October 2006 - 05:35 PM

Was This Post Helpful? 0
  • +
  • -

#14 bigfootmatthew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-February 09

Re: Case statement help

Posted 14 February 2009 - 04:23 AM

This was my very first time using both dev c++ and visual c++ and this code works in both c++ programs and even displayes october, november and december.
#include "iostream"
	#include <stdio.h>
#include <conio.h>
using namespace std;


int main()

{
	int month;
	
	
	printf("Enter Month:");
	cin>>month;

	switch (month)
	{
	case 1:
		printf("\nMonth is January\n");
		break;
	case 2:
		printf("\nMonth is February\n"); 
		break;
	case 3:
		printf("\nMonth is March\n");
		break;
	case 4:
		printf("\nMonth is April\n");
		break;
	case 5:
		printf("\nMonth is May\n");
		break;
	case 6:
		printf("\nMonth is June\n");
		break;
	case 7:
		printf("\nMonth is July\n");
		break;
	case 8:
		printf("\nMonth is August\n");
		break;
	case 9:
		printf("\nMonth is September\n");
		break;
	case 10:
		printf("\nMonth is October\n");
		break;
	case 11:
		printf("\nMonth is November\n");
		break;
	case 12:
		printf("\nMonth is December\n");
		break;
	default:
		printf("\nIncorrect Data Entered!\n");
		break;
	}
   

system("pause");

return 0;
}
 

hope this code will help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1