about C error

V

Victor

I'm trying to run this java program, but somehow the program always
quit w/o giving any error msg at all. it happenned inside the first
case statements. Strangely, after printing happen2, it just stopped,
and I had no idea what happens.
another error is on the last function. I have already declared plane
as M x N array, but it keeps giving error like

hwone.cpp(50) : warning C4101: 'plane' : unreferenced local variable
hwone.cpp(212) : error C2065: 'plane' : undeclared identifier
hwone.cpp(212) : error C2109: subscript requires array or pointer type
hwone.cpp(212) : error C2109: subscript requires array or pointer type
hwone.cpp(212) : error C2106: '=' : left operand must be l-value
Error executing cl.exe.

hwone.exe - 4 error(s), 1 warning(s)

anybody can help me is really appreciated.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

#define MAXVERTICES (20) /*Maximum number of object's vertices*/
#define MAXFILENAME (20) /*Maximum characters of file's name*/
#define MAXDATA (40) //Maximum data can be read
#define M (256)
#define N (256)

struct vector {
double x;
double y;
double z;
}ic,jc,kc,O,C, posW[MAXVERTICES], camera_origin, temp, posC, pixel,
temp2;

struct cv {
int pos1;
int pos2;
}connected[20];

void solveK(struct vector one, struct vector two);
void solveI(struct vector one);
void solveJ();
void calcPc(int h);
void calcPixel(struct vector pos, int f, int no);
void calcOc(struct vector camera_origin, struct vector cam_origin,
struct vector one, int f);

void main(void)
{
FILE *fp;
char str[MAXFILENAME], record[MAXDATA];
char tempStr[5] = " ";
char emptyStr[MAXDATA];
int temp[MAXDATA];
int plane[M][N];
int ref[20];
int counter = 0;
int count = 0;
int acount = 0;
int focal = 0;
int edges = 0;
int vertices = 0;
int a = 0;

printf("Please Enter the data file name!\n");
printf("Name : ");
gets(str);

/*Checking file*/
if ((fp = fopen(str, "r")) == NULL)
{
printf("Cannot open file.\n");
exit(1);
}

while ( fgets( record, MAXDATA, fp ) != NULL )
{
strcat(record, tempStr);
printf("record is %s\n", record);
printf("tempStr is %s\n", tempStr);
int j = strlen(record);
printf("j is %d, and record a is %c\n",j,record[16]);

for ( int i = 0, k = 0 ; i < j ; i++ )
{
if ((record == ' ')&&(record[i+1] != ' '))
{
ref[k] = i;
printf("happen1 %d\n", ref[k]); //testing
k++;
}
emptyStr = ' ';
}

switch ( counter ) {
case 0:
{
printf("happen2\n"); <<--- quit around here
for ( a = 0 ; a < j ; a = a+1 )
{
temp[a] = atoi(record);
strncpy(record, emptyStr, ref[a]);
}
O.x = temp[0];
O.y = temp[1];
O.z = temp[2];
C.x = temp[3];
C.y = temp[4];
C.z = temp[5];
focal = temp[6];
counter++;
break;
}
case 1:
{
printf("happen3\n");
for ( int b = 0 ; b < j ; b++ )
{
temp = atoi(record);
strncpy(record, emptyStr, ref);
}
edges = temp[0];
vertices = temp[1];
counter++;
break;
}
case 2:
{
printf("happen3\n");
for ( int c = 0 ; c < j ; c++ )
{
temp[c] = atoi(record);
strncpy(record, emptyStr, ref[c]);
}
posW[count].x = temp[0];
posW[count].y = temp[1];
posW[count].z = temp[3];
if (count == vertices)
counter++;
count++;
break;
}
case 3:
{
printf("happen4\n");
for ( int d = 0 ; d < j ; d++ )
{
temp[d] = atoi(record);
strncpy(record, emptyStr, ref[d]);
}
connected[acount].pos1 = temp[0];
connected[acount].pos2 = temp[1];
if (acount == edges)
counter++;
acount++;
break;
}
}
}
printf("Oc is %f,%f,%f\n",O.x,O.y,O.z);
}



void solveK(struct vector one, struct vector two)
{
double a, b, c;
double temp;
a = two.x - one.x;
b = two.y - one.y;
c = two.z - one.z;
temp = sqrt(pow(a,2)+pow(b,2)+pow(c,2));
kc.x = (a/temp);
kc.y = (b/temp);
kc.z = (c/temp);
}

void solveI(struct vector one)
{
ic.x = sqrt(pow(one.y,2)/(pow(one.x,2)+pow(one.y,2)));
ic.y = sqrt(1 - pow(ic.x,2));
ic.z = 0;
}

void solveJ()
{
jc.x = ((kc.y*ic.z) - (kc.z*ic.y));
jc.y = ((kc.z*ic.x) - (kc.x*ic.z));
jc.z = ((kc.x*ic.y) - (kc.y*ic.x));
}

void calcOc(struct vector cam_origin, struct vector one, int f)
{
camera_origin.x = cam_origin.x + (one.x*f);
camera_origin.y = cam_origin.y + (one.y*f);
camera_origin.z = cam_origin.z + (one.z*f);
}

void calcPc(int h)
{
temp.x = posW[h].x - camera_origin.x;
temp.y = posW[h].y - camera_origin.y;
temp.z = posW[h].z - camera_origin.z;
posC.x = (ic.x*temp.x)+(ic.y*temp.y)+(ic.z*temp.z);
posC.y = (jc.x*temp.x)+(jc.y*temp.y)+(jc.z*temp.z);
posC.z = (kc.x*temp.x)+(kc.y*temp.y)+(kc.z*temp.z);
}
/*
void calcPixel(struct vector pos, int f, int no)
{
temp2.x = (f/pos.z)*pos.x;
temp2.y = (f/pos.z)*pos.y;
pixel.x = temp2.x + (M-1)/2;
pixel.y = (N-1)/2 - temp2.y;
int tempo1 = (int) pixel.x;
int tempo2 = (int) pixel.y;
plane[tempo1][tempo2] = no;
}*/
 
K

Kevin Goodsell

Victor said:
I'm trying to run this java program,

In the subject you say it's a C problem, here you say it's Java, and you
posted to a C++ group. You are aware that these are three completely
different languages, aren't you? (And only one of them is topical here -
I'll leave it up to your deductive skills to determine which.)
but somehow the program always
quit w/o giving any error msg at all. it happenned inside the first
case statements. Strangely, after printing happen2, it just stopped,
and I had no idea what happens.
another error is on the last function. I have already declared plane
as M x N array, but it keeps giving error like

hwone.cpp(50) : warning C4101: 'plane' : unreferenced local variable
hwone.cpp(212) : error C2065: 'plane' : undeclared identifier
hwone.cpp(212) : error C2109: subscript requires array or pointer type
hwone.cpp(212) : error C2109: subscript requires array or pointer type
hwone.cpp(212) : error C2106: '=' : left operand must be l-value
Error executing cl.exe.

hwone.exe - 4 error(s), 1 warning(s)

anybody can help me is really appreciated.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

<cstdlib> said:
#define MAXVERTICES (20) /*Maximum number of object's vertices*/
#define MAXFILENAME (20) /*Maximum characters of file's name*/
#define MAXDATA (40) //Maximum data can be read
#define M (256)
#define N (256)

Macros are bad.
struct vector {
double x;
double y;
double z;
}ic,jc,kc,O,C, posW[MAXVERTICES], camera_origin, temp, posC, pixel,
temp2;

struct cv {
int pos1;
int pos2;
}connected[20];

Global variables are bad.
void solveK(struct vector one, struct vector two);
void solveI(struct vector one);
void solveJ();
void calcPc(int h);
void calcPixel(struct vector pos, int f, int no);
void calcOc(struct vector camera_origin, struct vector cam_origin,
struct vector one, int f);

void main(void)

The return type of main must be int. void is not and never has been an
acceptable return type for main.
{
FILE *fp;
char str[MAXFILENAME], record[MAXDATA];
char tempStr[5] = " ";
char emptyStr[MAXDATA];
int temp[MAXDATA];
int plane[M][N];
int ref[20];
int counter = 0;
int count = 0;
int acount = 0;
int focal = 0;
int edges = 0;
int vertices = 0;
int a = 0;

printf("Please Enter the data file name!\n");
printf("Name : ");
gets(str);

If you only learn ONE thing today, learn this: NEVER, UNDER ANY
CIRCUMSTANCES, USE THE gets() FUNCTION!!!! EVER!

There are too many problems with this code (and too much code) for me to
diagnose all of them. Besides that, it looks like C code, therefore you
should not have posted it here since any advice we give may be dead
wrong for C, and C is off-topic here.

Skipping ahead:
/*
void calcPixel(struct vector pos, int f, int no)
{
temp2.x = (f/pos.z)*pos.x;
temp2.y = (f/pos.z)*pos.y;
pixel.x = temp2.x + (M-1)/2;
pixel.y = (N-1)/2 - temp2.y;
int tempo1 = (int) pixel.x;
int tempo2 = (int) pixel.y;
plane[tempo1][tempo2] = no;
}*/

The compiler is right. plane is undeclared in this scope. Variable
declared in a function are local to that function, so plane only exists
inside main().

You are getting way ahead of yourself with this code. You are having
problems because you don't understand the language. You need to learn
more before you attempt to write a non-trivial program.

-Kevin
 
J

Jack Klein

I'm trying to run this java program, but somehow the program always
quit w/o giving any error msg at all. it happenned inside the first
case statements. Strangely, after printing happen2, it just stopped,
and I had no idea what happens.

This is not a C program, it is a C++ program. If it were truly a C
program it would be off-topic here and you would need to post in
comp.lang.c, not here. Since you have given your source code file an
extension of .cpp and compiled it with Visual C++, it is a C++ program
no matter what you think.
another error is on the last function. I have already declared plane
as M x N array, but it keeps giving error like

hwone.cpp(50) : warning C4101: 'plane' : unreferenced local variable
hwone.cpp(212) : error C2065: 'plane' : undeclared identifier
hwone.cpp(212) : error C2109: subscript requires array or pointer type
hwone.cpp(212) : error C2109: subscript requires array or pointer type
hwone.cpp(212) : error C2106: '=' : left operand must be l-value
Error executing cl.exe.

hwone.exe - 4 error(s), 1 warning(s)

anybody can help me is really appreciated.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

#define MAXVERTICES (20) /*Maximum number of object's vertices*/
#define MAXFILENAME (20) /*Maximum characters of file's name*/
#define MAXDATA (40) //Maximum data can be read
#define M (256)
#define N (256)

Why are you putting parentheses around simple text macros like this?
It can't do you any good, and it just might cause problems in some
instances, although I can't think of any off-hand.

Since you are compiling with a C++ compiler, why not use constant
variables instead?
struct vector {
double x;
double y;
double z;
}ic,jc,kc,O,C, posW[MAXVERTICES], camera_origin, temp, posC, pixel,
temp2;

This is an extremely poor coding technique in either language. If you
need exactly one instance (single or array) of an aggregate type,
there is nothing wrong with this, but cramming a large number of
definitions onto a single line like this is error prone and will
become a maintenance nightmare.
struct cv {
int pos1;
int pos2;
}connected[20];

void solveK(struct vector one, struct vector two);
void solveI(struct vector one);
void solveJ();
void calcPc(int h);
void calcPixel(struct vector pos, int f, int no);
void calcOc(struct vector camera_origin, struct vector cam_origin,
struct vector one, int f);

void main(void)

Both C and C++ require that main() be defined with a return type of
int in a hosted environment. Since this is a C++ program, it is
ill-formed.
{
FILE *fp;
char str[MAXFILENAME], record[MAXDATA];
char tempStr[5] = " ";
char emptyStr[MAXDATA];
int temp[MAXDATA];
int plane[M][N];

You defined plane as a local array inside your main() function, it is
not accessible by name in other functions.
int ref[20];
int counter = 0;
int count = 0;
int acount = 0;
int focal = 0;
int edges = 0;
int vertices = 0;
int a = 0;

printf("Please Enter the data file name!\n");
printf("Name : ");
gets(str);

gets() is the most dangerous function in the entire standard library,
since there is no way to use if safely.
/*Checking file*/
if ((fp = fopen(str, "r")) == NULL)
{
printf("Cannot open file.\n");
exit(1);
}

while ( fgets( record, MAXDATA, fp ) != NULL )
{
strcat(record, tempStr);

It the record uses all MAXDATA bytes of record, trying to strcat()
something onto the end of record will overwrite the array boundaries
and cause undefined behavior.
printf("record is %s\n", record);
printf("tempStr is %s\n", tempStr);
int j = strlen(record);
printf("j is %d, and record a is %c\n",j,record[16]);

for ( int i = 0, k = 0 ; i < j ; i++ )

Yes, definitely a C++ program. The definition of variable inside a
for statement is not legal in any version of C that any Microsoft
compiler claims conformance to.
{
if ((record == ' ')&&(record[i+1] != ' '))
{
ref[k] = i;


Where do you check to keep bad things from happening is k ever becomes
greater than or equal to 20 and so writes past the end of your array?
printf("happen1 %d\n", ref[k]); //testing
k++;
}
emptyStr = ' ';
}

switch ( counter ) {
case 0:
{
printf("happen2\n"); <<--- quit around here
for ( a = 0 ; a < j ; a = a+1 )


j is an assigned that was assigned a size_t value returned by the
strlen() function. You called strlen() on an input string that could
have been up to 239 characters, before you concatenated something else
onto the end of it. So j could cause you to loop past the end of the
temp array, and easily past the end of the ref array.
{
temp[a] = atoi(record);
strncpy(record, emptyStr, ref[a]);
}
O.x = temp[0];
O.y = temp[1];
O.z = temp[2];
C.x = temp[3];
C.y = temp[4];
C.z = temp[5];
focal = temp[6];
counter++;
break;
}
case 1:
{
printf("happen3\n");
for ( int b = 0 ; b < j ; b++ )
{
temp = atoi(record);
strncpy(record, emptyStr, ref);
}
edges = temp[0];
vertices = temp[1];
counter++;
break;
}
case 2:
{
printf("happen3\n");
for ( int c = 0 ; c < j ; c++ )
{
temp[c] = atoi(record);
strncpy(record, emptyStr, ref[c]);
}
posW[count].x = temp[0];
posW[count].y = temp[1];
posW[count].z = temp[3];
if (count == vertices)
counter++;
count++;
break;
}
case 3:
{
printf("happen4\n");
for ( int d = 0 ; d < j ; d++ )
{
temp[d] = atoi(record);
strncpy(record, emptyStr, ref[d]);
}
connected[acount].pos1 = temp[0];
connected[acount].pos2 = temp[1];
if (acount == edges)
counter++;
acount++;
break;
}
}
}
printf("Oc is %f,%f,%f\n",O.x,O.y,O.z);
}
[snip]

void calcPixel(struct vector pos, int f, int no)
{
temp2.x = (f/pos.z)*pos.x;
temp2.y = (f/pos.z)*pos.y;
pixel.x = temp2.x + (M-1)/2;
pixel.y = (N-1)/2 - temp2.y;
int tempo1 = (int) pixel.x;
int tempo2 = (int) pixel.y;
plane[tempo1][tempo2] = no;


Here is where you can't access the local array plane defined inside
main().

Your code is just too unreadable for me to spend any more time on. It
needs comments to give a reader some understanding of what it is
supposed to be doing. It needs to be broken up into understandable
modules. It needs to have several examples of undefined behavior and
the possibility of array overflow fixed.

Fix those things first and then repost much more readable code, or
learn to use the excellent debugger that comes with Visual C++.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
V

Victor

about java program, that was a typo.

I have been using Java a lot in most of my classes, not C/C++. That's
why I am having problem with it. and the last classes I took was two
years ago.
But thanks a lot, all the advices are appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top