about C error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Victor

    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(struc t 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)+p ow(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)+p ow(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)+(i c.z*temp.z);
    posC.y = (jc.x*temp.x)+( jc.y*temp.y)+(j c.z*temp.z);
    posC.z = (kc.x*temp.x)+( kc.y*temp.y)+(k c.z*temp.z);
    }
    /*
    void calcPixel(struc t 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;
    }*/
  • Kevin Goodsell

    #2
    Re: about C error

    Victor wrote:
    [color=blue]
    > I'm trying to run this java program,[/color]

    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.)
    [color=blue]
    > 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 >[/color]

    All of these are deprecated in C++. <cstdio>, <cstdlib>, <cstring>, and
    <cmath> are the replacements.
    [color=blue]
    >
    > #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)[/color]

    Macros are bad.
    [color=blue]
    >
    > 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];[/color]

    Global variables are bad.
    [color=blue]
    >
    > void solveK(struct vector one, struct vector two);
    > void solveI(struct vector one);
    > void solveJ();
    > void calcPc(int h);
    > void calcPixel(struc t vector pos, int f, int no);
    > void calcOc(struct vector camera_origin, struct vector cam_origin,
    > struct vector one, int f);
    >
    > void main(void)[/color]

    The return type of main must be int. void is not and never has been an
    acceptable return type for main.
    [color=blue]
    > {
    > 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);[/color]

    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:
    [color=blue]
    > /*
    > void calcPixel(struc t 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;
    > }*/[/color]

    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.

    Comment

    • Jack Klein

      #3
      Re: about C error

      On 21 Sep 2003 10:56:12 -0700, mr526@yahoo.com (Victor) wrote in
      comp.lang.c++:
      [color=blue]
      > 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.[/color]

      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.
      [color=blue]
      > 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)[/color]

      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?
      [color=blue]
      > struct vector {
      > double x;
      > double y;
      > double z;
      > }ic,jc,kc,O,C, posW[MAXVERTICES], camera_origin, temp, posC, pixel,
      > temp2;[/color]

      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.
      [color=blue]
      > 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(struc t vector pos, int f, int no);
      > void calcOc(struct vector camera_origin, struct vector cam_origin,
      > struct vector one, int f);
      >
      > void main(void)[/color]

      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.
      [color=blue]
      > {
      > FILE *fp;
      > char str[MAXFILENAME], record[MAXDATA];
      > char tempStr[5] = " ";
      > char emptyStr[MAXDATA];
      > int temp[MAXDATA];
      > int plane[M][N];[/color]

      You defined plane as a local array inside your main() function, it is
      not accessible by name in other functions.
      [color=blue]
      > 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);[/color]

      gets() is the most dangerous function in the entire standard library,
      since there is no way to use if safely.
      [color=blue]
      >
      > /*Checking file*/
      > if ((fp = fopen(str, "r")) == NULL)
      > {
      > printf("Cannot open file.\n");
      > exit(1);
      > }
      >
      > while ( fgets( record, MAXDATA, fp ) != NULL )
      > {
      > strcat(record, tempStr);[/color]

      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.
      [color=blue]
      > 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++ )[/color]

      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.
      [color=blue]
      > {
      > if ((record[i] == ' ')&&(record[i+1] != ' '))
      > {
      > ref[k] = i;[/color]

      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?
      [color=blue]
      > 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 )[/color]

      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.
      [color=blue]
      > {
      > 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);
      > }[/color]

      [snip]
      [color=blue]
      > void calcPixel(struc t 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;[/color]

      Here is where you can't access the local array plane defined inside
      main().
      [color=blue]
      > }*/[/color]

      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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

      Comment

      • Victor

        #4
        Re: about C error

        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.

        Comment

        Working...