944,183 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3538
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 13th, 2004
0

problem with Chars and Strings

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Nov 13th, 2004
0

Re: problem with Chars and Strings

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 13th, 2004
0

Re: problem with Chars and Strings

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
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Nov 13th, 2004
0

Re: problem with Chars and Strings

You're thinking too hard:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 13th, 2004
0

Re: problem with Chars and Strings

Quote 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
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Nov 13th, 2004
0

Re: problem with Chars and Strings

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
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Nov 13th, 2004
0

Re: problem with Chars and Strings

>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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 13th, 2004
0

Re: problem with Chars and Strings

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!
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Nov 13th, 2004
0

Re: problem with Chars and Strings

ps i 'm not sure if i'm comfortable placing the prompts in the actual while loop could they be placed outside aswell?
Reputation Points: 10
Solved Threads: 0
Junior Poster
boujibabe is offline Offline
123 posts
since Nov 2004
Nov 13th, 2004
0

Re: problem with Chars and Strings

>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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: program w/ switch AND nested if. six error messages
Next Thread in C++ Forum Timeline: Tutorials on Debugging





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC