true/false while loop

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?
kgprettyAsked:
Who is Participating?
 
itsmeandnobodyelseCommented:
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
 
efnCommented:
> 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
 
jhshuklaCommented:
guys you are using too much computing power.

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

jaydutt
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
kmcorbett2Commented:
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
 
jhshuklaCommented:
I don't see what you are trying to say. could u provide an example?
0
 
kgprettyAuthor Commented:
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
 
itsmeandnobodyelseCommented:
>> 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
 
arun80_ininCommented:
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
 
kgprettyAuthor Commented:
I took out the ";'.. my silly mistake. It works fine now.Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.