Jump to content



Photo

swich, no output


  • Please log in to reply
26 replies to this topic

#16 OP Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 01 October 2013 - 22:51

Orangekillerx and Asik, thank you for explaining that to me.  Apparently what I was reading was just some guy full of hot air trying to explain something he knew nothing about.  I really appreciate you taking the time to explain this to me in terms that I'm grasping, slowly, but hey it's still progress!  Thank you again!




#17 OP Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 01 October 2013 - 23:06

Okay one more question about this orangekillerx.  How does this work:

    enter_part_id:9

 

I've never seen this done before.  Could you please explain it to me?



#18 +Karl L.

Karl L.

    xorangekiller

  • Tech Issues Solved: 15
  • Joined: 24-January 09
  • Location: Virginia, USA
  • OS: Debian Testing

Posted 01 October 2013 - 23:20

Okay one more question about this orangekillerx.  How does this work:

    enter_part_id:9

 

I've never seen this done before.  Could you please explain it to me?

 

If you are referring to the iteration of your code I posted yesterday in post #10, enter_part_id: is a label. It literally does nothing by itself. In fact, the compiler doesn't translate it into any machine instructions at all, even a nop (no operation - waste a CPU cycle). However, it marks a place in the code that you can return to later in the function (and this is an important limitation - only within the same function) using a goto statement. So in the program in the aforementioned post, the goto statement on line 38 references the label on line 12. So in the default "part cannot be found" case of the partID switch, the goto statement sets the program counter back to line 12 (rather than incrementing it to line 39 like would have happened otherwise) so the user is prompted to enter a partID again.



#19 OP Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 01 October 2013 - 23:30

Oh okay!  Thanks!  That makes a lot of sense.  I thought that might be something it was doing, but I wanted to get confirmation first before I tried messing around with it. 



#20 OP Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 01 October 2013 - 23:45

Okay, so I tried to do something similar to what you did orangekillerx .  I tried modifying what you had done and tried to use it for answers to a monty python skit from "the holy hand grenade".  here is what I have:

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

int main()
{
	unsigned int num;
	int monty_python;
	monty_python = 0;

	enter_monty_python:	
	printf("What number doest thou wish, between 1 and 5 (0 to exit):\n", num);
	scanf("%d", &num);

	switch(monty_python)
		{
	case 1:
		printf("One shall be the number thou shalt count, and the number of they counting shall be three.\n", monty_python);
		break;
	case 2:
		printf("Neither thou count Two, 'cepting that thou then proceed to Three.\n", monty_python);
		break;
	case 3:
		printf("Three shall be the number thouh shalt count, and the number of thy counting shall be three.\n", monty_python);
		break;
	case 4:
		printf("Four thou shall not count, neither count til two.\n", monty_python);
		break;
	case 5:
		printf("Five is RIGHT OUT.\n", monty_python);
		break;
		case 0:
		printf("Amen\n", num);
		break;
	default:
		printf("That is not a correct choice, please try again\n");
		break;
		goto monty_python;
	}
	return 0;


}

However.  Its telling me that I am referencing monty_python but it is not defined.  before I did that I tried:

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

void main()
{
	int num = 0;
	switch(num)
	do{
	

	printf("What number doest thou wish, between 1 and 5 (0 to exit):\n");

	case 1:
		printf("One shall not be the number thou shalt count, and the number of they counting shall be three.\n");
		num++;
		break;
	case 2:
		printf("Neither thou count Two, 'cepting that thou then proceed to Three.\n");
		num++;
		break;
	case 3:
		printf("Three shall be the number thouh shalt count, and the number of thy counting shall be three.\n");
		num++;
		break;
	case 4:
		printf("Four thou shalt not count, neither count til two.\n");
		num++;
		break;
	case 5:
		printf("Five is RIGHT OUT.\n");
		num++;
		break;
	default:
		printf("That is not a correct choice, please try again\n");
		num++;
		break;
	case 0:
		printf("Amen\n");
		num++;
		break;
	}

	while (num != 0);
	


}

neither of which are working.  where am I messing up?  I've tried it more than these two ways and I'm just not getting this at all.  I figured that if I kept trying different ways to do the same things that eventually I'd pick it up, but these switches are just not sinking in.   On the second one I'm only getting the output of Amen, which means its going directly to 0 and exiting. 



#21 +Karl L.

Karl L.

    xorangekiller

  • Tech Issues Solved: 15
  • Joined: 24-January 09
  • Location: Virginia, USA
  • OS: Debian Testing

Posted 01 October 2013 - 23:49

I feel like I have just given you a dangerous weapon, and I would be remiss if I did not mention the reason that many professors (and even many C textbooks) do not discuss goto at all. The goto statement is considered by some to be a relic of a by-gone age. It is a critical construct in most assembly languages, but is largely obviated by more specialized - not to mention less error-prone - constructs in high-level languages like C. For example, the way I re-wrote the program from post #10 in post #12 would most likely be considered the "correct" way to write that program by goto detractors. The reason the goto statement has acquired such a venomous reputation that many do not teach it and even more strongly believe that it should be avoided at all costs, is because programmers have a tendency to abuse it to write spaghetti code. Although it is occasionally the best way to structure your control logic, it is generally a good idea to avoid using goto unless the alternatives would make your code significantly more complicated.



#22 OP Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 01 October 2013 - 23:52

LOL!  Okay, forget the first set of my previous post and ill work on the second.  My switch loop is going directly to the 0 (Amen) phase and exiting.  What did I miss because I'm just not seeing it. 



#23 OP Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 01 October 2013 - 23:57

Wait!  I went through and tried this...but its still ignoring everything but the case 0.  :

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

void main()
{
	int num = 0;
	switch(num)
	do{
	

	printf("What number doest thou wish, between 1 and 5 (0 to exit):\n", num);
	scanf("%d", &num);

	case 1:
		printf("One shall be the number thou shalt count, and the number of they counting shall be three.\n", num);
		num++;
		break;
	case 2:
		printf("Neither thou count Two, 'cepting that thou then proceed to Three.\n", num);
		num++;
		break;
	case 3:
		printf("Three shall be the number thouh shalt count, and the number of thy counting shall be three.\n", num);
		num++;
		break;
	case 4:
		printf("Four thou shall not count, neither count til two.\n", num);
		num++;
		break;
	case 5:
		printf("Five is RIGHT OUT.\n", num);
		num++;
		break;
	default:
		printf("That is not a correct choice, please try again\n", num);
		num++;
		break;
	case 0:
		printf("Amen\n", num);
		num++;
		break;
	}
		while (num != 0);
}

Attached Images

  • 3-3 printout.png


#24 +Karl L.

Karl L.

    xorangekiller

  • Tech Issues Solved: 15
  • Joined: 24-January 09
  • Location: Virginia, USA
  • OS: Debian Testing

Posted 02 October 2013 - 00:08

You have several mistakes in this new program. To be honest, I'm a little surprised that it compiles. Your biggest problem is that your switch statement should be inside of your loop, not outside. In the interest of reducing redundant code and simplifying your logic, you should probably put num++ outside of your switch case rather than repeating it in every case statement. Additionally, why are you incrementing num at all? Since you require your user to enter a new number (overwriting num) before it is used again, incrementing it seems wholly useless.



#25 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 02 October 2013 - 00:39

I'm not even sure what this program is supposed to mean.  :/ Stop, think and go through this logically.

 

Start by writing it out in whatever pseudo-code:

Initially set user input to something other than 0
WHILE user input is not 0
     get user input
     SWITCH on user input:
         | 0 -> print "Amen"
         | 1 -> print "One shall be..."
         (...)
         | default -> print "that is not a valid entry"

Can you run this mentally and see why it works? If so it should be obvious where your implementation is wrong.



#26 OP Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 02 October 2013 - 19:21

Okay, after much thought and concentration, I FIXED IT!!! BOOYEAH!  (sorry about the excitement, but I'm so happy!!!)

 

Here is what I did:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
 
void main()

{

	unsigned int num;
	printf("What number doest thou wish, between 1 and 5 (0 to exit):\n");
	scanf("%d", &num);

	switch(num)

{
	case 1:
	printf("One shall be the number thou shalt count, and the number of they counting shall be three.\n", num);
	break;

	case 2:
	printf("Neither thou count Two, 'cepting that thou then proceed to Three.\n", num);
	break;

	case 3:
	printf("Three shall be the number thouh shalt count, and the number of thy counting shall be three.\n", num);
	break;

	case 4:
	printf("Four thou shall not count, neither count til two.\n", num);
	break;

	case 5:
	printf("Five is RIGHT OUT.\n", num);
	break;

	case 0:
	printf("Amen\n", num);
	break;

	default:
	printf("That is not a correct choice, please try again\n");
	break;
}

}

Everything works flawlessly thanks to your help and a friend of mine helping me figure out where I went wrong.  Again, thank you for all of your help and patience with me.  This has not been easy for me to go from pure user with no experience whatsoever to writing code.



#27 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 02 October 2013 - 20:43

Well now you don't have a loop anymore, so the program only ever asks one question: is this what you want? Looks like you were confusing loops and switch statements and never really intended to use a loop in the first place. Anyway, glad you figured it out. :)