User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,929 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,685 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 191 | Replies: 6
Reply
Join Date: Jul 2008
Posts: 2
Reputation: danieldan is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
danieldan danieldan is offline Offline
Newbie Poster

New to C++ and programming: PLEASE HELP!!!!! with "Switch"

  #1  
27 Days Ago
Hellp all! Hoping to make new friends here:

Anyways,

I am new to C++ and to programming. I just started reading the book "Problem Solving with C++, 7th ed." by Walter Savitch. I am already having problems. One of the programs I am trying to figure out (not in the book) is how to figure for how many days a month has. The problem I am having is with leap year (Feb). Here is what I have so far:
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int Month, Year;
  6.  
  7. cout << "Enter a number matching the month \n";
  8. cout << "of the year, then press return\n";
  9. cin >> Month;
  10. cout << " \n";
  11.  
  12. switch (Month)
  13. {
  14. case 1:
  15. cout << "There are 31 days in January.\n";
  16. break;
  17. case 3:
  18. cout << "There are 31 days in March.\n";
  19. break;
  20. case 4:
  21. cout << "There are 30 days in April.\n";
  22. break;
  23. case 5:
  24. cout << "There are 31 days in May.\n";
  25. break;
  26. case 6:
  27. cout << "There are 30 days in June.\n";
  28. break;
  29. case 7:
  30. cout << "There are 31 days in July.\n";
  31. break;
  32. case 8:
  33. cout << "There are 31 days in August.\n";
  34. break;
  35. case 9:
  36. cout << "There are 30 days in September.\n";
  37. break;
  38. case 10:
  39. cout << "There are 31 days in October.\n";
  40. break;
  41. case 11:
  42. cout << "There are 30 days in November.\n";
  43. break;
  44. case 12:
  45. cout << "There are 31 days in December.\n";
  46. break;
  47.  
  48. case 2:
  49. cout << "Please enter the year.\n";
  50. cin >> Year;
  51. break;
  52.  
  53. if (Year % 4 == 0)
  54. cout << "February has 29 days. This is a Leap Year\n";
  55. else
  56. cout << "February has 28 days. \n";
  57. break;
  58.  
  59. default:
  60. cout << "Please select from 1-12. There are only 12 months in the year. Thank you.\n";
  61. }
  62.  
  63. cout << " \n";
  64.  
  65. char letter;
  66. cout << "Enter a letter to end the program:\n";
  67. cin >> letter;
  68.  
  69. cout << "Goodbye.\n";
  70.  
  71. return 0;
  72. }
Last edited by Ancient Dragon : 27 Days Ago at 6:24 pm. Reason: add code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2008
Posts: 1,405
Reputation: VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough 
Rep Power: 6
Solved Threads: 179
VernonDozier VernonDozier is offline Offline
Nearly a Posting Virtuoso

Re: New to C++ and programming: PLEASE HELP!!!!! with "Switch"

  #2  
27 Days Ago
Originally Posted by danieldan View Post
Hellp all! Hoping to make new friends here:

Anyways,

I am new to C++ and to programming. I just started reading the book "Problem Solving with C++, 7th ed." by Walter Savitch. I am already having problems. One of the programs I am trying to figure out (not in the book) is how to figure for how many days a month has. The problem I am having is with leap year (Feb). Here is what I have so far:

#include <iostream>
using namespace std;
int main()
{
int Month, Year;

cout << "Enter a number matching the month \n";
cout << "of the year, then press return\n";
cin >> Month;
cout << " \n";

switch (Month)
{
case 1:
cout << "There are 31 days in January.\n";
break;
case 3:
cout << "There are 31 days in March.\n";
break;
case 4:
cout << "There are 30 days in April.\n";
break;
case 5:
cout << "There are 31 days in May.\n";
break;
case 6:
cout << "There are 30 days in June.\n";
break;
case 7:
cout << "There are 31 days in July.\n";
break;
case 8:
cout << "There are 31 days in August.\n";
break;
case 9:
cout << "There are 30 days in September.\n";
break;
case 10:
cout << "There are 31 days in October.\n";
break;
case 11:
cout << "There are 30 days in November.\n";
break;
case 12:
cout << "There are 31 days in December.\n";
break;

case 2:
cout << "Please enter the year.\n";
cin >> Year;
break;

if (Year % 4 == 0)
cout << "February has 29 days. This is a Leap Year\n";
else
cout << "February has 28 days. \n";
break;

default:
cout << "Please select from 1-12. There are only 12 months in the year. Thank you.\n";
}

cout << " \n";

char letter;
cout << "Enter a letter to end the program:\n";
cin >> letter;

cout << "Goodbye.\n";

return 0;
}


	case 2:
			cout << "Please enter the year.\n";
			cin >> Year;
			break;   // delete this line

	if (Year % 4 == 0) 
		cout << "February has 29 days. This is a Leap Year\n";
	else
		cout << "February has 28 days. \n";
	break;

You are breaking out of the code to be executed for February too soon. The code after the first break is not executed, so the if statement is never executed. You already have a break at the end, so delete the first one.
Reply With Quote  
Join Date: Feb 2008
Location: Inside main function
Posts: 5
Reputation: Emerald214 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Emerald214's Avatar
Emerald214 Emerald214 is offline Offline
Newbie Poster

Re: New to C++ and programming: PLEASE HELP!!!!! with "Switch"

  #3  
27 Days Ago
1) Remember to wrap the code with tag \[code\]\[\/code\] next time.
2) Your algorithm isn't right. Please visit here ^^ http://en.wikipedia.org/wiki/Leap_year

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int month, year;
  6.  
  7. cout << "Enter a number matching the month \n";
  8. cout << "of the year, then press return\n";
  9. cin >> month;
  10. cout << " \n";
  11.  
  12. switch (month)
  13. {
  14. case 1:
  15. cout << "There are 31 days in January.\n";
  16. break;
  17.  
  18. case 2:
  19. cout << "Please enter the year.\n";
  20. cin >> year;
  21.  
  22. if ( (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0) )
  23. cout << "February has 29 days. This is a Leap year\n";
  24. else
  25. cout << "February has 28 days. \n";
  26. break;
  27.  
  28. case 3:
  29. cout << "There are 31 days in March.\n";
  30. break;
  31.  
  32. case 4:
  33. cout << "There are 30 days in April.\n";
  34. break;
  35.  
  36. case 5:
  37. cout << "There are 31 days in May.\n";
  38. break;
  39.  
  40. case 6:
  41. cout << "There are 30 days in June.\n";
  42. break;
  43.  
  44. case 7:
  45. cout << "There are 31 days in July.\n";
  46. break;
  47.  
  48. case 8:
  49. cout << "There are 31 days in August.\n";
  50. break;
  51.  
  52. case 9:
  53. cout << "There are 30 days in September.\n";
  54. break;
  55.  
  56. case 10:
  57. cout << "There are 31 days in October.\n";
  58. break;
  59.  
  60. case 11:
  61. cout << "There are 30 days in November.\n";
  62. break;
  63.  
  64. case 12:
  65. cout << "There are 31 days in December.\n";
  66. break;
  67. default:
  68. cout << "Please select from 1-12. There are only 12 months in the year. Thank you.\n";
  69. }
  70.  
  71. cout << " \n";
  72.  
  73. char letter;
  74. cout << "Enter a letter to end the program:\n";
  75. cin >> letter;
  76.  
  77. cout << "Goodbye.\n";
  78.  
  79. return 0;
  80. }
Reply With Quote  
Join Date: Jul 2008
Posts: 60
Reputation: Wiki_Tiki is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 3
Wiki_Tiki Wiki_Tiki is offline Offline
Junior Poster in Training

Re: New to C++ and programming: PLEASE HELP!!!!! with "Switch"

  #4  
27 Days Ago
cout << " \n"; 
char letter;	

cout << "Enter a letter to end the program:\n";
cin >> letter;
cout << "Goodbye.\n"; 
return 0;
}

I know this probably isn't going to be much help to you, but it's way easier and simpler to, instead of having to type a random letter and press enter, you can either use:
system("pause");
This will print the line, 'Press any key to continue...', so the user doesn't have to press enter.

And by using
#include <conio.h>
and, for example,
cout << "Press any key to do something";
_getch();

That allows you to enter your own custom message to the user, before instantly exiting the program on the key press.
- The C++ fatal n00b
Reply With Quote  
Join Date: Jan 2008
Location: USA East Cost
Posts: 386
Reputation: CoolGamer48 is on a distinguished road 
Rep Power: 1
Solved Threads: 37
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Whiz

Re: New to C++ and programming: PLEASE HELP!!!!! with "Switch"

  #5  
27 Days Ago
Originally Posted by Wiki_Tiki View Post
cout << " \n"; 
char letter;	

cout << "Enter a letter to end the program:\n";
cin >> letter;
cout << "Goodbye.\n"; 
return 0;
}

I know this probably isn't going to be much help to you, but it's way easier and simpler to, instead of having to type a random letter and press enter, you can either use:
system("pause");
This will print the line, 'Press any key to continue...', so the user doesn't have to press enter.

And by using
#include <conio.h>
and, for example,
cout << "Press any key to do something";
_getch();

That allows you to enter your own custom message to the user, before instantly exiting the program on the key press.

cin.get() works. it doesn't disrupt the program like system("PAUSE"); and you only need to #include <iostream>
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote  
Join Date: Jul 2008
Posts: 2
Reputation: danieldan is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
danieldan danieldan is offline Offline
Newbie Poster

Re: New to C++ and programming: PLEASE HELP!!!!! with "Switch"

  #6  
27 Days Ago
AWESOME!!! Thanks! Thanks!!! Don't know how much I was starting to bust my head with this!!!!! Gonna make some good friends here!!! Glad I joined
Reply With Quote  
Join Date: Jul 2008
Posts: 60
Reputation: Wiki_Tiki is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 3
Wiki_Tiki Wiki_Tiki is offline Offline
Junior Poster in Training

Re: New to C++ and programming: PLEASE HELP!!!!! with "Switch"

  #7  
27 Days Ago
Glad we could help
Last edited by Wiki_Tiki : 27 Days Ago at 11:25 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:25 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC