•
•
•
•
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
![]() |
•
•
Join Date: Jul 2008
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
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:
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:
cplusplus Syntax (Toggle Plain Text)
#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; }
Last edited by Ancient Dragon : 27 Days Ago at 6:24 pm. Reason: add code tags
•
•
Join Date: Jan 2008
Posts: 1,405
Reputation:
Rep Power: 6
Solved Threads: 179
•
•
•
•
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.
•
•
Join Date: Feb 2008
Location: Inside main function
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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
2) Your algorithm isn't right. Please visit here ^^ http://en.wikipedia.org/wiki/Leap_year
C++ Syntax (Toggle Plain Text)
#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 2: cout << "Please enter the year.\n"; cin >> year; if ( (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0) ) cout << "February has 29 days. This is a Leap year\n"; else cout << "February has 28 days. \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; 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; }
•
•
Join Date: Jul 2008
Posts: 60
Reputation:
Rep Power: 1
Solved Threads: 3
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");And by using
#include <conio.h>
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
•
•
Join Date: Jan 2008
Location: USA East Cost
Posts: 386
Reputation:
Rep Power: 1
Solved Threads: 37
•
•
•
•
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:This will print the line, 'Press any key to continue...', so the user doesn't have to press enter.system("pause");
And by usingand, for example,#include <conio.h>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...".
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
api apple asm assembly x86 programming hla demo blogger blogging c c++ ccna cocoa code coding competition compiler computer debugging desktop developer developers development evaluation framework gdata google high-performance innovation java linerider linux mcse microsystems networking news next object online oriented planning platform programmers programming python rss software step steps sun tools tutorials windows xml
- Function being "skipped" in switch statement (C++)
- Just to say "Hi everyone..." (Community Introductions)
- How to "delete" this memory??? (C++)
- "Craps", Game Help (C++)
Other Threads in the C++ Forum
- Previous Thread: Visial C++ again
- Next Thread: Moore Curve Homework Help



Linear Mode