How do you convert a string into a char array?

Hello,

I know there has got to be a simple way to do this, but I am trying to convert a string that was recieved using
getline (cin, string)

to a char array[].

the reason is because I have a function that acts as a type writer on the screen.
it is defined as

void typeout(char type[])
{
int limit = strlen (type)-1;
for (int index = 0; index <= limit; index++)
{
cout << type[index];
wait (250);
}
}

I hope someone can help me with this.

Thanks
Zack
Do you mean you want to get a c-like string from a string?
I think you can use function c_str() to do it.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

int main () {
    std::string str;
    char *arr;

    std::cin >> str;
    strcpy(arr,str.c_str());

    return 0;
}
Last edited on
closed account (4Gb4jE8b)
1
2
3
4
5
6
7
8
9
void typeout(string type)
{
   int limit = type.length() -1;
   for (int index = 0; index <= limit; index++)
   {
      cout << type[index];
      wait (250);
   }
}


should do it.

NOTE
int limit = type.length() -1;
may not need -1, you could have errors with it. You might not, ie code is untested.
Thanks, those are both helpful Ideas! I figured out a way arround it though. I used
1
2
char your_name[100];
cin.getline(your_name,100);


I found the .getline to be perfect and I didn't have to change my typeout() function.

Thanks for your help though.
Zack
Topic archived. No new replies allowed.