12 Replies - 941 Views - Last Post: 24 March 2009 - 09:52 PM Rate Topic: -----

#1 hawkysu  Icon User is offline

  • I am a spoon
  • member icon

Reputation: 5
  • View blog
  • Posts: 1,432
  • Joined: 20-February 09

Goto

Posted 23 March 2009 - 08:32 PM

Is there a way to use the goto line in C? I know there's a way to do it in C++. I am writing a lecture/project for my english class and I want to be able to show the different types of sentences or quit by pressing the corresponding numbers: 1, 2, 3, 4, 5, 0.

Would i just be able to type Goto([enter line number here]); ?

and the lecture/project thus far:
#include "stdio.h"
#include "genlib.h"
#include "simpio.h"

main()
{
	  int i;
	  printf("Enter 1 for the first Sentence type, 2 for the second 3 for third, 4 for fourth,5 for fifth and 0 to quit\n");
	  i=GetInteger();
	  if(i=1){
	  printf("I. Sentence Type 1: Intrsitive verb\n\n");
	  printf("Definition: A sentence type that requires no more than a subject and a verb\n\n");
	  printf("Sue ran.\nSubject: Sue\nVerb: ran\n");
	  printf("Sue | ran\n");
	  printf("----|------\n\n");
	  printf("Joe listened intently\nSubject: Joe\nVerb: listened\n");
	  printf("Joe | listened\n");
	  printf("----|----------\n");
	  printf("		\\intently\n");
	  printf("		  \\ \n\n");
	  printf("People live\nSubject: People\nVerb: live\n");
	  printf("People | live\n");
	  printf("-------|-------\n");
	  goto 9;
	  }
	  if(i=2){
			  }
	  if(i=3){
			  }
	  if(i=4){
			  }
	  if(i=5){
			  }
	  if(i=0){
	  system("pause");
	  }
	  }


as you can see, it's not a complete lecture/project.

Is This A Good Question/Topic? 0
  • +

Replies To: Goto

#2 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Goto

Posted 23 March 2009 - 08:35 PM

#include <iostream>
using namespace std;

int main() {

    int Counter = 0;

    MyLoop:
    if (Counter < 5) { Counter++; goto MyLoop; }

    return 0;
}


Tada! 100% C
Was This Post Helpful? 1

#3 hawkysu  Icon User is offline

  • I am a spoon
  • member icon

Reputation: 5
  • View blog
  • Posts: 1,432
  • Joined: 20-February 09

Re: Goto

Posted 23 March 2009 - 08:41 PM

haha, if only that was C.... wait... are C and C++ interchangeable? I mean some stuff works the same in both...
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Goto

Posted 23 March 2009 - 08:50 PM

View PostHyper, on 23 Mar, 2009 - 09:35 PM, said:

Tada! 100% C


uhm, that code was C++
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5908
  • View blog
  • Posts: 12,814
  • Joined: 16-October 07

Re: Goto

Posted 24 March 2009 - 04:36 AM

First, Hyper's example is a valid use of a goto in C or C++.

Second, it doesn't quite sound like you're really looking for a goto. Maybe something like this:
#include "stdio.h"
#include "genlib.h"
#include "simpio.h"

void printSelection(int i) {
	if (i==1) {
		printf("I. Sentence Type 1: Intrsitive verb\n\n");
		printf("Definition: A sentence type that requires no more than a subject and a verb\n\n");
		printf("Sue ran.\nSubject: Sue\nVerb: ran\n");
		printf("Sue | ran\n");
		printf("----|------\n\n");
		printf("Joe listened intently\nSubject: Joe\nVerb: listened\n");
		printf("Joe | listened\n");
		printf("----|----------\n");
		printf("		\\intently\n");
		printf("		  \\ \n\n");
		printf("People live\nSubject: People\nVerb: live\n");
		printf("People | live\n");
		printf("-------|-------\n");
	} else if (i==2) {
		printf("Coming soon.\n");
	} else if (i==3) {
		printf("Coming soon.\n");
	} else if (i==4) {
		printf("Coming soon.\n");
	} else if (i==5) {
		printf("Coming soon.\n");
	} else {
		printf("Please enter a valid value\n");
	}
}

int main() {
	int i;
	for(;;)/> { // forever loop ( if you like, you'd use goto for the forever loop.  goto is bad, so don't )
		printf("Enter 1 for the first Sentence type, 2 for the second 3 for third, 4 for fourth,5 for fifth and 0 to quit\n");
		i=GetInteger();
		if (i==0) { break; } // if 0, leave loop, done
		printSelection(i);
	}
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Goto

Posted 24 March 2009 - 01:27 PM

"Is there a way to use the goto line in C?"
He wasn't asking for a better program, he was asking if you could use goto in C; Which you can.

On a side note, while (true) is SO much more appealing that for (;;) although both work fine.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5908
  • View blog
  • Posts: 12,814
  • Joined: 16-October 07

Re: Goto

Posted 24 March 2009 - 01:54 PM

View PostHyper, on 24 Mar, 2009 - 02:27 PM, said:

He wasn't asking for a better program, he was asking if you could use goto in C; Which you can.

On a side note, while (true) is SO much more appealing that for (;;) although both work fine.


True, but since an actual goto example caused confusion, it seemed appropriate to offer an alternative.

Appealing... perhaps, but in C, without stdbool.h, it would be while (1) or while (!0), both of which are no less wonky than for (;;). Since the latter form saves a memory location, it's oft preferred by the more anal C crowd.
Was This Post Helpful? 0
  • +
  • -

#8 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Goto

Posted 24 March 2009 - 01:56 PM

Could just #define TRUE 1 no? And ok.
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Goto

Posted 24 March 2009 - 02:12 PM

Yes you can use goto in C or C++. You should *TRY* to avoid using goto as in general it is a bad practice.

Generally to create a goto you create a label (an identifier with a colon at the end) and then use goto label;

So for a truly 100% C version
#include <stdio.h> 

int main() {
	int count = 0;
	//while(count++ < 10) { printf("%d\n",count); }
	label1:
	if (count++ < 10) {
		printf("count = %d\n", count);
		goto label1;
	}
	
	//do { count--; printf("%d\n", count); } while(count > 0);
	label2:
	count--;
	printf("count = %d\n", count);
	if (count > 0) { goto label2; }


	//for(count = 10; count > 1; count/= 2) { printf("%d\n); }
	count = 128;
	label3:
	if(count > 1) {
		printf("count = %d\n", count);
		count /= 2;
		goto label3;
	}

	return 0;
}


Note to Hyper: iostream and namespaces are C++ not C.

While while(true) may be prettier than for(;;) historically the reason for choose one over the other was that while(true) is ever so slightly slower since it does a check to see if true==true (the for has no such check). Modern compilers should optimize and leave out the check but you can always check your compiler output to see.
Was This Post Helpful? 0
  • +
  • -

#10 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Goto

Posted 24 March 2009 - 02:19 PM

I'm aware iostream is apart of C++ and not C. stdio is apart of C. It's just a force of habit to use those two. It's not like I needed them; Didn't output any text and never planned on it.

The "100% C" part I was refering to was the usage of the goto statement. Speaking of which, an easier way of getting the point across is something like this.
Was This Post Helpful? 0
  • +
  • -

#11 hawkysu  Icon User is offline

  • I am a spoon
  • member icon

Reputation: 5
  • View blog
  • Posts: 1,432
  • Joined: 20-February 09

Re: Goto

Posted 24 March 2009 - 09:20 PM

You know
I think I'll just stick to repeating the i=GetInteger();
Was This Post Helpful? 0
  • +
  • -

#12 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Goto

Posted 24 March 2009 - 09:23 PM

Summary: Loops are prefered over goto statements.
Functions + Loops = easier to read/manage code.
Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Goto

Posted 24 March 2009 - 09:52 PM

View PostHyper, on 24 Mar, 2009 - 11:23 PM, said:

Summary: Loops are preferred over goto statements.
Functions + Loops = easier to read/manage code.

:) exactly!

Your instincts tell you that "there has to be a better way" -- so you tied to use goto... but there are better ways than using goto. For example baavgai's code uses a function to display the text. There are even better ways... follow you instincts... its really not all that complicated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1