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 Abdullah Nauman · March 10, 2015
If the code works its fine
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 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.
+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.
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
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 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 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
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.
+1 Number Double07 · February 26, 2015
Thanks Altaf.  That is very useful information you have provided.  I now understand the goto.  
  • 1
  • 2

C++

107,107 followers
About

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

Links
Moderators
Bucky Roberts Administrator