1

i was trying to write a project in C but it is not working

can someone tell me why? and what do i need to change? i am trying to recive the day in week the birthday of a man will be next year after he tell me the date in this year( example 14/11 day 2(monday)

#include<stdio.h>

int day,month,day_in_week;

void main()
{
    printf("Enter the date of your birthday in 2015 (day,month, day in week):\n");
    scanf("%d%d%d",&day,&month,&day_in_week);

   if(day_in_week<1 || day_in_week>7)
   {
       printf("Error date entered");
       return;
   }
   if(month<1 || month>12)
   {
       printf("Error date entered");
       return;
   }
   switch(month)
   {
   case 2:
       if(day<1 || day>28)
              {
                  printf("Error date entered");
                  return;
              }
   case 1: case 3:case 5:case 7:case 8:case 10:case 12:
       if(day<1 || day>31)
              {
                  printf("Error date entered");
                  return;
              }
   case 4:case 6:case 9:case 11:
       if(day<1 || day>30)
              {
                  printf("Error date entered");
                  return;
              }
   }

   switch(month)
   {
   case 1:case 2:
           day_in_week++;
           if (day_in_week>7)
               day_in_week= day_in_week-7;
   case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12:
              day_in_week=day_in_week+2;
           if (day_in_week>7)
               day_in_week= day_in_week-7;
   }

   switch(day_in_week)
   {
   case 1:
       printf("Sunday");
   case 2:
       printf("Monday");
   case 3:
       printf("Tuesday");
   case 4:
       printf("Wednesday");
   case 5:
       printf("Thursday");
   case 6:
       printf("Friday");
   case 7:
       printf("Saturday");
   }
   getch();
   return 0;
}
elia333
  • 13
  • 5

3 Answers3

5

You should add break:

switch(month)
   {
   case 1:case 2:
           day_in_week++;
           if (day_in_week>7)
               day_in_week= day_in_week-7;
           break;
   case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12:
              day_in_week=day_in_week+2;
           if (day_in_week>7)
               day_in_week= day_in_week-7;
           break;
   }

Also you should do it in next switch too.
So, your code should looks like this:

#include<stdio.h>

int day,month,day_in_week;

void main()
{
    printf("Enter the date of your birthday in 2015 (day,month, day in week):\n");
    scanf("%d%d%d",&day,&month,&day_in_week);

   if(day_in_week<1 || day_in_week>7)
   {
       printf("Error date entered");
       return;
   }
   if(month<1 || month>12)
   {
       printf("Error date entered");
       return;
   }
   switch(month)
   {
   case 2:
       if(day<1 || day>28)
              {
                  printf("Error date entered");
                  return;
              }
   case 1: case 3:case 5:case 7:case 8:case 10:case 12:
       if(day<1 || day>31)
              {
                  printf("Error date entered");
                  return;
              }
   case 4:case 6:case 9:case 11:
       if(day<1 || day>30)
              {
                  printf("Error date entered");
                  return;
              }
   }

   switch(month)
   {
   case 1:case 2:
           day_in_week++;
           if (day_in_week>7)
               day_in_week= day_in_week-7;
           break;
   case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12:
              day_in_week=day_in_week+2;
           if (day_in_week>7)
               day_in_week= day_in_week-7;
           break;
   }

   switch(day_in_week)
   {
   case 1:
       printf("Sunday");
       break;
   case 2:
       printf("Monday");
       break;
   case 3:
       printf("Tuesday");
       break;
   case 4:
       printf("Wednesday");
       break;
   case 5:
       printf("Thursday");
       break;
   case 6:
       printf("Friday");
       break;
   case 7:
       printf("Saturday");
       break;
   }
   getch();
   return 0;
}

Here's a small help for you, to get it more clearly:

Switch case statements are a substitute for long if statements that compare a variable to several "integral" values. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point.

switch ( <variable> ) {
case this-value:
  Code to execute if <variable> == this-value
  break;
case that-value:
  Code to execute if <variable> == that-value
  break;
...
default:
  Code to execute if <variable> does not equal the value following any of the cases
  break;
}
tema
  • 1,115
  • 14
  • 33
  • Thanks, and about the "return" yes it will get out from all void main() which is what i need..( if the man was enter wrong number in the input..the program will finish – elia333 Apr 27 '15 at 14:58
2

A c switch command will keep running from one case to the next unless there is a break command at the end of the case.

switch (x) {
case 1:
  printf ( "case 1\n" );
//no break command, if x == 1, it keeps going
case 2:
  printf ( "case 1 and 2!\n" );
  break; // break command, it doesn't keep going to 3

case 3:
  printf ( "case 3 only\n" );
}
Politank-Z
  • 3,653
  • 3
  • 24
  • 28
2

The problem is because you do not have a break at the end of each case. Once you add break at the end of each one of your case ( at the very end ), that should fix it.

Arun A S
  • 6,421
  • 4
  • 29
  • 43