GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #51  
Old 18-May-2007, 12:11
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Linked Lists advice request


Quote:
a guide to rebuild my own switches
Ok, glad your examining the switch a bit.
But please go back to simpler example as suggested.
Your compiler runs it as it compiles?
Will ctrl-c stop it instead of ctrl-alt-del? hmmm
Post the entire case 3 including it's 'case' and it's 'break'.
sorry, maybe a bad example.
The point is that it attempts to assign data outside of the memory space that the OS has allocated for your program. That is a segfault or "segmentation fault"
I know what you mean, I get timed out while composing sometimes too...
I have to go , but look forward to seeing a single moving shape later!
or just as enjoyable, an understandable posting of problems associated to creating that single shape.
  #52  
Old 18-May-2007, 13:32
promsan promsan is offline
Junior Member
 
Join Date: May 2007
Location: North of the rhubarb triangle, GB
Posts: 53
promsan is on a distinguished road

Re: Linked Lists advice request


"post"?
I mean, I copied it into the prog in place of the 2-3 that was there including its case and break, and it just spat it out, so I'm just focussing on rebuilding the switch from scratch, and I've deleted everything that's not being used or relevant to get the basic skeleton working.

"composing" ! if only i could write code by waggling a chopstick in front of the screen!
  #53  
Old 18-May-2007, 20:57
promsan promsan is offline
Junior Member
 
Join Date: May 2007
Location: North of the rhubarb triangle, GB
Posts: 53
promsan is on a distinguished road

Re: Linked Lists advice request


gettin' 2 persistent errors:
CPP / C++ / C Code:
typedef struct long_shape_type {
    int start_x1, start_y1;
    int start_x2, start_y2;
    int start_x3, start_y3;
    int x;
    int y;
    int size;
    int radius;
    int colour;
    struct long_shape_type *prev;
    struct long_shape_type *next;
} shape_type;

typedef struct long_line_type {
    int start_x, start_y;
    int end_x, end_y;
    int x;
    int y;
    int size;
    int radius;
    int colour;
    struct long_line_type *prev;
    struct long_line_type *next;
} line_type;

#include <stdio.h>
#include "graphics_lib.h"
#include <math.h>
#include <conio.h>

struct triangle {
  int corner1x;
  int corner1y;
  int corner2x;
  int corner2y;
  int corner3x;
  int corner3y;
  int colour;
  float theta;
} tangle;

// adapted from [" http://lavluda.x10hosting.com/download/2d.doc"]

struct point
{
    double corner1x;
    double corner1y;
    double corner2x;
    double corner2y;
    double corner3x;
    double corner3y;
    double x;
    double y;
    double theta;
    int colour;
    int size;
}point1,point2,point3,newpoint1,newpoint2,newpoint3;

struct triangle *Tptr;

void display_line(struct triangle *Tptr);
void get_tangle(struct triangle *Tptr);

#define PI 3.14159265

int menu(void);

 /*
 * THE MAIN FUNCTION - the programme starts executing here
 */

int main()
{
//	int choice;
	int i;
	i = menu();

	printf("exiting main(), i = %d\n", i);

    getch();
   
    return 0;
}

/* **********  functions  *********** */

/*
INTRO SCREENS
*/

/* SWITCH */

int menu(void)
{
    /* DECLARE VARIABLES */
//    int x;
//    int y;
//    int size;
    int sizeshape_x;
    int sizeshape_y;
    float theta;
//    int shape_select;

//    line_type lt;

	int i, choice1, choice2, choice3, choice4, choice5, choice6, choice7;
	i = choice1 = choice2 = choice3 = choice4 = choice5 = choice6 = choice7 = 0;

    printf("Menu\n");
    printf("1: Add new shape \n");
    printf("2: Delete last shape \n");
	printf("3: Instructions \n");
	printf("4: About this programme \n");
	printf("5: Exit programme \n");
	printf("Enter your choice: ");

    while(choice1 < 1 || choice1 > 5) {//open while1
        scanf("%d", &choice1);

		switch(choice1) {//open switch1
			case 1:	{//open case1
				printf("Choose a new shape:\n");
				printf("1: Line \n");
				printf("2: Circle \n");
				printf("3: Triangle \n");
				printf("4: Square \n");
				printf("5: Return to main menu \n");
				printf("Enter your choice: ");
	         
				while(choice2 < 1 || choice2 > 5) {//open while1-1
					scanf("%d", &choice2);
					switch(choice2) {//open switch1-1
						case 1://{//open case1-1
							/* Line */
							printf("Choose characteristics of your line:\n");
							printf("1: Size \n");
							printf("2: Colour \n");
							printf("3: Orientation \n");
							printf("4: Position \n");
							printf("5: Draw Line \n");
							printf("Enter your choice: ");
	         
							while(choice3 < 1 || choice3 > 5) {//open while1-1-1
								scanf("%d", &choice3);
								switch(choice3) {//open switch1-1-1
								case 1:
										printf("1: Size \n");
										/* Size Selection
										printf("please enter a number for the size of your shape:\n");
										scanf("%d",&size);
										*/
										break;
									case 2:
										printf("2: Colour \n");
										break;	
									case 3:								
										printf("3: Orientation \n");
										break;	
									case 4:								
										printf("4: Position \n");
										/* Get initial coordinates
										printf("please enter an x and a y co-ordinate for your shape\n");
										printf("x position is: \n");
										scanf("%d",&x);
										printf("y position is: \n");
										scanf("%d",&y);
										*/
										break;	
									case 5:
										printf("5: Draw line \n");
										/* Line Construxion
										lt.start_x = x;
										lt.start_y = y+(size/2);
										lt.end_x = x;
										lt.end_y = y-(size/2);
										printf("lt.start_x = %d\n", lt.start_x);
										printf("lt.start_y = %d\n", lt.start_y);
										printf("lt.end_x = %d\n", lt.end_x);
										printf("lt.end_y = %d\n", lt.end_y); 
										*/
										break;
									default:
										printf("Choice not recognised in line menu, try again \n");
								}//close switch1-1-1
							}//close while1-1-1	
							break;
						case 2:
							/* Circle */
							printf("Choose characteristics of your circle:\n");
							printf("1: Size \n");
							printf("2: Colour \n");
							printf("3: Orientation \n");
							printf("4: Position \n");
							printf("5: Return to main menu \n");
							printf("Enter your choice: ");
	         
							while(choice4 < 1 || choice4 > 5) {//open while1-1-2
								scanf("%d", &choice4);
		
								switch(choice4) {//open switch1-1-2
									case 1:	
										printf("1: Size \n");
										break;
									case 2:								
										printf("2: Colour \n");								
										break;	
									case 3:								
										printf("3: Orientation \n");								
										break;	
									case 4:
										printf("4: Position \n");								
										break;	
									case 5:	
										printf("5: Draw circle \n");
										/* Circle Construxion
										lt.start_x = x;
										lt.start_y = y;
										lt.radius = size;
										printf(" lt.start_x = %d\n", lt.start_x);
										printf("lt.start_y = %d\n", lt.start_y);
										printf("lt.radius = %d\n", lt.radius);
										*/
										break;
									default:
										printf("Not recognised in circle menu, try again \n");
								}//close switch1-1-2
							}//close while1-1-2
							break;
						case 3:
							/* Triangle */
							printf("Choose characteristics of your triangle:\n");
							printf("1: Size \n");
							printf("2: Colour \n");
							printf("3: Orientation \n");
							printf("4: Position \n");
							printf("5: Draw triangle \n");
							printf("Enter your choice: ");
	         
							while(choice5 < 1 || choice5 > 5) {//open while1-1-3
								scanf("%d", &choice5);
		
								switch(choice5) {//open switch1-1-3
									case 1:
										printf("1: Size \n");
										printf("Please enter the scaling value (x,y):");
										scanf("%f %f",&sizeshape_x,&sizeshape_y);
										newpoint1.x=(float)(point1.x*sizeshape_x),newpoint1.y=(float)(point1.y*sizeshape_y);
										newpoint2.x=(float)(point2.x*sizeshape_x),newpoint2.y=(float)(point2.y*sizeshape_y);
										newpoint3.x=(float)(point3.x*sizeshape_x),newpoint3.y=(float)(point3.y*sizeshape_y);
										break;
									case 2:					
										printf("2: Colour \n");
										break;	
									case 3:					
										printf("3: Orientation \n");
										printf("please enter an angle to rotate your shape by\n");
										printf("angle:\n");
										scanf("%d",&theta);
										printf("press any key to continue\n");
										getch();
										break;	
									case 4:					
										printf("4: Position \n");
										break;	
									case 5:					
										printf("5: Draw Triangle \n");
										Tptr = &tangle;
				                        get_tangle(Tptr);
						                printf("The line goes from (%d,%d) to (%d,%d) to (%d,%d) and finally back to (%d,%d); it's colour is code %d \n", tangle.corner1x, tangle.corner1y, tangle.corner2x, tangle.corner2y, tangle.corner3x, tangle.corner3y, tangle.corner1x, tangle.corner1y ,  tangle.colour);
								        display_line(Tptr);
										break;
									default:
							            printf("Not recognised in triangle menu, try again \n");
								}//close switch1-1-3
							}//close while1-1-3
							break;
						case 4:
							/* Quadrilateral */                        {
							printf("Choose characteristics of your square:\n");
							printf("1: Size \n");
							printf("2: Colour \n");
							printf("3: Orientation \n");
							printf("4: Position \n");
							printf("5: Return to main menu \n");
							printf("Enter your choice: ");
	         
							while(choice6 < 1 || choice6 > 5) {//open while1-1-4
								scanf("%d", &choice6);
		
								switch(choice6) {//open switch1-1-4
									case 1:
										printf("1: Size \n");						
										break;
									case 2:
										printf("2: Colour \n");						
										break;	
									case 3:	
										printf("3: Orientation \n");
										break;	
									case 4:					
										printf("4: Position \n");
										break;	
									case 5:					
										printf("5: Draw Square \n");
										break;
									default:
										printf("Not recognised in square menu, try again \n");
								}//close switch1-1-4
							}//close while1-1-4
							break;
						case 5:
							/* Return to main menu */                        {
							printf("Choose characteristics of your square:\n");
							printf("1: Return to main menu \n");
							printf("2: Exit Programme \n");
							printf("Enter your choice: ");
	         
							while(choice6 < 1 || choice7 > 2) {//open while1-1-5
								scanf("%d", &choice7);
		
								switch(choice6) {//open switch1-1-5
									case 1:
										printf("1: Return to main menu \n");						
										break;
									case 2:
										printf("2: Exit \n");						
										break;	
									default:
										printf("Not recognised in exit programme menu, try again \n");
								}//close switch1-1-4
							}//close while1-1-4
							//break; //break 1-1
						}//close case1-1
					}//close switch1-1
				}//close while1-1
				break;
			case 2:
	            printf("Delete last shape:\n");
				break;
			case 3:        
				printf("Instructions:\n");
				break;
		    case 4:    
				printf("About this programme:\n");
				break;
		    case 5:    
				printf("Ctrl-C to exit:\n");
				break;
			default:
			    printf("Not recognised in main menu, try again \n");            
        }//close switch1
    }//close while1

    getch();
   
    return 0;
}//close menu function


void display_line(struct triangle *Tptr)
{
  initwindow(600, 400);
  setcolor(Tptr->colour);
  line(Tptr->corner1x, Tptr->corner1y, Tptr->corner2x, Tptr->corner2y);
  line(Tptr->corner2x, Tptr->corner2y, Tptr->corner3x, Tptr->corner3y);
  line(Tptr->corner3x, Tptr->corner3y, Tptr->corner1x, Tptr->corner1y);

}


void get_tangle(struct triangle *Tptr)
{
  printf("The Display is 600 x 400.  Enter COORDs for: \n       corner1x: ");
  scanf ("%d", &Tptr->corner1x);
  printf("       T1y: ");
  scanf ("%d", &Tptr->corner1y);
  printf("       T2x: ");
  scanf ("%d", &Tptr->corner2x);
  printf("       T2y: ");
  scanf ("%d", &Tptr->corner2y);
  printf("       T3x: ");
  scanf ("%d", &Tptr->corner3x);
  printf("       T3y: ");
  scanf ("%d", &Tptr->corner3y);
  printf("and colour: ");
  scanf ("%d", &Tptr->colour);
  printf("%d, %d, %d, %d, %d, %d, %d <-and there they are. \n", Tptr->corner1x, Tptr->corner1y, Tptr->corner2x, Tptr->corner2y, Tptr->corner3x, Tptr->corner3y, Tptr->colour);
}

here:
Quote:
--------------------Configuration: shapeshifter - Win32 Debug--------------------
Compiling...
shapeshifter.c
C:\Documents and Settings\...\shapeshifter\shapeshifter\shapeshifte r.c(360) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\...\shapeshifter\shapeshifter\shapeshifte r.c(371) : error C2143: syntax error : missing ';' before 'type'
Error executing cl.exe.

shapeshifter.obj - 2 error(s), 0 warning(s)

spent ages fiddling, just can't seem to shake 'em... time for bed, I'm knackered.
  #54  
Old 18-May-2007, 22:55
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Linked Lists advice request


unbelievable.... . . . . slow count to 10...100...1000

Your missing two closing braces somewhere in your rediculous 250 line switch to nowhere... don't ask me where they might go , I ain't into wasting my time figuring that mess out... are you done yet???

You are trying to soar while learning to crawl. Ever hear of Icarus?
  #55  
Old 19-May-2007, 03:23
shalombi shalombi is offline
Junior Member
 
Join Date: Feb 2007
Posts: 47
shalombi is on a distinguished road

Re: Linked Lists advice request


CPP / C++ / C Code:
C:\Documents and Settings\...\shapeshifter\shapeshifter\shapeshifte r.c(360) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\...\shapeshifter\shapeshifter\shapeshifte r.c(371) : error C2143: syntax error : missing ';' before 'type' 

This is the most basic type of error, they mean a few possible things, missing semicolon, missing bracket , missing inverted commas or missing curly bracket.

So it's up to you to go back up and look.

Anyone can count the number of brackets and such, you don't need help for that.

If you indent your code properly, comment it properly, and implement the big switches and such from WORKING prototypes, you will avoid these problems. (I'm repeating myself, I know)

I would suggest you take time to reconstruct your code from ground one reusing all the functions that you know work, won't take long and you will save hours on the long run.

And with your brand new knowledge make it in an ordered fashion, put each huge block of code in the switch statements in a separate function switch_func1 2 3... if not the whole of the switch itself.

It may seem a waste of time but:

1 your code might become readable and we could help.

2 you will understand what the hell your doing.

Naturally the down side is you wont win the OCC contest this year.

Good luck.

Max
  #56  
Old 19-May-2007, 03:26
promsan promsan is offline
Junior Member
 
Join Date: May 2007
Location: North of the rhubarb triangle, GB
Posts: 53
promsan is on a distinguished road

Re: Linked Lists advice request


I deridiculousised my switch, after getting some sleep.
I knew there was a brace issue.
Quote:
If you indent your code properly, comment it properly, and implement the big switches and such from WORKING prototypes, you will avoid these problems.
I have indented it properly haven't I? (have another look!), I don't see what's wrong with the commenting on the last bit of code (there's no essays or random ponderings any more!), I have implemented it from a working prototype and counted and gone through it line by line several times... It looks the same. I've swapped my function with Howard's again, and no error... so I'll do it again.
  #57  
Old 19-May-2007, 06:05
promsan promsan is offline
Junior Member
 
Join Date: May 2007
Location: North of the rhubarb triangle, GB
Posts: 53
promsan is on a distinguished road
Post

Re: Linked Lists advice request


Sorry if I'm winding people up.
I Rebuilt it. It works. No obvious location for the missing brace(s).
So now, I'm trying to add my working line drawing programme into the second level of the switch.
Quote:
--------------------Configuration: shapeshifter - Win32 Debug--------------------
Compiling...
shapeshifter.c
C:\Documents and Settings\...\shapeshifter.c(106) : error C2275: 'line_type' : illegal use of this type as an expression
C:\Documents and Settings\...\shapeshifter.c(24) : see declaration of 'line_type'
C:\Documents and Settings\...\shapeshifter.c(106) : error C2146: syntax error : missing ';' before identifier 'lt'
C:\Documents and Settings\...\shapeshifter.c(106) : error C2065: 'lt' : undeclared identifier
C:\Documents and Settings\...\shapeshifter.c(139) : error C2065: 'colour' : undeclared identifier
C:\Documents and Settings\...\shapeshifter.c(155) : error C2224: left of '.start_x' must have struct/union type
C:\Documents and Settings\...\shapeshifter.c(156) : error C2224: left of '.start_y' must have struct/union type
C:\Documents and Settings\...\shapeshifter.c(157) : error C2224: left of '.end_x' must have struct/union type
C:\Documents and Settings\...\shapeshifter.c(15 : error C2224: left of '.end_y' must have struct/union type
C:\Documents and Settings\...\shapeshifter.c(159) : error C2224: left of '.start_x' must have struct/union type
C:\Documents and Settings\...\shapeshifter.c(160) : error C2224: left of '.start_y' must have struct/union type
C:\Documents and Settings\...\shapeshifter.c(161) : error C2224: left of '.end_x' must have struct/union type
C:\Documents and Settings\...\shapeshifter.c(162) : error C2224: left of '.end_y' must have struct/union type
C:\Documents and Settings\...\shapeshifter.c(207) : warning C4028: formal parameter 1 different from declaration
C:\Documents and Settings\...\shapeshifter.c(209) : error C2037: left of 'colour' specifies undefined struct/union 'line_type'
C:\Documents and Settings\...\shapeshifter.c(209) : error C2198: 'setcolor' : too few actual parameters
C:\Documents and Settings\...\shapeshifter.c(211) : error C2143: syntax error : missing ')' before '->'
C:\Documents and Settings\...\shapeshifter.c(211) : error C2143: syntax error : missing '{' before '->'
C:\Documents and Settings\...\shapeshifter.c(211) : error C2059: syntax error : '->'
C:\Documents and Settings\...\shapeshifter.c(211) : error C2059: syntax error : ')'
C:\Documents and Settings\...\shapeshifter.c(212) : error C2059: syntax error : '}'
C:\Documents and Settings\...\shapeshifter.c(224) : error C2084: function 'void __cdecl display_line(struct triangle *)' already has a body
Error executing cl.exe.

shapeshifter.obj - 20 error(s), 1 warning(s)

current (shorter) code:
CPP / C++ / C Code:
typedef struct long_shape_type {
    int start_x1, start_y1;
    int start_x2, start_y2;
    int start_x3, start_y3;
    int x;
    int y;
    int size;
    int radius;
    int colour;
    struct long_shape_type *prev;
    struct long_shape_type *next;
} shape_type;

typedef struct long_line_type {
    int start_x, start_y;
    int end_x, end_y;
    int x;
    int y;
    int size;
    int radius;
    int colour;
    struct long_line_type *prev;
    struct long_line_type *next;
} line_type;

#include <stdio.h>
#include "graphics_lib.h"
#include <math.h>
#include <conio.h>

struct triangle {
  int corner1x;
  int corner1y;
  int corner2x;
  int corner2y;
  int corner3x;
  int corner3y;
  int colour;
  float theta;
} tangle;

// adapted from [" http://lavluda.x10hosting.com/download/2d.doc"]

struct point
{
    double corner1x;
    double corner1y;
    double corner2x;
    double corner2y;
    double corner3x;
    double corner3y;
    double x;
    double y;
    double theta;
    int colour;
    int size;
}point1,point2,point3,newpoint1,newpoint2,newpoint3;

struct triangle *Tptr;

void display_line(struct triangle *Tptr);
void get_tangle(struct triangle *Tptr);

#define PI 3.14159265

int menu(void);

 /*
 * THE MAIN FUNCTION - the programme starts executing here
 */

int main()
{
	int i;
	i = menu(); 

	printf("exiting main(), i = %d\n", i);
    return 0;
}

/* **********  functions  *********** */

/* SWITCH */

int menu(void)
{
     /* DECLARE VARIABLES - Do I declare them all here or in the main... how to access the functions and pointers? */ 
    int x;
    int y;
    int size;
    int choice;
    //int moveshape_x;
    //int moveshape_y;
    int sizeshape_x;
    int sizeshape_y;
    //double sin_value;
    //double cos_value;
    float theta;
    //int x_pos;
    //int y_pos;
    //char move;
    int shape_select;
	int i, choice1, choice2;
	i = choice1 = choice2 = 0;
	
	line_type lt;

  printf("Menu\n");
  printf("1: Add new shape \n");
  printf("2: Delete last shape \n");
  printf("3: Instructions \n");
  printf("4: About this programme \n");
  printf("5: Exit programme \n");
  printf("Enter your choice: ");

  while(choice1 < 1 || choice1 > 5) {
    scanf("%d", &choice1);

    switch(choice1) {
      case 1: {
		printf("Choose a new shape:\n");
        printf("1: Line \n");
        printf("2: Circle \n");
        printf("3: Triangle \n");
        printf("4: Square \n");
        printf("5: Return to main menu \n");
        printf("Enter your choice: ");

        while(choice2 < 1 || choice2 > 5) {
          scanf("%d", &choice2);
          switch(choice2) {
            case 1:
              printf("You selected draw Line \n");
                  /* Size Selection */
                     printf("please enter a number for the size of your shape:\n");
                     scanf("%d",&size);
                                        
                     printf("please enter a number for the colour \n");
                     scanf("%d",&colour);   
                     printf("use the cursor keys to orientate your line \n");
                     scanf("%d",&colour);                             
                     printf("4: Position \n");
                     /* Get initial coordinates */ 
                     printf("please enter an x and a y co-ordinate for your shape\n");
                     printf("x position is: \n");
                     scanf("%d",&x);
                     printf("y position is: \n");
                     scanf("%d",&y);
                                        
                     printf("press return to draw the line\n");
					 getch();
                     /* Line Construxion */
					 //get_line(&lt); 
					 //display_line(&line);
                     lt.start_x = x;
                     lt.start_y = y+(size/2);
                     lt.end_x = x;
                     lt.end_y = y-(size/2);
                     printf("lt.start_x = %d\n", lt.start_x);
                     printf("lt.start_y = %d\n", lt.start_y);
                     printf("lt.end_x = %d\n", lt.end_x);
                     printf("lt.end_y = %d\n", lt.end_y);
                                                        
              break;
            case 2:
	          printf("You selected draw Circle  ...look familiar? \n(ctrl-c (win) or ctrl-d (linux) will kill it) \n");
	          while(i >= 0)
	            i= 2;
              break;
            case 3:
              printf("You selected draw Triangle \n");
              break;
            case 4:
              printf("You selected draw Square \n");
              break;
            case 5:
              printf("You selected Return to main menu \n");
              break;
            default:
              printf("You entered wrongly in menu 2, try again \n");
          }     //close switch2
        }       //close while2
        }       //close case1-1 stuff
        //break;  //break 1-1 , , try removing this , I had forgotten it at first!
      case 2:
        printf("You selected Delete last shape , choice1 = %d \n", choice1);
        break;
      case 3:
        printf("You selected Instructions \n");
        break;
      case 4:
        printf("You selected About this programme \n");
        break;
      case 5:
        printf("You selected exit (press CTRL-C) \n");
        break;
      default:
        printf("Not recognised in main menu, try again \n");

    }  //close switch1
  }    //close while1
  return 0;
}      //close function


void display_line(struct line_type *line_pointer)
{
  initwindow(600, 400);
  setcolor(line_pointer->colour);
}
line(this_line->start_x,this_line->start_y,this_line->end_x,this_line->end_y);
}

void get_line(line_type *line_pointer)
{
scanf("%d", &line_pointer->start_x);
scanf("%d", &line_pointer->start_y);
scanf("%d", &line_pointer->end_x);
scanf("%d", &line_pointer->end_y);
scanf("%d", &line_pointer->colour);
}

void display_line(struct triangle *Tptr)
{
  initwindow(600, 400);
  setcolor(Tptr->colour);
  line(Tptr->corner1x, Tptr->corner1y, Tptr->corner2x, Tptr->corner2y);
  line(Tptr->corner2x, Tptr->corner2y, Tptr->corner3x, Tptr->corner3y);
  line(Tptr->corner3x, Tptr->corner3y, Tptr->corner1x, Tptr->corner1y);

}


void get_tangle(struct triangle *Tptr)
{
  printf("The Display is 600 x 400.  Enter COORDs for: \n       corner1x: ");
  scanf ("%d", &Tptr->corner1x);
  printf("       T1y: ");
  scanf ("%d", &Tptr->corner1y);
  printf("       T2x: ");
  scanf ("%d", &Tptr->corner2x);
  printf("       T2y: ");
  scanf ("%d", &Tptr->corner2y);
  printf("       T3x: ");
  scanf ("%d", &Tptr->corner3x);
  printf("       T3y: ");
  scanf ("%d", &Tptr->corner3y);
  printf("and colour: ");
  scanf ("%d", &Tptr->colour);
  printf("%d, %d, %d, %d, %d, %d, %d <-and there they are. \n", Tptr->corner1x, Tptr->corner1y, Tptr->corner2x, Tptr->corner2y, Tptr->corner3x, Tptr->corner3y, Tptr->colour);
}
I'm genuinely trying to understand how this works rather than looking for code to copy and paste.

cheers,

J
  #58  
Old 19-May-2007, 09:02
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Linked Lists advice request


Yo Icarus!
If someone hands you a loaded gun,,, do yourself a favor and give it back.

What you posted compiles and runs in g++.... not well though...
What your errors came from was obviously different...

With -Wall I got 1 warning about menu() having no return value.
There is one there so I think the switch is messed. not my problem...

To echo Shalombi (thanks dude):
On the switch issue: "K&R"-kinda: when you find yourself using a lot of indents and your code is running way off the right side it's probably time to think about using a function for that section...
Like for starters, switch2 could go into a menu2().
Any more complex case could go to a separate().

I implore you to remove all extraneous BS: switches, extra structures... and get down to one simple shape.

long_shape_type an shape_type are cumbersome names.
The structure look looks cumbersome as well.

I don't think a one size fit's all approach is a good idea.

Design/Define a structure for a particular shape. ONE SHAPE.. like triangle!

You asked about where to put variables?
- Externally declared variables are available to all functions BUT they should also be redeclared with 'extern' in functions in which they are used.
- A variable is only available in the function in which it was declared. (that functions 'SCOPE')
- However, the VALUE of a variable may be passed from one function to another.

Soak in that a while.
  #59  
Old 19-May-2007, 11:22
promsan promsan is offline
Junior Member
 
Join Date: May 2007
Location: North of the rhubarb triangle, GB
Posts: 53
promsan is on a distinguished road
Red face

Re: Linked Lists advice request


Now then Helios,

At last I've found my book on C (it was helpfully tidied away for the last few days by her indoors).
I've read some of it and found some useful thoughts on using unions as an element of a structure to try and lose some flabby code, e.g.:

CPP / C++ / C Code:
struct shape {
   int shape_type;
   union {
      struct line         l;
      struct circle      c;
      struct triangle   t;
      struct square    s;
   } data;
};

summat along these lines:
CPP / C++ / C Code:
/* Code adapted from p448-449 "Indispensible Guide to C" */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

/* structure templates for each type of shape */

struct line       {int x1, y1, x2, y2; };
struct circle     {int x1, y1, radius};
struct triangle {int x1, y1, x2, y2, x3, y3; };
struct square   {int x1, y1, x2, y2, x3, y3; x4, y4; };

struct shape {
   int shape_type;
   union {
      struct line         l;
      struct circle      c;
      struct triangle   t;
      struct square    s;
   } data;
};

/* Function prototypes */

void get_line(struct shape *pointer);
void get_circle(struct shape *pointer);
void get_triangle(struct shape *pointer);
void get_square(struct shape *pointer);

/* Constants used to identify each shape */

const int LINE=0;
const int CIRCLE=1;
const int TRIANGLE=2;
const int SQUARE=3;

int main(void)
{
   int     i;
   char  c;

/* Restrict to 5 shapes*/

   struct   shape   shape[5]

   for(i=0;i<5;i++) {
      printf("\n\nEnter shape type... L(ine), C(ircle), T(riangle), S(quare)");
      c=toupper(getchar());
      fflush(stdin);            /* to get rid of the "\n" left by "getchar()" */

      switch(c) {
         case 'L': get_line(&shape[i]);
            break;
         case 'C': get_circle(&shape[i]);
            break;
         case 'T': get_triangle(&shape[i]);
            break;
         case 'S': get_square(&shape[i]);
            break;
       }
   }

   return 0;
}

void get_line(struct shape *pointer)
{
   char buff[BUFSIZE];

/* Example of code to read co-ords for a lin from the operator */

   pointer->shape_type=LINE;
   printf("\nEnter a start 'x' co-ordinate for the line...");
   pointer->data.p.x1=atoi(gets(buff));
   printf("Enter a start  'y' co-ordinate for  the line...");
   pointer->data.p.y1=atoi(gets(buff));
   printf("\nEnter an end 'x' co-ordinate for the line...");
   pointer->data.p.x2=atoi(gets(buff));
   printf("Enter an end 'y' co-ordinate for  the line...");
   pointer->data.p.y2=atoi(gets(buff));
}

void get_line(struct shape *pointer)
{
   pointer->shape_type=LINE;
   printf("\nEnter a centre 'x' co-ordinate for the circle...");
   pointer->data.p.x1=atoi(gets(buff));
   printf("Enter a centre  'y' co-ordinate for  the circle...");
   pointer->data.p.y1=atoi(gets(buff));
   printf("\nEnter a radius for the circle...");
   pointer->data.p.radius=atoi(gets(buff));
}

void get_line(struct shape *pointer)
{
   pointer->shape_type=TRIANGLE;
   printf("\nEnter an 'x' co-ordinate for corner 1...");
   pointer->data.p.x1=atoi(gets(buff));
   printf("\nEnter a 'y' co-ordinate for corner 1...");
   pointer->data.p.y1=atoi(gets(buff));
   printf("\nEnter an 'x' co-ordinate for corner 2...");
   pointer->data.p.x2=atoi(gets(buff));
   printf("\nEnter a 'y' co-ordinate for corner 2...");
   pointer->data.p.y2=atoi(gets(buff));
   printf("\nEnter an 'x' co-ordinate for corner 3...");
   pointer->data.p.x3=atoi(gets(buff));
   printf("\nEnter a 'y' co-ordinate for corner 3...");
   pointer->data.p.y3=atoi(gets(buff));
}

void get_line(struct shape *pointer)
{
   pointer->shape_type=SQUARE;
    printf("\nEnter an 'x' co-ordinate for corner 1...");
   pointer->data.p.x1=atoi(gets(buff));
   printf("\nEnter a 'y' co-ordinate for corner 1...");
   pointer->data.p.y1=atoi(gets(buff));
   printf("\nEnter an 'x' co-ordinate for corner 2...");
   pointer->data.p.x2=atoi(gets(buff));
   printf("\nEnter a 'y' co-ordinate for corner 2...");
   pointer->data.p.y2=atoi(gets(buff));
   printf("\nEnter an 'x' co-ordinate for corner 3...");
   pointer->data.p.x3=atoi(gets(buff));
   printf("\nEnter a 'y' co-ordinate for corner 3...");
   pointer->data.p.y3=atoi(gets(buff));
   printf("\nEnter an 'x' co-ordinate for corner 4...");
   pointer->data.p.x4=atoi(gets(buff));
   printf("\nEnter a 'y' co-ordinate for corner 4...");
   pointer->data.p.y4=atoi(gets(buff));
}

void display(struct shape x)
{
   if(x.shape_type == LINE)
      display_line(&x);
   if(x.shape_type == CIRCLE)
      display_circle(&x);
   if(x.shape_type == SQUARE)
      display_square(&x); 
   if(x.shape_type == TRIANGLE)
      display_triangle(&x);
}
/*
for(i-0;i<5;i++)
   display(shape[i]);
*/

The book dun't say owt (=aught) about where to declare the variables, so thanks for this:
Quote:
- Externally declared variables are available to all functions BUT they should also be redeclared with 'extern' in functions in which they are used.
- A variable is only available in the function in which it was declared. (that functions 'SCOPE')
- However, the VALUE of a variable may be passed from one function to another.

The switch that's there is the same as yours, and it's fine until I started trying to use the data in the structures at the top of the programme within the nested switch cases.

I shall slow down now, and hopefully come back with summat less embarrassing... time for some football (the one where the foot comes into regular contact with the ball).

cheers again,

J
  #60  
Old 19-May-2007, 21:08
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Linked Lists advice request


Here, this is fixed.... (before you ask where the problems are)
Now just what is the game plan for a union,,,
and why would you need both 'data' and 'shape[5]'
You already have 5 perfecly good shape structures to work with.
You could create as many copies of either of them as you want.

CPP / C++ / C Code:
/* Code adapted from p448-449 "Indispensible Guide to C" */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

/* structure templates for each type of shape (this is good)*/
struct line     {int x1, y1, x2, y2; };
struct circle   {int x1, y1, radius; };
struct triangle {int x1, y1, x2, y2, x3, y3; };
struct square   {int x1, y1, x2, y2, x3, y3, x4, y4; };

/* struct/union for some purpose I'm too stupid to realize... */
struct shape {
  int shape_type;
  union {
    struct line      l;
    struct circle    c;
    struct triangle  t;
    struct square    s;
  } data;
};


/* Function prototypes */
void get_line(struct shape *pointer);
void get_circle(struct shape *pointer);
void get_triangle(struct shape *pointer);
void get_square(struct shape *pointer);

/* Constants used to identify each shape */
const int LINE     =  0;
const int CIRCLE   =  1;
const int TRIANGLE =  2;
const int SQUARE   =  3;
const int BUFSIZE  = 20; // Howard added, was undeclared

int main(void)
{
  int     i;
  char  c;

/* Restrict to 5 shapes (u mean declare array of 5 struct 'shape')*/
  struct   shape   shape[5];

  for(i = 0; i < 3; i++) {
    printf("\n\nEnter shape type... (L)ine, (C)ircle, (T)riangle, (S)quare -> ");
    c = toupper(getchar());
    fflush(stdin);            // to get rid of the "\n" left by "getchar()"
    printf("c = %c \n", c);
  }

/* this is much better, now any problem will be associated to
   a either the switch or a particular function               */
  switch(c) {
    case 'L': get_line(&shape[i]);
      break;
/*  case 'C': get_circle(&shape[i]);
      break;
    case 'T': get_triangle(&shape[i]);
      break;
    case 'S': get_square(&shape[i]);
      break;
*/
  }

  printf("sizeof(shape)= %d bytes \n", sizeof(shape) );
  printf("exiting main()");
  return 0;
}


void get_line(struct shape *pointer)
{
  char buff[BUFSIZE];

// Example of code to read co-ords for a line from the operator
  pointer->shape_type=LINE;
  printf("\nEnter a start 'x' co-ordinate for the line...");
/*    there is no data.p I guess you meant l.
  pointer->data.p.x1=atoi(gets(buff));
*/
  pointer->data.l.x1=atoi(gets(buff));
  printf("Enter a start  'y' co-ordinate for  the line...");
  pointer->data.l.y1=atoi(gets(buff));
  printf("\nEnter an end 'x' co-ordinate for the line...");
  pointer->data.l.x2=atoi(gets(buff));
  printf("Enter an end 'y' co-ordinate for  the line...");
  pointer->data.l.y2=atoi(gets(buff));
}
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
triangle (polygon), drawing, sizing, and rotation programme using linked lists... promsan C Programming Language 12 14-May-2007 15:03
Doubly linked lists in C++ sweeeeeeetlipss C++ Forum 1 24-Oct-2006 00:27
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
help on linked lists any1????? nick4 C Programming Language 1 17-May-2004 10:32
linked lists, newbie needs help moltarim C Programming Language 4 06-May-2004 12:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:40.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.