| | |
problem with Chars and Strings
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Solved Threads: 0
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
#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
You're thinking too hard:
Though that test chain is pretty long. You can tighten things up a bit by using a search string:
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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; } }
I'm here to prove you wrong.
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Solved Threads: 0
•
•
•
•
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 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:
By the way, wasn't Attila the Hun a "bad" person? If so, then the example works properly.
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)
while ( cout<<"Name? ", getline ( cin, name ) ) {
I'm here to prove you wrong.
>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:
That's not necessarily a bad thing though, and you can get around it with an infinite loop construct:
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.
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)
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? "; }
C++ Syntax (Toggle Plain Text)
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; }
I'm here to prove you wrong.
![]() |
Similar Threads
- chars and strings (Assembly)
- problem in concatenation of 2 strings (C)
Other Threads in the C++ Forum
- Previous Thread: program w/ switch AND nested if. six error messages
- Next Thread: Tutorials on Debugging
| Thread Tools | Search this Thread |
api application array arrays beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets







tutors don't know squat now isn't that ironic!