Weird Problem with Declarations

Even though my code is in C not C++, the error is universal I would think, but for some reason I wrote this code in CodeBlocks but am not moving it into VB 2008 Prof edition. It works in CodeBlocks but not in VB. Here is the code:

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

/*  Name: Matt Hintzke
    Date: 8/29/2011
    Class: CptS 121
*/

#include <stdio.h>
#include <math.h>
#define PI 3.14159
#define HYDROGEN_MASS 1.008
#define PHOSPHORUS_MASS 30.97
#define OXYGEN_MASS 16

int main()
{
    //Newton's Second Law
    double mass, acceleration, force;
    printf("Please enter the mass and acceleration to be used in Newton's Second Law of Motion:\n");
    printf("Mass: ");
    scanf("%f", &mass);
    printf("Acceleration: ");
    scanf("%f", &acceleration);
    force = mass*acceleration;
    printf("Force = mass * acceleration = %f * %f = %f\n\n", mass, acceleration, force);

    //Volume of a Cylinder
    double volume_cylinder, radius, height;
    printf("Please enter the radius and height to find the volume of a cylinder.\n");
    printf("Radius: ");
    scanf("%f", &radius);
    printf("Height: ");
    scanf("%f", &height);
    volume_cylinder = PI*radius*radius*height;
    printf("\nThe volume of the cylinder is Volume = PI*r^2*h = %f\n\n", volume_cylinder);

    //Character Encoding
    char plain_character, encoded_character;
    printf("Please input a single character: ");
    scanf(" %c", &plain_character);
    encoded_character = plain_character+4;
    printf("The encoded character is '%c'\n\n", encoded_character);

    //Height of a projectile
    float height_proj, time, initial_height, initial_vel;
    printf("Please enter the following information to find the height of a projectile: \n");
    printf("Time(sec): ");
    scanf("%f", &time);
    printf("Initial Height: ");
    scanf("%f", &initial_height);
    printf("Initial Velocity: ");
    scanf("%f", &initial_vel);
    height_proj = -16*time*time+initial_vel*time+initial_height;
    printf("The height of the projectile after %f seconds have passed is %f\n\n", time, height_proj);

    //Current
    float current, power, resistance;
    printf("Please enter the following information to find the current of a circuit: \n");
    printf("Power: ");
    scanf("%f", &power);
    printf("Resistance: ");
    scanf("%f", &resistance);
    current = sqrt((power/resistance));
    printf("The current of the circuit is %f amps\n\n", current);

    //General Equation
    float y, x, z;
    int a;
    printf("For the equation y=(3/4)x-z/(a//%2)+PI please enter the following information: \n");
    printf("x: ");
    scanf("%f", &x);
    printf("z: ");
    scanf("%f", &z);
    printf("a: ");
    scanf("%d", &a);
    y=(3/4)*x-(z/(a%2))+PI;
    printf("%f %f %d", x, z, a);
    printf("For the given equation y = %f", y);

    return 0;
}


I am getting 45 errors all saying that the declarations of my variables are not valid:

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
Error	3	error C2143: syntax error : missing ';' before 'type'	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	26	Assignment1
Error	4	error C2065: 'radius' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	29	Assignment1
Error	5	error C2065: 'height' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	31	Assignment1
Error	6	error C2065: 'volume_cylinder' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	32	Assignment1
Error	7	error C2065: 'radius' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	32	Assignment1
Error	8	error C2065: 'radius' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	32	Assignment1
Error	9	error C2065: 'height' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	32	Assignment1
Error	11	error C2065: 'volume_cylinder' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	33	Assignment1
Error	12	error C2143: syntax error : missing ';' before 'type'	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	36	Assignment1
Error	13	error C2065: 'plain_character' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	38	Assignment1
Error	14	error C2065: 'encoded_character' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	39	Assignment1
Error	15	error C2065: 'plain_character' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	39	Assignment1
Error	16	error C2065: 'encoded_character' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	40	Assignment1
Error	17	error C2143: syntax error : missing ';' before 'type'	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	43	Assignment1
Error	18	error C2065: 'time' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	46	Assignment1
Error	19	error C2065: 'initial_height' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	48	Assignment1
Error	20	error C2065: 'initial_vel' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	50	Assignment1
Error	21	error C2065: 'height_proj' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	51	Assignment1
Error	22	error C2065: 'time' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	51	Assignment1
Error	23	error C2065: 'time' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	51	Assignment1
Error	24	error C2065: 'initial_vel' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	51	Assignment1
Error	25	error C2065: 'time' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	51	Assignment1
Error	26	error C2065: 'initial_height' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	51	Assignment1
Error	27	error C2065: 'time' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	52	Assignment1
Error	28	error C2065: 'height_proj' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	52	Assignment1
Error	29	error C2143: syntax error : missing ';' before 'type'	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	55	Assignment1
Error	30	error C2065: 'power' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	58	Assignment1
Error	31	error C2065: 'resistance' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	60	Assignment1
Error	32	error C2065: 'current' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	61	Assignment1
Error	33	error C2065: 'power' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	61	Assignment1
Error	34	error C2065: 'resistance' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	61	Assignment1
Error	36	error C2065: 'current' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	62	Assignment1
Error	37	error C2143: syntax error : missing ';' before 'type'	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	65	Assignment1
Error	38	error C2143: syntax error : missing ';' before 'type'	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	66	Assignment1
Error	39	error C2065: 'x' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	69	Assignment1
Error	40	error C2065: 'z' : undeclared identifier	c:\users\matt\documents\visual studio 2008\projects\assignment1\assignment1\main.c	71	Assignment1

No idea why this is happening. There are more, I just can't put them all in this content box.
I can see there are syntax errors on lines 26, 36, 43, 55, 65, 66 -- these are all comment lines.

Does this compiler have a problem with C++ style comments??


Sheesh! I missed the obvious C-ism! => see shacktar's following post.

(Actually, after seeing lots of horrible C-style declaration in supposedly C++ files, I was pleased to see some well placed variable declarations. Pity it was in C code!)
Last edited on
the error is universal I would think

Actually, this error is C specific. C requires all declarations to be at the beginning of the block. So, you would have to do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
     //Declare all variables
     double mass, acceleration, force;
     double volume_cylinder, radius, height;
     char plain_character, encoded_character;
     float height_proj, time, initial_height, initial_vel;
     float current, power, resistance;
     float y, x, z;
     int a;

     //Newton's Second Law
     printf("Please enter the mass and acceleration to be used in Newton's Second Law of Motion:\n");

     ...


If you want this to compile as is, then you should change the extension from ".c" to ".cpp".

y=(3/4)*x-(z/(a%2))+PI;

The (3/4) here does integer division, so the result will be zero. You should cast one of the operands to float, or you could do the following because x is a float (3 * x/4)

I can see there are syntax errors on lines 26, 36, 43, 55, 65, 66 -- these are all comment lines.

This is just a coincidence because the variable declarations are right after the comments. There is no error at the first comment //Newton's Second Law because mass, acceleration, and force are declared at the beginning of the block.
Last edited on
Thanks a lot, I will try that out. Sorry, I am used to coding in C++ but am in a class that uses C so that is why I am used to declaring variables as needed rather than all at the beginning.
Topic archived. No new replies allowed.