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 2
Oldest  Newest  Rating
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
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 Troy Jones · February 21, 2015
Use a do / while loop.
  • 1
  • 2

C++

107,260 followers
About

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

Links
Moderators
Bucky Roberts Administrator