problem with Chars and Strings

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

problem with Chars and Strings

 
0
  #1
Nov 13th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,771
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 743
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: problem with Chars and Strings

 
0
  #2
Nov 13th, 2004
>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'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: problem with Chars and Strings

 
0
  #3
Nov 13th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,771
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 743
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: problem with Chars and Strings

 
0
  #4
Nov 13th, 2004
You're thinking too hard:
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5. int main()
  6. {
  7. //declare variables
  8. string name;
  9.  
  10. //input items
  11. while (cout<<"Name? ", cin>>name ) {
  12. if(name[0]=='G'||name[0]=='H'||name[0]=='I'||name[0]=='J'||name[0]=='K'||name[0]=='L')
  13. cout<<name<<" is a good person"<<endl<<endl;
  14. else
  15. cout<<name<<" is a bad person"<<endl<<endl;
  16. //endif
  17. }
  18. //endwhile
  19. return 0;
  20. }
  21. //end of main function
Though that test chain is pretty long. You can tighten things up a bit by using a search string:
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string good ( "GHIJKL" );
  9. string name;
  10.  
  11. while ( cout<<"Name? ", cin>>name ) {
  12. if ( good.find ( name[0] ) != string::npos )
  13. cout<< name <<" is a good person"<<endl<<endl;
  14. else
  15. cout<< name <<" is a bad person"<<endl<<endl;
  16. }
  17. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: problem with Chars and Strings

 
0
  #5
Nov 13th, 2004
Originally Posted by Narue
>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
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: problem with Chars and Strings

 
0
  #6
Nov 13th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,771
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 743
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: problem with Chars and Strings

 
0
  #7
Nov 13th, 2004
>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:
  1. while ( cout<<"Name? ", getline ( cin, name ) ) {
By the way, wasn't Attila the Hun a "bad" person? If so, then the example works properly.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: problem with Chars and Strings

 
0
  #8
Nov 13th, 2004
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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: problem with Chars and Strings

 
0
  #9
Nov 13th, 2004
ps i 'm not sure if i'm comfortable placing the prompts in the actual while loop could they be placed outside aswell?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,771
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 743
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: problem with Chars and Strings

 
0
  #10
Nov 13th, 2004
>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:
  1. cout<<"Name? ";
  2. while ( cin>>name ) {
  3. if ( good.find ( name[0] ) != string::npos )
  4. cout<< name <<" is a good person"<<endl<<endl;
  5. else
  6. cout<< name <<" is a bad person"<<endl<<endl;
  7. cout<<"Name? ";
  8. }
That's not necessarily a bad thing though, and you can get around it with an infinite loop construct:
  1. while ( true ) {
  2. cout<<"Name? ";
  3. if ( !( cin>>name ) )
  4. break;
  5. if ( good.find ( name[0] ) != string::npos )
  6. cout<< name <<" is a good person"<<endl<<endl;
  7. else
  8. cout<< name <<" is a bad person"<<endl<<endl;
  9. }
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC