-2
#include <iostream>
using namespace std;
int main(){
    cout<<"What year are you in"<<endl;
    int a(0);
    cin>>a;
    int b(a % 4);
    cout<<"Enter an index for one of the 12 months of the year:"<< endl;
   int month{0};
   cin>>month;
   switch(month){
   case 1:
   cout<<"January is 31 days";
   break;
   case 2:{
   if(b=0){
   cout<<"February is 29 days";} 
   break;}
   case 3:{
   if (b!=0){
   cout<<"February is 28 days";}
   break;
   }
   case 4:
   cout<<"March is 31 days";
   break;
   case 5:
   cout<<"April is 30 days";
   break;
   case 6:
   cout<<"May is 31 days";
   break;
   case 7:
   cout<<"June is 30 days";
   break;
   case 8:
   cout<<"July is 31 days";
   break;
   case 9:
   cout<<"August is 31 days";
   break;
   case 10:
   cout<<"September is 30 days";
   break;
   case 11:
   cout<<"October is 30 days";
   break;
   case 12:
   cout<<"November is 30 days";
   break;
   case 13:
   cout<<"December is 31 days";
   break;
   default:
   cout<<"Not a valid index";
   }  
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    return 0;
}






















Write a C program that takes in the index of a month (1 for January, 2 for February, ...etc.) and displays how many days there are in that month. Now modify the above code, this time accounting for leap years (i.e., when February is 29 days instead of 28).

I was trying to display to day of the months and i made it but when deciding february is 28 or 29 i'm stuck and i don't know what to do here please help me.I don't know if im using modulus wrong or maybe paranthesis. How do i make this work?

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • Please **please** use your code editor's "format code" function. this is **very** tiring to read, and a tiny bit of automatic indentation would fix that – and also, and that's very important, help you see any logical bugs. Also, you're using self-contradicting tags. Your code is either C++17, C++14 or C++11. Since it's doesn't even use *any* functionality introduced by C++11 or later, removing all tags but [tag:c++], since they are misleading. – Marcus Müller Nov 06 '22 at 10:02
  • "I'm stuck" is also not very helpful. Where exactly are you stuck? what does your program do that it shouldn't? You haven't actually told us any problem with this! – Marcus Müller Nov 06 '22 at 10:04
  • (hint: this is probably just a little mistake you made while putting code in the wrong place, and again, **automatic code formatting** would have made that mistake very obvious! Automatic code formatting is your friend. Professional programmers use it all the time, while writing their code, because it makes mistakes so so so much easier to spot.) – Marcus Müller Nov 06 '22 at 10:05
  • https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems?r=Saves_AllUserSaves – πάντα ῥεῖ Nov 06 '22 at 10:06
  • If you want extra test cases, try the years 1900 and 2000, which should have *different* results. – BoP Nov 06 '22 at 10:31
  • `if(b=0)` is assigning `0` to `b`, not comparing. The statement is always `false` since it's testing the value of `b` after the assignment, and that's always `false`. You want `if (b == 0)` . – Jesper Juhl Nov 06 '22 at 10:53
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Nov 06 '22 at 10:54
  • Jesper Juhl thank you very much i didn't know that i need to use == to compare them. – Anixton Nov 06 '22 at 16:14

1 Answers1

1

Firstly, you missed a } at the end of your code, along with the almighty return 0.

After that, the main problem was just the usage of = instead of == when testing for b, as well with using case 3: when determining for February as well.

int main()
{
 ...
    case 2:
        {
            if (b == 0)
            {
                cout << "February is 29 days";
            }
            else {
                cout << "February is 28 days";  
            }
            break;
        }
    ...
    return 0;
}
Mike4544
  • 123
  • 1
  • 6
  • 1
    `return 0;` and the closing bracket are there, you just got a weirdly large number of blank lines in the code block. Also for the `main` function you don't actually need a `return` statement; for this specific function reaching the end on the function without encountering a return statement has the same effect as returning `return 0;` at the end of the function, see https://en.cppreference.com/w/cpp/language/main_function . – fabian Nov 06 '22 at 10:19
  • My bad then, guess I have to refresh my memory a tad. – Mike4544 Nov 06 '22 at 10:21
  • 1
    `return` is optional in `main()`. If you leave it out it is defined to return `0`. – Jesper Juhl Nov 06 '22 at 10:50