Why won't this loop end??

This is a discussion on Why won't this loop end?? within the C++ Programming forums, part of the General Programming Boards category; I'm sorry, I'm green as grass to C++. I do have experience with Basic4Gl and some regular and visual Basic. ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Location
    Salisbury N.C.
    Posts
    20

    Post Why won't this loop end??

    I'm sorry, I'm green as grass to C++. I do have experience with Basic4Gl and some regular and visual Basic. I have a small test program set up to test out the things i've learned from the 1st 3 tutorials. These cover topics from variables up until loops. My program has a Do...While loops in it, the loop works, but I cannot get it to stop can somone please point out my problem?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int age, number, yob;
        char retry;
        do {
        cout<<"How old are you?\n";
        cin>>age;
        cin.ignore();
        cout<<"What is your favorite number?\n";
        cin>>number;
        cin.ignore();
        yob = 2008 - age;
        if (age == 15) {
                cout<<"So you're "<< age<<"? Awsome! You're the same age as me!\n";
                }
        else {
             cout<<"So you're "<< age<<", were born in "<< yob<<", and you favorite number is "<< number<<".\n";
        }
        cout<<"Repeat, Y/N \n";
        cin>>retry;
        cin.ignore();
        
        } 
        while ( retry = "Y"||"y" );
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > while ( retry = "Y"||"y" )
    1. = is not the same as ==

    Also, it's not the same as
    while ( retry == "Y" || retry == "y" )
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Location
    Salisbury N.C.
    Posts
    20
    Quote Originally Posted by Salem View Post
    > while ( retry = "Y"||"y" )
    1. = is not the same as ==

    Also, it's not the same as
    while ( retry == "Y" || retry == "y" )
    I'm confused, I know that = and == are not the same, but if I do:

    Code:
    while ( retry == "Y"||"y" );
    Then I get an error:

    ISO C++ forbids comparison between pointer and interger
    Last edited by The7thCrest; 01-01-2009 at 11:57 PM.

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Quote Originally Posted by The7thCrest View Post
    I'm confused, I know that = and == are not the same, but if I do:

    Code:
    while ( retry == "Y"||"y" );
    Then I get an error
    Character literals look like 'y' or 'Y'.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Salisbury N.C.
    Posts
    20
    Quote Originally Posted by NeonBlack View Post
    Character literals look like 'y' or 'Y'.
    Nice ty, It works now, very helpful information. Thanks

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I'm surprised. You should do as Salem says and
    Code:
    while( retry=='y' || retry=='Y' ) ...
    as the other way won't work. You can't nest the || comparison operator like that.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    It is still valid what you do, it just won't do what you think!
    The retry == 'Y' || 'y' meanst retry EQUALS 'Y' OR 'y', which can be (retry == 'Y') || 'y' or it can be retry == ('Y' || 'y'). In any case 'Y' || 'y' should always be true since 'Y' != 0 and 'y' != 0 (0 is false, anything else true)
    So either you get always true or always false or just have undefined behaviour...

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Less usefully,
    Code:
    (A == B||C) == (A == B || A == C)
    is seldom true for general types (which is what has been said).

    Luckily, in most languages (including English), "is" distributes over "or," giving us a meaningful equality. Suppose A, B and C are all of the same type, then A == B || A == C compares objects of the same type. A == B||C compares typeof(A) to a bool. You probably got this long ago, and I'm still talking.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modify to make Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 10
    Last Post: 11-03-2008, 06:25 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM
  4. Reading an array in a while not end of file loop
    By npg316 in forum C Programming
    Replies: 5
    Last Post: 10-18-2004, 12:25 PM
  5. End of Code Loop Question
    By JuanSverige in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 10:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21