C++ if loop question

Discussion in 'C++' started by gabego, Feb 11, 2008.

  1. gabego

    gabego New Member

    Joined:
    Feb 11, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    This program will terminate for an input < 0 using this particular exit() function. Is there a function or a different statement to use that will loop back to the first cout statement so the user could input a new number? In this current form, the user has to re-execute the program after making a mistake.

    int p, k = 5;

    cout << "enter a number greater than or equal to 0" <<;
    cin >> p;
    cout << p + k;
    if (p < 0)
    cout << "error try again"
    exit(EXIT_FAILURE) ;

    Thanks for any comments!!
    -Gabe
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,346
    Likes Received:
    382
    Trophy Points:
    83
    You need to use loops/.
     
  3. gabego

    gabego New Member

    Joined:
    Feb 11, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    yea, directly after the cout , do cin again instead of exiting. That would be a loop.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice