Solved

true/false while loop

Posted on 2004-10-30
227 Views
Last Modified: 2010-04-01
I believe the loop is the problem in my C++ code.
Here it is;
http://www.freewebs.com/kgpretty/lll.cpp

I'm supposed to display "Good person" if the first letter of the name begins with G - L and "Bad Person" for everything else.

It works for the Good Person path but not when u enter anything not starting with G - L.

What's wrong and how do I fix it?
0
Question by:kgpretty
    9 Comments
     
    LVL 15

    Expert Comment

    by:efn
    > if (fletter == G || H || I || J || K || L)

    When you have something like

    if (fletter == G || H)

    there are two ways the compiler might interpret it, neither of which is what you want.  As it happens, the == operator has precedence over the || operator, so the compiler interprets it as

    if ((fletter == G) || H)

    Characters can be treated as integers and true/false is equivalent to nonzero/zero, so to execute this test, the compiler generates code that first compares fletter to G.  If they are the same, the whole condition is true; otherwise, it tests H for a truth value.  H is not zero because the ASCII code for the letter 'H' is not zero, so the test is true.  So this test will always be true.

    What you want to do is write a bunch of separate tests for each letter and logically "or" the results.

    if (fletter == G || fletter == H || ...

    Also, you might want to assign a value other than 'H' to the variable G, since the variable H also has this value.

    --efn
    0
     
    LVL 39

    Accepted Solution

    by:
    Though i actually don't know wat you are intending with that program, it easily could be improved  ;-)

    string G_L = "GHIJKL";
    while (name == "0");
        {  
             if (G_L.find(fletter) != string::npos)
          cout << fletter << name << " is a good person!" << endl << endl;
             else
                    ...

    Regards, Alex



                 
    0
     
    LVL 9

    Expert Comment

    by:jhshukla
    guys you are using too much computing power.

    if('G' <= fletter && fletter <= 'L')
       the person is good;
    else
       the person is bad;

    jaydutt
    0
     
    LVL 2

    Expert Comment

    by:kmcorbett2
    The code from itsmeandnobodyelse has one advantage over jhshukla's approach. Using a "lookup table" makes it easier to adapt to changing requirements, for example for I18N, Unicode character encoding etc.

    :)
    0
     
    LVL 9

    Expert Comment

    by:jhshukla
    I don't see what you are trying to say. could u provide an example?
    0
     

    Author Comment

    by:kgpretty
    Here's the assignment question;
    Good people all have last names that begin with the letters G through L; all others are bad. Write a program that differentiates the good people from the bad.

    The updated file;
    http://www.freewebs.com/kgpretty/assignmentb.cpp

    I've gotten it to work fine so far except the loop doesn't work. What can I put for name so that it repeats?
          while (name == " ");
    0
     
    LVL 39

    Expert Comment

    by:itsmeandnobodyelse
    >> while (name == " ");  

    That statement is wrong as it ends with an semicolon. It doesn't work on the next block of statements because of that. If the condition was true it would be an endless loop. However, the condition was wrong as name was empty and was not equal to a string containing one space character " ".

    >>       cout << "Enter a last name : ";
    >>       cin >> fletter;
    >>       getline (cin, name);

    That sequence also looks wrongly. You are asking for a name. That's ok. But then, you expect a single letter as input - followed by a name. That is very strange. Maybe, you wanted to do that:

        // start an endless loop
        while (true)
        {
            cout << "Enter a last name : ";
            getline (cin, name);
            if (name.empty() || name == "quit")
                 break;      // break the loop on empty name or if "quit" was entered

            fletter = name[0];  // get the first letter of name
            ...
       }

    Regards, Alex
          
    0
     
    LVL 2

    Expert Comment

    by:arun80_inin
    try the if condition like  this

    f('G' <= toupper(fletter) &&toupper( fletter )<= 'L')
       the person is good;
    else
       the person is bad;


    and remove the ; in while statement.
    0
     

    Author Comment

    by:kgpretty
    I took out the ";'.. my silly mistake. It works fine now.Thanks
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: From Zero to Hero with Nodejs & MongoDB

    Interested in Node.js, but don't know where to start or how to learn it properly? Confused about how the MEAN stack pieces of MongoDB, Expressjs, Angularjs, and Nodejs fit together? Or how it's even possible to run JavaScript outside of the browser?

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now