Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help

Status
Not open for further replies.

xeocube

Programmer
Apr 2, 2003
2
US
hey can anyone help me convert an array type to a string?

..like array a into string hello

:/

thanks
 
I assume you mean an array of characters and an STL string. The string class takes a char* in one of its constructors. You can also assign a char* to a string object.
 
Heres what i have for the problem, sorry but im quite a nub at this stuff :/ its killing me though because i had to write a project that was quite long. I was lazy and just put a sentence into a string not thinking anything, but the spaces messed it up somehow.. so.. heh if i could get the input sentence in the program below to be converted into a string i would be home free.

#include <iostream>

using namespace std;

int main()
{

int i = 0, sentencelength = 0;
char letter = 0;

char a[80];
int y = 0;

cout << &quot;Enter a sentance&quot; << endl;

do {
cin.get(letter);

if (letter != '\n')

a = letter;
i++;
sentencelength=i-1;
} while (letter != '\n');


return 0;

}
 
>> a string

As chipper pointed out that is not totally clear so we are guessing you mean the STL string class.

So assuming you don't forget to null terminate your array char a[80], you can do this:

Code:
string mystr( a);

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top