expected primary-expression before '>=' token

Hello World, I am new to c++ and can you help me with this program. Is says that there needs to be a primary expression before '>=' token on line 21.
I am using Dev-CPP

Thanks,
usandfriends



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* summer.c
 * ---------------
 * This program reads a month number and checks if it
 * is in the months of summer.
 */
 
#include <stdio.h>
#include "simpio.h"
#include "genlib.h"

main()
{
      int month;
      printf ("This program reads a month number and checks if it is in the months of summer.\n");
      printf ("Enter a month number.\n");
       switch (month)
              {
                     case 6: printf ("June is in summer.\n"); break;
                     case 7: printf ("July is in summer.\n"); break;
                     case 8: printf ("August is in summer.\n"); break;
                     case (>=1) && (<=12): printf ("%d is not in summer.\n", month); break;
                     default: printf ("%d is out of range.\n", month); break;
              }
       
getchar();
}

You can't have a condition inside of a case statement. You have to simply list all the cases out or embed an if statement inside the default case.
Is this good? Thanks BTW

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* summer.c
 * ---------------
 * This program reads a month number and checks if it
 * is in the months of summer.
 */
 
#include <stdio.h>
#include "simpio.h"
#include "genlib.h"

main()
{
      int month;
      printf ("This program reads a month number and checks if it is in the months of summer.\n");
      printf ("Enter a month number.\n");
      month = GetInteger();
       switch (month)
              {
                     case 1: printf ("January is not in summer.\n"); break;
                     case 2: printf ("February is not in summer.\n"); break;
                     case 3: printf ("March is not in summer.\n"); break;
                     case 4: printf ("April is not in summer.\n"); break;
                     case 5: printf ("May is not in summer.\n"); break;
                     case 6: printf ("June is not in summer.\n"); break;
                     case 7: printf ("July is in summer.\n"); break;
                     case 8: printf ("August is in summer.\n"); break;
                     case 9: printf ("September is not in summer.\n"); break;
                     case 10: printf ("October is not in summer.\n"); break;
                     case 11: printf ("November is not in summer.\n"); break;
                     case 12: printf ("December is not in summer.\n"); break;
                     default: printf ("%d is out of range.\n"); break;
              }
       
getchar();
}
Last edited on
That should work, though you could also just run all the case statements together.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch(x) {
    case 1:
        // do stuff
        break;
    case 2:
        // do stuff
        break;
    case 3:
    case 4:
    case 5:
        // do same stuff for some cases
        break;
    default:
        // do stuff
}
yeah i did that after i read about that. thanks
Topic archived. No new replies allowed.