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
  #41  
Old 17-May-2007, 18:16
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
Unhappy

Switch doesn't work?


No errors at all on compile now, but my "switch within a switch" just won't "work"
I can select "New Shape" by pressing one, and then return; then it displays nothing and lets me tap in return twice and then closes.
Here's the switch:
CPP / C++ / C Code:
	do 
    {
	choice=getch();
	if(choice == 0 )
	choice=getch();
	switch(choice)
		{
		case 1:
			{
			int shape_array[1][5]={1,2,3,4,5};

			char Menu1[]="Line";
			char Menu2[]="Circle";
			char Menu3[]="Triangle";
			char Menu4[]="Square";
			char Menu5[]="Exit";

			printf("New shape:\n");
			printf("1.Line\n2.Circle\n3.Triangle\n4.Square\n");
			printf("Enter your choice: ");
			scanf("%d",&shape_select);
			printf("\n");
				do 
				{
				shape_select=getch();
				if(shape_select == 0 )
				shape_select=getch();
				switch(shape_select)
					{
						case 1:
						{
						/* Size Selection */
						printf("please enter a number for the size of your shape:\n");
						scanf("%d",&size);
					    /* 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);
						/* 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;
						case 2:
						{
						/* 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);
						}
						case 3:
						{
						/* Triangle Construction */
						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;
						case 4:
						{
						printf("Quadrilateral");
						}
						break;
					}
				}
			while(shape_select!=13);
			getch();
			cleardevice();
		}
		break;
		case 2:
			{
			printf("Scaling:\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);
			//told(point1,point2,point3,0);
			//new_triangle(newpoint1,newpoint2,newpoint3,5,1);
			//copyvalue();
			}
		break;
		case 3:
			{
			printf("Rotation:\n");
			printf("please enter an angle to rotate your shape by\n");
			getch();
			printf("angle:\n");
			scanf("%d",&theta);
			getch();
			printf("press any key to continue\n");
			}
		break;
		case 4:
			{
			printf("Instruxions:\n");
			getch();
			printf("Return to menu? (Y/N):\n");
			//scanf("%c",&key_select);
			}
			break;
		case 5:
			{
			printf("bye bye!:\n");
			printf("Return to menu? (Y/N):\n");
			//scanf("%c",&key_select);
			getch();
			}
		}
	}
	while(choice!=13);
  #42  
Old 17-May-2007, 18:21
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
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:
any ideas why?
Yes , you are not building, compiling and running a program in a logical progression.
You are violating basic rules of C.

Look at your errors. What does "initialized" mean. (look it up)

Describe crash.
- An alert box informing of fatal blah blah and has to shut down the program and you get a command line?
- cursor sits there and your input doesn't do anything.

- If the former your probably trying to store data outside of your programs' allocated user space.
- If the latter it's probably an endless loop.

look for it...
  #43  
Old 17-May-2007, 18:26
shalombi shalombi is offline
Junior Member
 
Join Date: Feb 2007
Posts: 47
shalombi is on a distinguished road

Re: Linked Lists advice request


Ok, here are some debugging tips that will greatly help you.

In each case add a printf telling you where the program entered and what the values are, try to get in each menu, see which work which don't which values you have and what they are compared to, rubbish values will indicate out of bound variables (if you're lucky).

If you want to debug your menu easily just compile the menu separately removing all the stuff in between and see how it works, once you get the prototype working just adjust your program.

This is by far simpler than what you've been throwing around before, try to understand the way switches work.

Max
  #44  
Old 18-May-2007, 01:19
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
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


Nothing yet eh?.... consider this-
CPP / C++ / C Code:
/*  func_menu_case.c  morning excercises: ...some things to think about.
Nested switches can get very confusing , even a simple example like this:
- try removing the break after case1-1
- try 2-2
*/

#include <stdio.h>
int menu(void);

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

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

int menu(void)
{
  int i, choice1, choice2;
  i = choice1 = choice2 = 0;

  printf("Menu1: select a choice1 from this list: \n");
  printf("(1) choice 1-1 \n");
  printf("(2) choice 1-2 \n");
  printf("(3) choice 1-3 \n");
  printf("(4) choice 1-4 \n");
  printf("(5) choice 1-5 \n");
  printf("Enter choice: ");

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

    switch(choice1) {
      case 1: {
        printf("You selected 1-1, now select a choice2 from this list: \n");
        printf("(1) choice 2-1 \n");
        printf("(2) choice 2-2 \n");
        printf("(3) choice 2-3 \n");
        printf("(4) choice 2-4 \n");
        printf("(5) choice 2-5 \n");
        printf("Enter choice: ");

        while(choice2 < 1 || choice2 > 5) {
          scanf("%d", &choice2);
          switch(choice2) {
            case 1:
              printf("You selected 2-1 \n");
              break;
            case 2:
	          printf("You selected 2-2  ...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 2-3 \n");
              break;
            case 4:
              printf("You selected 2-4 \n");
              break;
            case 5:
              printf("You selected 2-5 \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 1-2 , choice1 = %d \n", choice1);
        break;
      case 3:
        printf("You selected 1-3 \n");
        break;
      case 4:
        printf("You selected 1-4 \n");
        break;
      case 5:
        printf("You selected 1-5 \n");
        break;
      default:
        printf("You entered wrongly in menu 1, try again \n");

    }  //close switch1
  }    //close while1
  return 0;
}      //close function
...with that firmly in hand, start adding the even more confusing shape manipulations ONE (1) ((uno)) at a time , compile , run , fix errors , repeat until COMPLETELY satisfied ,
Then and ONLY then... move to the next. Do not pass go...
++Howard;
  #45  
Old 18-May-2007, 02:46
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


The crash, was definitely something to the outtextxy part of the code, as it didn't happen when I commented all the outtextxy stuff out.
What happened was:
1. the prog executed and displayed the menu as normal
2. i entered "1" and pressed "return"
3. The cursor moved below the menu and changed - I think it resembled the way text changes in appearance when you used outtextxy (looking a bit like the text display on an old Commodore 64),
4. I pressed "return" twice, the "press any key" printf was displayed,
(4.a. I pressed "return", a "windows error report" came up... but doesn't happen now)
5. the prog closed.
I wonder if the outtextxy thing needs the screen clearing before it can display?
I tried inserting a cleardevice to remove the menu after the selection, but it had no effect.

Thanks again Howard - I've learnt more about C these last few days than I have in the last few months!

I think the switch needs rearranging, thus:

select_shape_switch -> draw shape
then
switch_between: move_shape_switch (use 4 cursors); size_shape_switch; rotate_shape_switch; colour_select_switch (all use 2 cursors).
...of course if that's prone to problems, then I suppose having a sequential list of switches is probably fine.

(the problem is not that I'm not capable of looking stuff up and working logically and sequentially; it's just that I get very little time to play with this (wife & kid) - which makes me rush things because I can't rely on having enough time to carry on with summat... plus I'm on a different timezone to most of you... sorry for complicationalerising things)
  #46  
Old 18-May-2007, 09:12
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
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


Then wouldn't it make sense to make it Quality time with either?

Playing around with simple modules and realizing what they demonstrate can actually reduce time and hair-tear in the long run by increasing your intuition of what might cause a particular problem even before you write it into the code or after you are getting errors. You might then go spend time with your family with a smile instead of a frown. ...and after all, isn't that what it's all about? smiling?

I urge you to try playing with the simple nested switch I posted and observe behavior. Inserting the below should give you an idea of what might be causing the alert-box-failure.
Help us help you , , run the code Luke...
CPP / C++ / C Code:
            case 3:
              printf("You selected 2-3 \n");
              printf(" here we go with a segfault.... see where we run out of user space: \n");
              int * iptr = &choice1;
              for(i = 0; i < 100; i++) {
                printf(" %d:%p ", i, iptr);
                *iptr++ = 5;
              }
              break;
If the i < 100; doesn't do it increase it. Mine started a 5 at first, I think the OS has increased the space for this program name after pushing the limit, now 100 won't get an alert box but cursor still sits waiting for nothing.... 200 now gets the box after a few second pause , strange.
Anyhow , that's basically how you get a segfault , to my knowledge. Any Others?
Take your time and have fun...
Last edited by Howard_L : 18-May-2007 at 09:55.
  #47  
Old 18-May-2007, 10:12
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


Alright Yoda, "code the run I shall"

Actually, this morning when I posted that, the rib was ullulating at me from the kitchen whilst his majesty (the toddler) was bawling, so I'm forever getting interrupted, that's the thing - I've just done her maths homework for her to get left alone for this aft' so I can play with the code you posted, and get summat that works by next Choose-day.

I was just thinking (for a change), could I put another .c file in the workspace called "test.c" to run bits of code without any digital flatulence from my computer?
Quote:
to answer my own question... apparently not... it didn't like it! but just pasting it over the shapeshifter one: it works fine

I'm going to dump all that silly outtextxy stuff and just stick to a basic array and printf menu with the switches.
The only other issue is my rotation matrix code implementation posted earlier - you said it was confusing? is it? does it not make sense?

thanks again for your endless patience, may the farce be with you.

It spat out that modification to Case 2-3 (I did put it in the right place... when I declared iptr, it when into an eternal build loop, and I had to do ctrl+alt+del)
CPP / C++ / C Code:
--------------------Configuration: shapeshifter - Win32 Debug--------------------
Compiling...
shapeshifter.c
C:\Documents and Settings\...\shapeshifter\shapeshifter\shapeshifter.c(23) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\...\shapeshifter\shapeshifter\shapeshifter.c(60) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\...\shapeshifter\shapeshifter\shapeshifter.c(62) : error C2065: 'iptr' : undeclared identifier
C:\Documents and Settings\...\shapeshifter\shapeshifter\shapeshifter.c(63) : error C2100: illegal indirection
C:\Documents and Settings\...\shapeshifter\shapeshifter\shapeshifter.c(63) : error C2106: '=' : left operand must be l-value
Error executing cl.exe.

shapeshifter.obj - 5 error(s), 0 warning(s)
  #48  
Old 18-May-2007, 11:44
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
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:
... my rotation matrix code implementation posted earlier ..... does it not make sense?
This is what I'm talking about! Nobody knows what the heck to be looking at.
You included that snippet years ago along with a bunch of other stuff that didn't work.
I don't know what the best way to manipulate a shape would be. Haven't thought about it that much. I'm green at this too and just TRYING to follow along with what you're trying to do. That's why it would be nice if you could keep it a simple as possible and focus on one thing instead of cramming a bunch of stuff in there and wondering why it doesn't work...

I would suggest a logical approach like this (again):
Phase 1:
- make one object like a triangle because it only has three points.
. . Don't worry about user definitions at this point, define it in the program.
. . make it compile and run until your satisfied with it.

Phase 2:
- try a simple manipulation like clear the window and redraw in a different location.
. . I guess use a getch() to stop it at first position or #include <time.h> sleep()

Phase 3:
- try successive moves , like start it on the left and move it across the screen.

- Keep your code as simple as possible and orderly.
- Post with problems your having with accomplishing those "simple" tasks including the full code of as far as you can get , noting where how far you had gotten succesfully and then what you tried to add that caused problems.
- After you've sent us a succesfull compile of that we can talk about rotation.
. . we will all be on the same page.
- Also, don't try to "plug" other people code in.
. . Instead "realize" how they work and write your own.
Now, go to it man, don't even think about asking about phase 4 until this is done!
  #49  
Old 18-May-2007, 11:47
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


Quote:
--------------------Configuration: shapeshifter - Win32 Debug--------------------
Compiling...
shapeshifter.c
C:\Documents and Settings\...\shapeshifter\shapeshifter\shapeshifte r.c(59) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\...\shapeshifter\shapeshifter\shapeshifte r.c(62) : error C2100: illegal indirection
C:\Documents and Settings\...\shapeshifter\shapeshifter\shapeshifte r.c(62) : error C2106: '=' : left operand must be l-value
Error executing cl.exe.

shapeshifter.obj - 3 error(s), 0 warning(s)
(sorry it just blocked my last edit)
...for this code
CPP / C++ / C Code:
(59)              int * iptr = &choice1;
(60)              for(i = 0; i < 100; i++) {
(61)                printf(" %d:%p ", i, iptr);
(62)                *iptr++ = 5;

tsk
  #50  
Old 18-May-2007, 11:49
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: "bunches" of "stuff"


the now palaeolithic rotation thing I was on about is on page 4 (#36) - I won't repost it, to keep you calm.

I am focussing on the switches now... the extra bit in the case won't work, so I s'pose I'll just leave it, and start using it as a guide to rebuild my own switches.
 
 

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 18:17.


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