473,231 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

about C error

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[i] == ' ')&&(record[i+1] != ' '))
{
ref[k] = i;
printf("happen1 %d\n", ref[k]); //testing
k++;
}
emptyStr[i] = ' ';
}

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[b] = atoi(record);
strncpy(record, emptyStr, ref[b]);
}
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;
}*/
Jul 19 '05 #1
3 4134
Victor wrote:
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>
All of these are deprecated in C++. <cstdio>, <cstdlib>, <cstring>, and
<cmath> are the replacements.

#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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2
On 21 Sep 2003 10:56:12 -0700, mr***@yahoo.com (Victor) wrote in
comp.lang.c++:
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[i] == ' ')&&(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[i] = ' ';
}

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[b] = atoi(record);
strncpy(record, emptyStr, ref[b]);
}
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
Jul 19 '05 #3
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.
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Krzysztof Ksi±¿ek | last post by:
Hi! As almost totally newbe, I have written a simple script and I'm proud of it ;-) It connects to ftp server and uploads some files. Kind of backup. I've compiled it with py2exe and put it in...
1
by: Park Yeon Jo | last post by:
About Error : ORA-12514: TNS:listener could not resolve SERVICE_NAME given in connect descriptor I installed Oracle 8.1.7 on Windows XP Professional. and I wanto connect to that server...
5
by: Lau Lei Cheong | last post by:
Hello, I'm currently using Application_Error method in Global.asax.cs to handle errors. Recently, I heard about Page.ErrorPage and plan to use it for handling errors on certain pages. Are...
2
by: Diana Estrada | last post by:
Hi, I have a ASP .Net, and in this I load a report or Crystal, with this instruction: crReportDocument.Load(Server.MapPath("") & "reporte.rpt"); It's good, but when I try to load a remote...
4
by: Tom | last post by:
Hi All : I have some question about error handling : Can I get some system varabile which can get current function name , also if error occur, can I get some system varabile which show the...
9
by: Gustaf | last post by:
I'm confused about structured error handling. The following piece of code is a simplification of a class library I'm working on. It works, and it does what I want, but I'm still not convinced that...
33
by: Anthony England | last post by:
I am considering general error handling routines and have written a sample function to look up an ID in a table. The function returns True if it can find the ID and create a recordset based on...
10
by: Anthony England | last post by:
(sorry for the likely repost, but it is still not showing on my news server and after that much typing, I don't want to lose it) I am considering general error handling routines and have...
7
by: EManning | last post by:
Using A2003. I'm receiving this error when returning from a "DoCmd.OpenReport..." statement. I have a tab control with a subform on every tab. The user selects an item from a combobox at the top...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.