How do I return to a specific point in main if user input wrong data.

+2 Number Double07 · February 21, 2015
Okay, so I know that if I put return main ( ) in my main function, it will return to the start of the main.  How do I return to a specific line of code and retain the information previously entered?  In my code, I ask the user three things, but in the second question, they must enter a value between 0 or 1 (float) or they need to enter the value again.

int main()
{
    Newspaper tNewspaper;

    cout tNewspaper.Ads_Shown;

    cout tNewspaper.Percentage_User_Ads;

    if (tNewspaper.Percentage_User_Ads > 1.0000000)
    {
        if (tNewspaper.Percentage_User_Ads < 0.0000000)
        {
                cout

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Troy Jones · February 21, 2015
Use a do / while loop.
0 Number Double07 · February 26, 2015
That certainly would work Troy.  Thx bro.  There is also another method I'm considering.  It's called the goto.  It looks simpler than doing a do while loop, but I'm not familiar with the goto yet.
0 Altaf Husain Neva · February 26, 2015
goto statement is the best way to perform the operation you want to do. you have to just create a label to show the goto keyword where to go.
In your case:


int main()
{
Newspaper tNewspaper;

cout tNewspaper.Ads_Shown;

redirect_here: cout tNewspaper.Percentage_User_Ads;

if (tNewspaper.Percentage_User_Ads > 1.0000000 && tNewspaper.Percentage_User_Ads < 0.0000000)
{
cout
}
else
{
goto redirect_here;
}
here in the above code u can use the logical operator && to check multiple condition. and to read in detail whats goto have a look here: http://www.tutorialspoint.com/cprogramming/c_goto_statement.htm
+1 Number Double07 · February 26, 2015
Thanks Altaf.  That is very useful information you have provided.  I now understand the goto.  
0 Altaf Husain Neva · February 26, 2015
No Problem Dear, Everyone here is either to learn or to teach. Learn whatever you don't know and teach whatever you have got until now.
0 Troy Jones · February 26, 2015
I've heard using goto is bad programming practice, but that doesn't mean it won't work....
0 Altaf Husain Neva · February 26, 2015
Yuppp it is bad  when used with iteration that is loop or some recursive blocks where the chances of deadlocks are there. In this there is no issue.
0 Donald Genes · February 26, 2015
Alttaf Hussain, please, can you help me ..I am unable to install Intel HAXM on my Windows 8 HP AMD 1.40HZ Processor since morning..Check my post please
0 Altaf Husain Neva · February 27, 2015
@Donald I haven't yet started practicing it. Post your Question in the related forums and many guys will help you with that.

Post your question here: https://www.thenewboston.com/forum/category.php?id=10
+3 c student · February 27, 2015
goto is bad programming practice because it disturbs the systematic flow of a program meaning your code will jump around instead of going straight down or looping.  this can make it difficult for tasks such as tracking and debugging errors.  it is better to entirely forget it unless you are going to write some crazy nested conditionals since you won't need it for small-time programs like this.  if you need to use goto in such a simple situation, you should probably take a step back and rethink something.
  • 1
  • 2

C++

107,321 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator