RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 1358 | Replies: 13 | Thread Tools  Display Modes
Reply
Join Date: Jul 2008
Posts: 31
Reputation: coveredinflies is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
coveredinflies coveredinflies is offline Offline
Light Poster

Converting from a string to a character array.

  #1  
Jul 23rd, 2008
Hi

I am fairly new to programming and am trying to error check the user input. I stumbled upon the 'isalpha' etc functions and so my plan is to loop through the array checking everything is a number or (the one) decimal point. However I am using getline for my input which takes in a string and I can't figure out how to convert.

I searched the internet and someone had done a strcpy using pointers to characters but I a) can't get this to work and b) don't understand why pointers are used with the strcpy function I thought it had to be an character array. (On a related point how do you get strcpy, stcmp etc to work with strings?)

Thanks in advance for any help.

P.S Any better ways to error check would be appreciated although I would still be curious to know how to convert strings to an array.
Last edited by coveredinflies : Jul 23rd, 2008 at 3:57 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2008
Location: USA East Cost
Posts: 397
Reputation: CoolGamer48 is on a distinguished road 
Rep Power: 1
Solved Threads: 37
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Whiz

Re: Converting from a string to a character array.

  #2  
Jul 23rd, 2008
don't use char arrays or char*. use std::strings. if your input is a string, you can just use the [] operator to access elements in it (it's been overloaded)

string str = "Hello";
if(str[1] == 'e')
//this is true

If you really want to know how to convert a string to an array:
char strarray[100];
string str = "Hello";
for(int i = 0;i < str.length();i++)
    strarray[i] = str[i];

but this is useless given that the [] operator can be used with strings.
Last edited by CoolGamer48 : Jul 23rd, 2008 at 4:09 pm.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote  
Join Date: Jul 2008
Posts: 1,196
Reputation: ArkM is a glorious beacon of light ArkM is a glorious beacon of light ArkM is a glorious beacon of light ArkM is a glorious beacon of light ArkM is a glorious beacon of light 
Rep Power: 7
Solved Threads: 185
ArkM's Avatar
ArkM ArkM is online now Online
Veteran Poster

Re: Converting from a string to a character array.

  #3  
Jul 23rd, 2008
Good news: std::string has a wonderful member function c_str(). It returns a const pointer to the C-style version of the invoking string.
Now you may use all power of old good str family. Don't modify the string contents via forcibly casted as non-const pointer obtained from c_str()!
Reply With Quote  
Join Date: Jul 2008
Posts: 1,196
Reputation: ArkM is a glorious beacon of light ArkM is a glorious beacon of light ArkM is a glorious beacon of light ArkM is a glorious beacon of light ArkM is a glorious beacon of light 
Rep Power: 7
Solved Threads: 185
ArkM's Avatar
ArkM ArkM is online now Online
Veteran Poster

Re: Converting from a string to a character array.

  #4  
Jul 23rd, 2008
Rush after prev post (an example and the answer to the question):
  1. std::string stlString("Don\t tread on me!");
  2. char IloveC[2008];
  3. strcpy(IloveC,stlString.c_str());
Reply With Quote  
Join Date: Mar 2008
Location: UK - Lymm
Posts: 599
Reputation: williamhemsworth is just really nice williamhemsworth is just really nice williamhemsworth is just really nice williamhemsworth is just really nice williamhemsworth is just really nice 
Rep Power: 6
Solved Threads: 56
williamhemsworth's Avatar
williamhemsworth williamhemsworth is offline Offline
Posting Pro

Re: Converting from a string to a character array.

  #5  
Jul 23rd, 2008
Originally Posted by CoolGamer48 View Post
If you really want to know how to convert a string to an array:
char strarray[100];
string str = "Hello";
for(int i = 0;i < str.length();i++)
    strarray[i] = str[i];

This will work, but in future when copying variables like this from a vector, try to do it like this:
  1. char strarray[100];
  2. string str = "Hello";
  3. int arrayLength = str.length();
  4. for (int i = 0; i < arrayLength; i++)
  5. strarray[i] = str[i];
By using a variable to store the array length you do not need to call the .length() function for every cycle of the loop and will speed things up in more extreme cases.

Another way to access the char array is to simply get the pointer at the start of the string like this.
  1. string str = "Hello";
  2. char *cstr = &str[0];
Also this way the pointer isn“t const unlike the return value from the string::c_str() function.
(The only problem with this is that it is not a seperate char array, anything you change in cstr will also change in str).

Hope this helps.
Last edited by williamhemsworth : Jul 23rd, 2008 at 7:47 pm.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,861
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 1012
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Converting from a string to a character array.

  #6  
Jul 23rd, 2008
Originally Posted by williamhemswort View Post
  1. char strarray[100];
  2. string str = "Hello";
  3. int arrayLength = str.length();
  4. for (int i = 0; i < arrayLength; i++)
  5. strarray[i] = str[i];


1) Don't use [code=CPP] use instead [code=CPLUSPLUS]

2) The code above contains the same bug as the version previously posted. Q: What is the bug? A: The resulting string is not null terminated.
<<Hire Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Jun 2008
Location: WA, USA
Posts: 940
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Rep Power: 5
Solved Threads: 100
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Converting from a string to a character array.

  #7  
Jul 23rd, 2008
Originally Posted by Ancient Dragon View Post
1) Don't use [code=CPP] use instead [code=CPLUSPLUS]

2) The code above contains the same bug as the version previously posted. Q: What is the bug? A: The resulting string is not null terminated.


Well it's an easy fix isn't it? >_>

  1.  
  2. char strarray[100] = {0};
  3. string str = "Hello";
  4. int arrayLength = str.length();
  5. for (int i = 0; i < arrayLength; i++)
  6. strarray[i] = str[i];
  7.  
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,861
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 1012
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Converting from a string to a character array.

  #8  
Jul 23rd, 2008
Originally Posted by Alex Edwards View Post
Well it's an easy fix isn't it? >_>

  1.  
  2. char strarray[100] = {0};
  3. string str = "Hello";
  4. int arrayLength = str.length();
  5. for (int i = 0; i < arrayLength; i++)
  6. strarray[i] = str[i];
  7.  

Yes, it is an easy fix. What you did is one way to do it. The other way is to simply add another line starray[i] = 0; after the end of the code snippet (line 7). This is probably preferred because it insures the string is correctly null-terminated no matter how many different strings that are copied into the character array.
Last edited by Ancient Dragon : Jul 23rd, 2008 at 9:06 pm.
<<Hire Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Jul 2008
Posts: 31
Reputation: coveredinflies is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
coveredinflies coveredinflies is offline Offline
Light Poster

Re: Converting from a string to a character array.

  #9  
Jul 24th, 2008
Thanks guys (or girls). Didn't understand everything that was said but very helpful
Reply With Quote  
Join Date: Oct 2007
Posts: 260
Reputation: joshmo is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

Re: Converting from a string to a character array.

  #10  
Jul 24th, 2008
Originally Posted by coveredinflies View Post
Thanks guys (or girls). Didn't understand everything that was said but very helpful

What didnt you understand?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 8:37 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC