switch function error

Hi!I have a problem with the switch-case format.After using this function i have
reveived this error a couple of times:

error C2143: syntax error : missing ':' before ';'

I think in c++ 2008 they have changed the format of switch.
If that is so can someone tell me the correct format!


This is the referred program:

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
37
38
39
40
41
42
43
44
45
46
47
#include "BX40.h"

int citfig(FIG *p)
/*-reads  the elements of a geometrical object defined by p->tip;
  -returns:
              0-for EOF;
	     1-otherwise*/

{
	char t[255];
	switch(p->tip){
		case CERC:  /*reads the radius of a circle*/
			for( ; ; ){
				       printf("raza=");
					   if(gets(t) == 0)return 0;
					   if(sscanf(t,"%lf",&p->fig.raza)==1 &&
						   p->fig.raza>0)return 1;
					   printf("nu s-a tastat un numar pozitiv\n");
			          }
		case PATRAT:/*reads the side of a square*/
			for( ; ; ){
				       printf("latura patratului=");
					   if(gets(t) == 0)return 0;
					   if(sscanf(t,"%lf",&p->fig.lp)==1 &&
						   p->fig.lp>0)return 1;
					   printf("nu s-au tastat un numar pozitive\n");
			           }
        case DREPTUNGHI:/*reads the length and the width of a rectangle */
			for( ; ; ){
				printf("lungimea si latimea pe aceeasi linie:");
					   if(gets(t) == 0)return 0;
					   if(sscanf(t,"%lf %lf",&p->fig.ld[0],&p->fig.ld[1])==2 &&
						   p->fig.ld[0]>0 && p->fig.ld[1]>0)return 1;
					   printf("nu s-a tastat 2 numere pozitiv\n");
			          }
      case TRIUNGHI:/*reads the sides of a triangle*/
			for( ; ; ){
				printf("laturile triunghiului pe aceeasi linie ");
					   if(gets(t) == 0)return 0;
					   if(sscanf(t,"%lf %lf %lf",&p->fig.lt[0],&p->fig.lt[1,&p->fig.lt[2]])==3 &&
						   p->fig.lt[0]>0 && p->fig.lt[1]>0 && p->fig.lt[2]>0)return 1;
					   printf("nu s-au tastat 3 numere pozitive\n");
			         }
	  default:
		      return 0;
			}
			}




Last edited on
Could you please edit your original question and add the code to a CODE block? makes it much easier to read.
Also, do you get a line with the error message (in which error is it happening)?
Use code tags to display your code!! Surround any code you post like this:

[code]//your code[/code]

so it becomes:

//your code

crawler wrote:
I think in c++ 2008 they have changed the format of switch.
Not true. Documentation regarding switch statements can be found on the bottom of this page: http://www.cplusplus.com/doc/tutorial/control/

Just a few points.

You're code is almost indecipherable because of the lack of code tags and the formatting of your code and the code is also incomplete.

I'll try and point out the few problems I can spot.

You have missed the closing brace ("}") for the 2nd and third for loop.

You should never use an empty for loop! For loops are used when you need to iterate through code a predefined number of times. Use while loops instead as they are for iterations where you don't know the number of iterations required to complete the operation.

Your if statements are very long. You may want to consider wrapping them in an appropriately name temporary bool variable, for the sake of clarity.

You also may want to consider inserting a few comments so your code is a little clearer.

Try posting the your full code so we can get a good look at the code.

I hope this helps.
Aside!

I have to admit that I do use empty for loops!

While I try to avoid loops with constand conditions (while(true) and for( ; ; )), when I do use them I use for( ; ; ) as it avoids the "conditonal expression is constant" warning.

I don't think warnngs should ever be disabled globally, so the only other possibility is to disable the warning locally every time you use a while(true).

As I said, this shouldn't be that often. But code is cleaner with for( ; ; ) than the warning code.

For Visual C++ this is

1
2
3
4
5
6
7
8
9
#pragma warning(push)
#pragma warning(disable: 4127)

while(true)
{
    ...
}

#pragma warning(pop) 


I know later versions of gcc can do the same thing, but with their own syntax.

Andy
I have edited my original post.hopefully is clearer now.
I corrected the missing braces but the original error is still on.
The if statements are used to read the size of the geometrical figure.
While loops are likethis?: while()
Sorry if i am short in words,but my keyboard damaged and i am using on-screen keyboard:)
Yes -- where you used for( ; ; ) you could use while(true) instead.

If the error still occurs, try commenting out half the cases in the switch statement. Etc. Til you find where the problem is.

You didn't include the line number with the error code, so I can't say any more!

Andy
i get the error for case CERC:,case PATRAT:,case DREPTUNGHI:,case TRIUNGHI: lines
Last edited on
Is there a missing header?
Your problem may be in the CERC, PATRAT, DREPTUNGHI and TRIUNGHI definitions. Please post the code where you are defining them e.g.

1
2
3
4
5
#define CERC (someValue)
#define PATRAT (someValue)
#define DREPTUNGHI (someValue)
#define TRIUNGHI (someValue)
1
2
3
4
5

#define CERC 1
#define PATRAT 2
#define DREPTUNGHI 3
#define TRIUNGHI 4 
Topic archived. No new replies allowed.