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)

Post a Reply


- page 1
Oldest  Newest  Rating
+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 Number Double07 · February 26, 2015
Thanks Altaf.  That is very useful information you have provided.  I now understand the goto.  
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:
0 Troy Jones · February 21, 2015
Use a do / while loop.
0 Number Double07 · February 28, 2015
I see how it can be considered bad practice to use the goto.  It works here, but mainly because I am being careful about it's use.  But, I can see how it could lead to problems.  Imagine if I jumped from thebeginning of my code to somewhere in the middle.  I could miss many things in between easily.  Debugging would be messy.  I could miss variable declarations, all sort of nasty things.   It would be even more confusing the more gotos I used.  Lots of jumping, would make debugging a nightmare.
0 Number Double07 · March 10, 2015
Actually, the goto statement worked for me.  I got it to do what I wanted using the goto and it was really very simple; I just had to get around some scope issues.
0 Abdullah Nauman · March 10, 2015
If the code works its fine
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)
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:
  • 1
  • 2



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

Bucky Roberts Administrator