string into char

is it possible to get string or string stream to go into char, like x[0] = string(stream) s and so on.
yes

http://www.cplusplus.com/reference/string/string/data/

you can simply acces every char in a string by saying mystring[2] returns the third character in your string, so it's quite easy to build your own array.

You can use mystring.c_str() to create an array of chars, but it will have added a null character to the end.
Last edited on
closed account (3hM2Nwbp)
* Ah...too slow.
I'm not entirely sure what you want here, but std::string has a member method that retrieves the internal C-String representation of itself.

1
2
3
4
5
6
7
8
9
10
11
12
13

#include <iostream>
#include <string>

int main(void)
{
  std::string str = "What are you doing with my CString???";
  const char* pointer = str.c_str();
  std::cout << "std::string: " << str << std::endl;
  std::cout << "const char*: " << pointer << std::endl;
  return 0;
}
Last edited on
no no no no, i have an array of 70 members, but iwant to but an entire string int one array member. lets say strign si 42,657,435,754,456 and i want ot put it in x[0] then x[1] will be new strign and so on and so on.
An array of strings.

std:string x[70];
Or a vector of strings::
1
2
3
4
std::vector<std::string> x;
x.push_back(str1);
x.push_back(str2);
x.push_back(str3);
im only gettign conversion errors here. x[i] = total - cant conver int_64 to char possible loss of data and in the end x[d] << outdoes not allow. so what ot do to fix these problems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int long long fibo(int n);
string output_formatted_string(string u);
string x[70];
int main (void) {
int n= 0;
cout << "Enter a number: ";
cin >> n;
fibo(n);
string c=2;
output_formatted_string(c);
for(int f = 0; f<= 70; f++){
cout << x[f] << endl;
}
system("PAUSE");
return 0;
}

long long fibo(int n)
{
int i = 0;
if (n <2)
return 1;
long long temp1 = 1;
long long temp2= 2;
long long total = 0;
while (n-- > 1)
{
total = temp1 + temp2;
temp2 = temp1;
temp1 = total;
x[i] = total;
i++;
}
return 1;
}

#define GROUP_SEP ','
#define GROUP_SIZE 3

string output_formatted_string(string u){
stringstream temp, out;
string num;
for(int d = 0; d <= 70; d++){
	num = x[d];
temp << num;
string s = temp.str();
int n = s.size() % GROUP_SIZE;
int i =0;
if(n > 0 && s.size() > GROUP_SIZE){
out << s.substr(i, n) << GROUP_SEP;
i += n;
}

n = s.size() / GROUP_SIZE -1;
while (n-- > 0){
out << s.substr(i, GROUP_SIZE) << GROUP_SEP;
i += GROUP_SIZE;
}
out << s.substr(i);
x[d] << out;
}
return 1;
}
Well, that's because x is a string array and total is an integer...you have to convert the number to a string, and refer to a specific element of the array.
howto vocnvert long long itn into string
Topic archived. No new replies allowed.