I have a problem which states that good people all have last names that be begin with the letters G through L; all the others are bad. The program must differentiate the good people from the bad. Does neone have any design ideas?

output:Name? Farley
Farley is a bad person

>Does neone have any design ideas?
What is there to design? Read a string, check the first character, and print the appropriate message using an if statement and logical AND.

sorry i was just wondering if it could be done differently because mine isnt working

#include <iostream>
#include <string>

using namespace std;
int main()
{

//declare variables 
string first="";
string name="";




//input items
cout<<"Name?";
cin>>name;
   while (name!=0)
   {
     first.assign(name,0,1);
     first=toupper(first);

     if(first=='G'||first=='H'||first=='I'||first=='J'||first=='K'||first=='L')
      cout<<first<<"is a good person"<<endl<<endl;
     else
      cout<<first<<"is a bad person"<<endl<<endl;
      //endif 

      //input items
      cout<<"Name?"
      cin>>name;
   }
   //endwhile
   return 0;
}
//end of main function

Edited 3 Years Ago by happygeek: fixed formatting

You're thinking too hard:

#include <iostream>
#include <string>

using namespace std;
int main()
{
  //declare variables 
  string name;

  //input items
  while (cout<<"Name? ", cin>>name ) {
    if(name[0]=='G'||name[0]=='H'||name[0]=='I'||name[0]=='J'||name[0]=='K'||name[0]=='L')
      cout<<name<<" is a good person"<<endl<<endl;
    else
      cout<<name<<" is a bad person"<<endl<<endl;
    //endif 
  }
  //endwhile
  return 0;
}
//end of main function

Though that test chain is pretty long. You can tighten things up a bit by using a search string:

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string good ( "GHIJKL" );
  string name;

  while ( cout<<"Name? ", cin>>name ) {
    if ( good.find ( name[0] ) != string::npos )
      cout<< name <<" is a good person"<<endl<<endl;
    else
      cout<< name <<" is a bad person"<<endl<<endl;
  }
}

>Does neone have any design ideas?
What is there to design? Read a string, check the first character, and print the appropriate message using an if statement and logical AND.

I know its a simple program but I am a beginner i just would like to know if there is nething I am doing wrong

I do agree that the test is pretty long but that method was shown to me by a tutor so i figured he would know best. Don't I have to find the first letter? one of the output samples lists the name Attila the Hun

>i just would like to know if there is nething I am doing wrong
If it doesn't work then it's a safe bet you're doing something wrong. :rolleyes:

>shown to me by a tutor so i figured he would know best
Most of the tutors that I've seen don't know squat. But the test chain works; I was just showing you an alternative method.

>Don't I have to find the first letter?
name[0] is the first letter.

>one of the output samples lists the name Attila the Hun
Attila is the name of the person, the Hun is just a qualifier used to specify which Attila. However, you do have a distinct problem if this is one of the input strings. Using cin>>string causes the read to stop at whitespace, so you would only have "Attila" in name rather than "Attila the Hun". Further iterations of the loop would read "the" and "Hun", in that order. If you need to read lines instead of words, you want to use getline:

while ( cout<<"Name? ", getline ( cin, name ) ) {

By the way, wasn't Attila the Hun a "bad" person? If so, then the example works properly. ;)

the output does list Attila as a bad person when i tried th first one but i got sumthing like this
Name? Attila the hun
Attila the hun is a bad person

Name? the is a bad person
Name? hun is a bad person
Name?

lol :)tutors don't know squat now isn't that ironic!

ps i 'm not sure if i'm comfortable placing the prompts in the actual while loop could they be placed outside aswell?

>but i got sumthing like this
That's the problem I was talking about. Use getline to fix it.

>ps i 'm not sure if i'm comfortable placing the prompts in the actual while loop
To be perfectly honest, I haven't decided if it's good or bad style myself. ;) Convenient if the prompt is short, but I use it sparingly.

>could they be placed outside aswell?
Yes, but then you would have redundant prompts:

cout<<"Name? ";
while ( cin>>name ) {
  if ( good.find ( name[0] ) != string::npos )
    cout<< name <<" is a good person"<<endl<<endl;
  else
    cout<< name <<" is a bad person"<<endl<<endl;
  cout<<"Name? ";
}

That's not necessarily a bad thing though, and you can get around it with an infinite loop construct:

while ( true ) {
  cout<<"Name? ";
  if ( !( cin>>name ) )
    break;
  if ( good.find ( name[0] ) != string::npos )
    cout<< name <<" is a good person"<<endl<<endl;
  else
    cout<< name <<" is a bad person"<<endl<<endl;
}

Whether that's better or not is up to you. Loop structuring is a tricky business with all of the structured programming freaks running around out there.

>but i got sumthing like this
That's the problem I was talking about. Use getline to fix it.

Ok I replaced
while (cout<<"Name? ", cin>>name ) with
while ( cout<<"Name? ", getline ( cin, name ) ) and now i not getting any output at all. Should I try sumthing else or should i quit computer studies altogether? :confused:

Oh wait it is working, but the first line skips twice. Thanks a bundle you were a load of help. Everyone I asked at school acted like if i was asking them to write the program for me
Why are programmers so mean?
I look forward to bugging you on my next assignment.

>now i not getting any output at all
It works fine for me. Post the code that you're using so that I don't have to guess with my own test code.

>but the first line skips twice
That sounds like the Visual C++ 6 getline bug. What compiler do you use?

#include <iostream>
#include <string>

using namespace std;
int main()
{
//declare variables
string name;

//input items
while ( cout<<"Name? ", getline ( cin, name ) )
{
if(name[0]=='G'||name[0]=='H'||name[0]=='I'||name[0]=='J'||name[0]=='K'||name[0]=='L')
cout<<name<<" is a good person"<<endl<<endl;
else
cout<<name<<" is a bad person"<<endl<<endl;
//endif
}
//endwhile
return 0;
}
//end of main function


In my original program I converted the first letter to Capitals can i still do that?if so should I place it within the loop?

>can i still do that?
Sure. You want to place

name[0] = toupper ( name[0] );

Just before the if statement.

I'm using visual c++ 6.0 is there any way to fix the skipping problem?

>is there any way to fix the skipping problem?
If you're comfortable with the idea of hacking your compiler's code, go here. Otherwise, either live with it, or upgrade to Visual C++ .NET.

Again a million thanks the only problems i'm having are the line skipping and one of my outputs samples for G .I even learned some stuff

This article has been dead for over six months. Start a new discussion instead.