Return to beginning of code function?

I need help.

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 10223 Views - Last Post: 16 October 2010 - 07:07 AM Rate Topic: -----

#1 Upstepentertainment  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-October 10

Return to beginning of code function?

Posted 14 October 2010 - 07:24 PM

This is my first time here, please don't hate! :)

#include <iostream>
#include <string.h>
using namespace std;

int i;
int n;
int name;
int x;
int grade;

int main () {
	char name[100];

	cout <<"Welcome to the stupidity test. Please enter your name:";
	cin.getline(name, 99);
	grade=1;
	cout <<"Welcome" <<" " << name <<" " <<"We will begin momentarily" << endl << endl << endl << endl << endl;
	cout <<"Welcome to grade" <<" " << grade <<" " << name << endl;
	cout << name <<"," <<" " <<"What is 2 + 2?";
		cin >> n;
		if (n==4)
			cout <<"Congratulations" << name <<" That is correct";
		else
			cout <<"Sorry that is incorrect ";
		cout << name;


		
	return 0;
}



Here is the code for my little stupidity test program as i call it
It is far from finished so please just help me with the problem I have.

So my problem is...

What i want is, it asks you the question, if you get it right you coninue, if not you get it wrong and you return to the beginning of the project or to the beginning of the "Grade"
cout << name <<"," <<" " <<"What is 2 + 2?";
		cin >> n;
		if (n==4)
			cout <<"Congratulations" << name <<" That is correct";
		else
			cout <<"Sorry that is incorrect ";
		cout << name;


Here is my answer function i guess i will call it.

I need to know,

else
			cout <<"Sorry that is incorrect ";
		cout << name;


What to put here to get it to return to a specific part in the code. All help would be great, thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Return to beginning of code function?

#2 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Return to beginning of code function?

Posted 14 October 2010 - 07:47 PM

There are goto statements but don't use those they're not good practice to use anymore.

You could use loops or by calling a function. Have you learned about loops yet?

I need to know what part of the code you want it to go back to.

EDIT: Is this all your code? If not post it all.

This post has been edited by Alex6788: 14 October 2010 - 07:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 Upstepentertainment  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-October 10

Re: Return to beginning of code function?

Posted 14 October 2010 - 09:15 PM

View PostAlex6788, on 14 October 2010 - 06:47 PM, said:

There are goto statements but don't use those they're not good practice to use anymore.

You could use loops or by calling a function. Have you learned about loops yet?

I need to know what part of the code you want it to go back to.

EDIT: Is this all your code? If not post it all.


This is all the code i made so far for it, i know i should have done more, but all i really want it to do is. If you get the question right, you continue, if you don't; you return to the beginning of your "grade" or the beginning of the entire program either or, doesn't matter.
Was This Post Helpful? 0
  • +
  • -

#4 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Return to beginning of code function?

Posted 14 October 2010 - 09:53 PM

There are a lot of ways you could do it, this for loop will loop back to the beginning of the program if you get the wrong answer.

Made in browser.
#include <iostream>
#include <string> // You had this as <string.h> but the way it is now is correct.
using namespace std;

int i; // Try adding more meaningful name to variables so if you look back at this code in the future or someone else looks at it, it will make more sense.
int n;
int name;
int x;
int grade;

int main () {

	string name;

       for ( ;; )
       {// start of for loop

	cout <<"Welcome to the stupidity test. Please enter your name:";
	getline( cin,name );
	grade=1;
	cout <<"Welcome" <<" " << name <<" " <<"We will begin momentarily" << endl << endl << endl << endl << endl;
	cout <<"Welcome to grade" <<" " << grade <<" " << name << endl;
	cout << name <<"," <<" " <<"What is 2 + 2?";
		cin >> n;
		if (n==4)
                {
			cout <<"Congratulations" << name <<" That is correct";
                        break; // Forgot the break thanks dogstopper.
                }// Braces should be around you if and else statements like this.
		else
                {
			cout <<"Sorry that is incorrect ";
		cout << name;
                continue; //at top of forloop
                }


              } // end of for loop		

	return 0;
}




Hope that helps.

This post has been edited by Alex6788: 14 October 2010 - 10:07 PM

Was This Post Helpful? 1
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Return to beginning of code function?

Posted 14 October 2010 - 10:03 PM

I do think that a non-conditional while or for loop is the way to go here. You can use the break keyword to stop looping when your condition is met.

Alex6788 forgot the break keyword, so it will execute forever. To exit the loop, put a break statement after the correct coutput:

cout <<"Congratulations" << name <<" That is correct";
break;



Whilst a goto will technically work, it is better to avoid this in almost every case. There are reasons to use it, but that's a topic for another day.
Was This Post Helpful? 1
  • +
  • -

#6 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Return to beginning of code function?

Posted 14 October 2010 - 10:09 PM

View PostDogstopper, on 14 October 2010 - 11:03 PM, said:

I do think that a non-conditional while or for loop is the way to go here. You can use the break keyword to stop looping when your condition is met.

Alex6788 forgot the break keyword, so it will execute forever. To exit the loop, put a break statement after the correct coutput:

cout <<"Congratulations" << name <<" That is correct";
break;



Whilst a goto will technically work, it is better to avoid this in almost every case. There are reasons to use it, but that's a topic for another day.

Thanks, i knew it was supposed to be there but forgot to add it, i edited the code and thanked you in a comment beside the break.

This post has been edited by Alex6788: 14 October 2010 - 10:09 PM

Was This Post Helpful? 0
  • +
  • -

#7 Upstepentertainment  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-October 10

Re: Return to beginning of code function?

Posted 15 October 2010 - 06:54 AM

View PostAlex6788, on 14 October 2010 - 08:53 PM, said:

There are a lot of ways you could do it, this for loop will loop back to the beginning of the program if you get the wrong answer.

Made in browser.
#include <iostream>
#include <string> // You had this as <string.h> but the way it is now is correct.
using namespace std;

int i; // Try adding more meaningful name to variables so if you look back at this code in the future or someone else looks at it, it will make more sense.
int n;
int name;
int x;
int grade;

int main () {

	string name;

       for ( ;; )
       {// start of for loop

	cout <<"Welcome to the stupidity test. Please enter your name:";
	getline( cin,name );
	grade=1;
	cout <<"Welcome" <<" " << name <<" " <<"We will begin momentarily" << endl << endl << endl << endl << endl;
	cout <<"Welcome to grade" <<" " << grade <<" " << name << endl;
	cout << name <<"," <<" " <<"What is 2 + 2?";
		cin >> n;
		if (n==4)
                {
			cout <<"Congratulations" << name <<" That is correct";
                        break; // Forgot the break thanks dogstopper.
                }// Braces should be around you if and else statements like this.
		else
                {
			cout <<"Sorry that is incorrect ";
		cout << name;
                continue; //at top of forloop
                }


              } // end of for loop		

	return 0;
}




Hope that helps.



Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#8 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Return to beginning of code function?

Posted 15 October 2010 - 06:59 AM

You're welcome :-)
Was This Post Helpful? 0
  • +
  • -

#9 Upstepentertainment  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-October 10

Re: Return to beginning of code function?

Posted 15 October 2010 - 02:49 PM

View PostAlex6788, on 15 October 2010 - 05:59 AM, said:

You're welcome :-)

If your still here to look at this, can you give me a little code snippet of how to return to a different part of the project, kinda like using goto's like this?

#include <iostream>
using namespace std;

top:
cout <<"Hello";
goto top
;

Something like that in the loop function you showed me would be great, thanks again
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6056
  • View blog
  • Posts: 23,493
  • Joined: 23-August 08

Re: Return to beginning of code function?

Posted 15 October 2010 - 03:41 PM

You use a loop. Loops are a very important part of programming, you need to spend some time learning them. Give it a try, and if you run into trouble, then come back with the problems/errors.
Was This Post Helpful? 1
  • +
  • -

#11 Upstepentertainment  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-October 10

Re: Return to beginning of code function?

Posted 15 October 2010 - 04:08 PM

View PostJackOfAllTrades, on 15 October 2010 - 02:41 PM, said:

You use a loop. Loops are a very important part of programming, you need to spend some time learning them. Give it a try, and if you run into trouble, then come back with the problems/errors.


I know that, i have touched on them before, not to the extent that is needed to finish this though.
Was This Post Helpful? 0
  • +
  • -

#12 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Return to beginning of code function?

Posted 15 October 2010 - 04:29 PM

Quote

i have touched on them before, not to the extent that is needed to finish this though.
To what extent do you need to touch upon loops? They are a very fundamental and basic concept. It's not like you are studying monads or something.

Besides, whatever the complexity of loops, you still have to study them. If you aren't familiar enough with the material, shouldn't you then be cracking open a book and reading about it? Not asking us for the code?
Was This Post Helpful? 1
  • +
  • -

#13 Upstepentertainment  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-October 10

Re: Return to beginning of code function?

Posted 15 October 2010 - 05:12 PM

View PostOler1s, on 15 October 2010 - 03:29 PM, said:

Quote

i have touched on them before, not to the extent that is needed to finish this though.
To what extent do you need to touch upon loops? They are a very fundamental and basic concept. It's not like you are studying monads or something.

Besides, whatever the complexity of loops, you still have to study them. If you aren't familiar enough with the material, shouldn't you then be cracking open a book and reading about it? Not asking us for the code?


First of all. I haven't learnt all functions that can be executed in a loop. And sure, i should crack open a book, but i thought i would try HERE. Because i thought that kinda might be the point of forums. But i guess your right, i'm wrong. And no i wasn't asking you, i was specifically asking the amazing guy who helped me out before. If your not willing to help, i dont see why you posted here.
Was This Post Helpful? 0
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6056
  • View blog
  • Posts: 23,493
  • Joined: 23-August 08

Re: Return to beginning of code function?

Posted 15 October 2010 - 05:44 PM

We have a tutorial on loops. They're really not hard to understand, and there's no function that CAN'T be executed in a loop. We're really not here to feed you code so you can pass in your assignments then fail the test because you don't understand it; we're here to try to help you understand it so you PASS THE TESTS you will inevitably need to pass to get through your course successfully.
Was This Post Helpful? 0
  • +
  • -

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Return to beginning of code function?

Posted 15 October 2010 - 05:44 PM

View PostUpstepentertainment, on 16 October 2010 - 09:12 AM, said:

i was specifically asking the amazing guy who helped me out before.


The key point is that the code Alex6788 gave you does exactly what your question at 06:49AM asked.

Without reading the book to understand why what Alex6788 gave you works you won't be able to see how to apply it usefully. Which is exactly what has happened.

That's why, in the long run, the advice Oler1s is giving you is more helpful than the code that Alex6788 gave you. One gives you a fish dinner, one is helping you learn to fish. There is nothing wrong with a good free fish dinner but learning to fish is even better.

EDIT
Thought I better make it clear I don't think anything Alex6788 posted was not good help and all that good stuff. No intended suggestion otherwise. Too easy here to offend people who "read between the lines" and draw the wrong conclusions.

This post has been edited by janotte: 15 October 2010 - 06:48 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2