>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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;
}
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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?
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>can i still do that?
Sure. You want to place
name[0] = toupper ( name[0] );
Just before the if statement.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401