cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
convert string to char
convert string to char
Jan 4, 2011 at 3:03am UTC
sloper
(5)
How do I covert string abc to char?
#include <iostream>
#include <string>
using namespace std;
char letter;
int main ()
{
string abc ("This is an example phrase.");
cout << abc << endl;
// output This is an example phrase.
cin>>abc; //input qwerty
cout<<abc << endl;
//output qwerty
abc.erase (1);
cout << abc << endl;
//Output q
/*what code do I put here to convert abc string holding value of q
to char letter? Do I need additional header files to support this?*/
cout<< letter; //want output to be q from abc string
cin.ignore(256,'\n');//Code so program output does not flash off screen on Bloodshed Dev C++ compiler.
cin.get();//Stops program until enter key is pressed.
return 0;
}
Jan 4, 2011 at 3:18am UTC
firedraco
(6243)
Use string's operator[] like so:
some_string[0];
//this is the first character in the string
Jan 4, 2011 at 3:19am UTC
Computergeek01
(5613)
Nah, you don't need any more header files libs or any of that mumbo jumbo. Check this out:
http://www.cplusplus.com/reference/string/char_traits/
Basically a string is a dynamic array of chars so I guess the answer to your question is nothing? If you need to pass the string to a function that doesn't have a definition to handle strings then that's where we use these babies:
http://www.cplusplus.com/doc/tutorial/pointers/
Jan 4, 2011 at 3:27am UTC
sloper
(5)
Thank you!
Jan 4, 2011 at 3:33am UTC
closed account (
4Gb4jE8b
)
well answered computer geek!
Topic archived. No new replies allowed.