Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Newb to C++ needs help (http://www.go4expert.com/forums/newb-cpp-help-t12431/)

 danieldan 25Jul2008 12:50

Newb to C++ needs help

hello all! I'm a beginner to programming and am currently working on learning C++ program. I bought a book titled "Problem Solving with C++, 7th ed." by Walter Savitch but am stuck with a C++ problem (not in the book).

I'm trying to make my conditions program figure out how many days there are in a month; it also figures for leap year. The leap year part works, but I cannot make the rest to work out properly. If I enter anything other than 2 (for Feb), it will not end properly. It will give the right days of the month but will not end properly.

Can anyone help me out with my code??? Here is the code:
====================

Code:

```#include <iostream>  using namespace std;  int main()  {      int Month_Number, Year;        cout << "Enter a number matching the month \n";      cout << "of the year, then press return\n";      cin >> Month_Number;      cout << " \n";      {        if ( Month_Number == 1)          cout << "The month has 31 days,\n";      else if ( Month_Number == 3)          cout << "The month only has 31 days.\n";      else if ( Month_Number == 4)          cout << "The month only has 30 days.\n";        else if ( Month_Number == 5)          cout << "The month only has 31 days.\n";      else if ( Month_Number == 6)          cout << "The month only has 30 days.\n";        else if ( Month_Number == 7)          cout << "The month only has 31 days.\n";      else if ( Month_Number == 8)          cout << "The month only has 31 days.\n";        else if ( Month_Number == 9)          cout << "The month only has 30 days.\n";      else if ( Month_Number == 10)          cout << "The month only has 31 days.\n";        else if ( Month_Number == 11)          cout << "The month only has 30 days.\n";      else if ( Month_Number == 12)          cout << "The month only has 31 days.\n";      }        if (Month_Number == 2)          cout << "Please enter the year.\n";                    cin >> Year;          if (Year % 4 == 0)                        cout << "February has 29 days. This is a Leap year\n";                      else                              cout << "February has 28 days. \n";            system("pause");        return 0;  }```

 xpi0t0s 30Jul2008 00:16

Re: Newb to C++ needs help

You haven't said what "doesn't end properly" means, but I guess you're puzzled why it prompts you for the year without printing "Please enter the year." Or you're assuming that the prompt is some kind of hang.

See if you can see what's missing between "if (Month_Number == 2)" and "cout << "Please enter the year.\n";", and after "cout << "February has 28 days. \n";" Clue: you have unnecessary ones around your if/else if ladder. If you need another clue, have you been Python programming as well recently?

 sofprog66 1Aug2008 03:09

Re: Newb to C++ needs help

I would recommend using a switch statement because all of the if statements can get real messy.

 All times are GMT +5.5. The time now is 19:32.