?
Solved

true/false while loop

Posted on 2004-10-30
9
Medium Priority
?
232 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
Comment
Question by:kgpretty
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 15

Expert Comment

by:efn
ID: 12453908
> 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:
itsmeandnobodyelse earned 160 total points
ID: 12454375
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
ID: 12455871
guys you are using too much computing power.

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

jaydutt
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 2

Expert Comment

by:kmcorbett2
ID: 12456016
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
ID: 12458107
I don't see what you are trying to say. could u provide an example?
0
 

Author Comment

by:kgpretty
ID: 12466490
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
ID: 12468109
>> 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
ID: 12471832
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
ID: 12474578
I took out the ";'.. my silly mistake. It works fine now.Thanks
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

601 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