Thread: Two cins in a loop

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    62

    Two cins in a loop

    Hi say i want a while loop, ok and were adding two numbers useing 'cin' statements. I want to add two numbers by the user in the while loop but they have the option to exit by typing '-1'
    my while loop would look like this:

    while ( num1 != -1)

    Now if i put -1 in the first cin statement to end the loop its still asking me for the second number. Would a 'break' statement be what i need. Or do i need to rearange where i put my cin statements.

    Thanks for your help.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I assume a break statement would work fine - if you checked the value after the first input. There are probably other ways to do it that aren't necessarily better. If you have trouble with the break statement just show your code.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Posting the code concerning the statements in question would be better
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Thumbs up break;

    It sounds like break is exactly what you need! My only other suggestion is: Be consistant. Use the exact same code for both input-variable tests. This means that you will have to change to an infinite loop i.e. while(1).

    Although while(1) loops seem a little strange. It is extremely common to use while(1) loops with multiple break statements. (A comment at the start of the loop, telling the "reader" how you get out of the infinite loop is usually helpful.)

    [EDIT] -
    Sometimes you'll see multiple return statements if you want to exit the loop and the function at the same time.
    Last edited by DougDbug; 03-11-2004 at 05:03 PM.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Or an if statement that bypasses the second cin if the first was -1
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM