Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

true/false while loop

Posted on 2004-10-30
9
Medium Priority
?
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

618 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