Jump to content

Go back to part of a program?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
shibbythestoner

shibbythestoner

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
Hi
Started learning C++ a couple days ago. I was wondering if there's a way to go back to part of a program, for example:


#include <iostream>
#include <string>
using namespace std;
int main()
{
string age;
cout<<"Hello!"<<endl;
//go back to here?
cout<<"How old are you?: ";
getline(cin, age);
cout<<""<< age <<"?! I doubt that!"<<endl;
cin.get();
return 0;
}



#2
twitch

twitch

    Newbie

  • Members
  • PipPip
  • 10 posts
I use C but this works I tested it on g++.

#include<iostream>
#include <string>
using namespace std;
int main()
{
    string age;
    bool exitchoice;
    
    cout<<"Hello!"<<endl;
    while(exitchoice)
    {
        cout<<"How old are you?: ";
        cin>> age;
        cout<<""<< age <<"?! I doubt that!"<<endl;
        cout<< "Do you want to restart? 1 for yes or  0 for no:";
        cin>> exitchoice;
        if(exitchoice == 1)
        {
            continue;
        }
        else if(exitchoice == 0)
        {
            break;
        }
    }

    return 0;
}




#3
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
A loop will be the only way you will be able to go back. There use to be a command "goto" which would take your program back to a line number but I do not think it is used any longer.

#4
Dave

Dave

    Newbie

  • Members
  • Pip
  • 4 posts
Here's another way to do it:

#include <iostream>

#include <string>

using namespace std;

int main()

{

string age;


cout<<"Hello!"<<endl;

do

{

	cout<<"How old are you? (Type 0 to quit): ";

	getline(cin, age);

	cout<<""<< age <<"?! I doubt that!"<<endl;

} while( age[0] != '0' );


return 0;

}


'do...while' will always display your prompt at least once, and is more natural in this case. The condition is tested at the end of the loop, instead of the start.

Also note that the test condition was based on the value of the first character of the string, 'age[0]'. The escape character in this case is '0', as indicated in the prompt. If you do it this way, you don't need to create a new variable like 'exitchoice'. Personally, I like prompting the user if they want to continue, like twitch did, but I thought I'd show you an alternative. Either one may be more natural based on your programming needs. Here's the code for the alternative construction:


#include <iostream>

#include <string>

using namespace std;

int main()

{

string age;

string exitchoice;


cout<<"Hello!"<<endl;

do

{

	cout<<"How old are you?: ";

	getline(cin, age);

	cout<<""<< age <<"?! I doubt that!"<<endl;

	cout<<"Would you like to try again? [y/n]: ";

	getline(cin, exitchoice);

} while( exitchoice[0] != 'n' );


return 0;

}


In this case, if the user types anything but 'n', the loop will continue. Of course, there are lots of other ways of doing this, but hopefully this should give you some ideas.

As far as 'goto' goes, it is still a valid keyword, but it should be avoided in most cases. Code clarity should always be your top priority. Most of the time, goto makes things messy, so it's counterproductive to use it. If you happen to be deeply nested in several loops and conditional statements and you encounter an error and need to exit quickly and cleanly, it would probably be more clear to use goto than a series of 'break' statements. When writing code, always keep this mantra in mind: "What is the clearest way I can express this instruction?"

#5
0eraser

0eraser

    Newbie

  • Members
  • Pip
  • 3 posts
You can also use recursion
 void getage() {string age; cout << "How old are you(Type 0 to quit)"; getline(cin,age); cout << "" << age << "?! I doubt that"; if(age[0]!='0') gertage(); else return; } 


#6
shibbythestoner

shibbythestoner

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
Thanks very much you three, and thanks Dave for the explanations! I've had a hard time learning about ifs, elses and whiles from the web, so you have all helped me a great deal.

#7
shibbythestoner

shibbythestoner

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
thanks, 0eraser
I'm saving these source codes as I have learned best from examples so far.

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Just one additional note: some languages do support GOTO statements. Do not use them! One: they are not necessary if there is a while loop structure. Two: they can create really strange bugs that are almost impossible to detect.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
shibbythestoner

shibbythestoner

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts

WingedPanther said:

Just one additional note: some languages do support GOTO statements. Do not use them! One: they are not necessary if there is a while loop structure. Two: they can create really strange bugs that are almost impossible to detect.

Ok thanks. I had been considering looking for that earlier; so thanks for the advice.
I have plenty alternatives now.