Is It Possible To Return to a certain line?

I need my program to return to a certain line if something is not tru

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2813 Views - Last Post: 19 July 2013 - 08:27 AM Rate Topic: -----

#1 CodingNewb  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 229
  • Joined: 04-December 09

Is It Possible To Return to a certain line?

Post icon  Posted 09 December 2009 - 07:18 PM

All I need to know is what syntax belongs where I have my last comment in this code? I want the program to run again from line 7
if(yesorno != yes). Please post how I can do this, or a revised version of the code.

#include <stdafx.h>
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
	//This states all of the variables that will be used in the code.
	int division;
	int addition;
	int multiplication;
	int subtraction;
	int yesorno;
	int operation;
	int number1;
	int number2;
	
	//This states the forumula for each variable that will not be defined later
	division = number1 / number2;
	addition = number1 + number2;
	multiplication = number1 * number2;
	subtraction = number1 - number2;
 
	//Asks the user what they want to do
	cout << "Would you like to add, subtract, multiply, or add?";
	cout << "\n";
	cin >> operation;
	
	//Double checks with the user with what they want to do
	cout << "So you would like to ";
	cout << operation;
	cout << "?";
	cout << "YES/NO";
	cout << "\n";
	cin >> yesorno;

	//This decides the course of the user


 
	if(yesorno != yes) //What Do I put here to make the program to go back to line 7?

	if(yesorno = yes) cout << "What is the number you want to start out with?";


This post has been edited by CodingNewb: 09 December 2009 - 07:39 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Is It Possible To Return to a certain line?

#2 CodingNewb  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 229
  • Joined: 04-December 09

Re: Is It Possible To Return to a certain line?

Posted 09 December 2009 - 07:37 PM

I can't find help anywhere. In fact this thread with no replies shows up when you Google. I need this to finish my program, write a few snippets, and help get the basics down. If you need my question clarified, just ask.
Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Is It Possible To Return to a certain line?

Posted 09 December 2009 - 07:50 PM

Start reading about loops.

Quote

Please post how I can do this, or a revised version of the code.
No. We don't hand out code freebies.

Quote

I need this to finish my program, write a few snippets, and help get the basics down.
Perhaps. But we don't particularly care about your program. We care that you learn something. Us finishing a program for you is not educational.

This post has been edited by Oler1s: 09 December 2009 - 07:52 PM

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5306
  • View blog
  • Posts: 27,200
  • Joined: 10-May 07

Re: Is It Possible To Return to a certain line?

Posted 09 December 2009 - 07:55 PM

Why are you using a conditional if? If you want the code to run multiple times, use a while or for loop.

& what does the Google placement of this page have anything to do with you getting or not getting help?

View PostCodingNewb, on 9 Dec, 2009 - 08:37 PM, said:

I can't find help anywhere.

Why don't you show us what you've tried & why it didn't work.

Complaining isn't going to get you help any faster. The topic has not even been posted for an hour, & you've already bumped it. Horrible.
Was This Post Helpful? 0
  • +
  • -

#5 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Is It Possible To Return to a certain line?

Posted 09 December 2009 - 08:09 PM

You really do need to use loops. Just like saying

while (this condition is true)
   do this stuff here



This post has been edited by Aphex19: 09 December 2009 - 08:09 PM

Was This Post Helpful? 0
  • +
  • -

#6 srlester  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 53
  • Joined: 28-January 09

Re: Is It Possible To Return to a certain line?

Posted 09 December 2009 - 08:25 PM

Im pretty sure line 7 is int main() and if it is than this shouldnt be much of a problem. Just call the main function again.
 if(conditons) 
{
main();
}


But a do while loop would probably be cleaner.

This post has been edited by srlester: 09 December 2009 - 08:26 PM

Was This Post Helpful? 0
  • +
  • -

#7 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: Is It Possible To Return to a certain line?

Posted 10 December 2009 - 01:57 AM

View Postsrlester, on 10 Dec, 2009 - 03:25 AM, said:

Im pretty sure line 7 is int main() and if it is than this shouldnt be much of a problem. Just call the main function again.
 if(conditons) 
{
main();
}


It would also be illegal; A program which calls main produces this error message on a conforming C++ compiler in strict mode

Quote

error: function "main" may not be called or have its address taken

This error was produced using the comeau compiler http://www.comeaucom...g.com/tryitout/

This post has been edited by Bench: 10 December 2009 - 02:00 AM

Was This Post Helpful? 0
  • +
  • -

#8 CodingNewb  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 229
  • Joined: 04-December 09

Re: Is It Possible To Return to a certain line?

Posted 10 December 2009 - 04:23 AM

I was trying to do what srlester said.
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: Is It Possible To Return to a certain line?

Posted 10 December 2009 - 07:28 AM

Look the process is very simple:

int main() {
bool loopAgain = true;
{
//... do whatever you need to...
//to exit the loop set loopAgain to false. 
} while (loopAgain );


of course loopAgain does not have to be a variable itself, you can use any condition expression (such as yesorno == 1 -- note yesorno is defined as an int -- so the user will have to enter a number and not a string such as "yes" -- if you want the user to be able to enter a string you will have to change your program).
Was This Post Helpful? 0
  • +
  • -

#10 CodingNewb  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 229
  • Joined: 04-December 09

Re: Is It Possible To Return to a certain line?

Posted 10 December 2009 - 12:37 PM

Thank you........it basically boiled down to me not knowing if you could have a block of code within the main().
Was This Post Helpful? 0
  • +
  • -

#11 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Is It Possible To Return to a certain line?

Posted 10 December 2009 - 12:50 PM

View PostCodingNewb, on 10 Dec, 2009 - 11:37 AM, said:

Thank you........it basically boiled down to me not knowing if you could have a block of code within the main().



Huh? Not knowing if you could have a block of code with-in main? That's where all the code goes... o.O;;

That or a .h file or some global variables or functions. But everything is processed at some point, to be valid, in main()

This post has been edited by IngeniousHax: 10 December 2009 - 12:50 PM

Was This Post Helpful? 0
  • +
  • -

#12 srlester  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 53
  • Joined: 28-January 09

Re: Is It Possible To Return to a certain line?

Posted 10 December 2009 - 01:49 PM

View PostBench, on 10 Dec, 2009 - 12:57 AM, said:

View Postsrlester, on 10 Dec, 2009 - 03:25 AM, said:

Im pretty sure line 7 is int main() and if it is than this shouldnt be much of a problem. Just call the main function again.
 if(conditons) 
{
main();
}


It would also be illegal; A program which calls main produces this error message on a conforming C++ compiler in strict mode

Quote

error: function "main" may not be called or have its address taken

This error was produced using the comeau compiler http://www.comeaucom...g.com/tryitout/



Im pretty sure you can use main(); Ive used it many times to restart my program and its always worked heres an example from a Percent calculator I made for online shopping for my girlfriend.

 if (ch == 2)
{
	cout << "Whats the percent off you're getting?" <<endl;
	cin >> p;
	cout << "Now enter the Cost of your item" << endl;
	cin >> c;
	d = (p * .01);
	fp = (c * d);
	fc = ( c - fp);
	cout << "Total price is" << endl;
	cout << fc << endl;
	system("PAUSE");	  system("cls");
	main();		  
}



Lol and when you look at my algorithm, this was one of my first programs and I did things probably the hardest way possible.
Was This Post Helpful? 0
  • +
  • -

#13 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Is It Possible To Return to a certain line?

Posted 10 December 2009 - 02:31 PM

Quote

Im pretty sure you can use main();
It would not be a valid C++ program. Calling main is not a loop idiom. It's a recursive call. Recursively calling main is explicitly forbidden by the language.

This post has been edited by Oler1s: 10 December 2009 - 02:31 PM

Was This Post Helpful? 0
  • +
  • -

#14 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

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

Re: Is It Possible To Return to a certain line?

Posted 10 December 2009 - 08:32 PM

Calling main() is BAD and you really should not do it. Some compilers will let you get away with it (throw back to C) but that does not mean that you SHOULD do it. It is a good way to screw things up and make a very unstable program with the potential of cashing a computer.

Each time you call main() you are eating up stack space, and it is a good way to cause memory leaks if you allocate memory. Basically it is just not a good idea and something that should only be done by someone who knew what they were doing, but anyone who knew what they were doing would not do it... so...

The things about C/C++ is that they allow you to do what you want -- but its up to you to do what is smart.
Was This Post Helpful? 0
  • +
  • -

#15 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5306
  • View blog
  • Posts: 27,200
  • Joined: 10-May 07

Re: Is It Possible To Return to a certain line?

Posted 10 December 2009 - 08:34 PM

So the following is bad? ;)
#include <stdio.h>
void main(void) {
  while(1) main();
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2