Please support our C++ advertiser: Programming Forums
Views: 1068 | Replies: 13
![]() |
•
•
•
•
| |
•
•
Join Date: Jul 2008
Posts: 31
Reputation:
Rep Power: 1
Solved Threads: 0
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.
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.
•
•
Join Date: Jan 2008
Location: USA East Cost
Posts: 393
Reputation:
Rep Power: 1
Solved Threads: 37
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)
If you really want to know how to convert a string to an array:
but this is useless given that the [] operator can be used with strings.
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...".
Rush after prev post (an example and the answer to the question):
cpp Syntax (Toggle Plain Text)
std::string stlString("Don\t tread on me!"); char IloveC[2008]; strcpy(IloveC,stlString.c_str());
•
•
•
•
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:
CPP Syntax (Toggle Plain Text)
char strarray[100]; string str = "Hello"; int arrayLength = str.length(); for (int i = 0; i < arrayLength; i++) strarray[i] = str[i];
Another way to access the char array is to simply get the pointer at the start of the string like this.
CPP Syntax (Toggle Plain Text)
string str = "Hello"; char *cstr = &str[0];
(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.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,652
Reputation:
Rep Power: 40
Solved Threads: 988
•
•
•
•
CPP Syntax (Toggle Plain Text)
char strarray[100]; string str = "Hello"; int arrayLength = str.length(); for (int i = 0; i < arrayLength; i++) 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.
•
•
•
•
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? >_>
c++ Syntax (Toggle Plain Text)
char strarray[100] = {0}; string str = "Hello"; int arrayLength = str.length(); for (int i = 0; i < arrayLength; i++) strarray[i] = str[i];
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,652
Reputation:
Rep Power: 40
Solved Threads: 988
•
•
•
•
Well it's an easy fix isn't it? >_>
c++ Syntax (Toggle Plain Text)
char strarray[100] = {0}; string str = "Hello"; int arrayLength = str.length(); for (int i = 0; i < arrayLength; i++) strarray[i] = str[i];
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.
•
•
Join Date: Oct 2007
Posts: 260
Reputation:
Rep Power: 2
Solved Threads: 19
Everyone has a different way of writing their codes. What you may find long may be easy for someone else. I dont think from what you originally wanted pointers would be of alot of help but it can be done to.
>>Also if someone could give me a quick explanation of that std:"something here" thing
This can be used if the statement "using namespace std" has not been included in the code. All the files in the C++ standard library declare all of its entities within the std namespace. That is why someone can do something like this
>>Also if someone could give me a quick explanation of that std:"something here" thing
This can be used if the statement "using namespace std" has not been included in the code. All the files in the C++ standard library declare all of its entities within the std namespace. That is why someone can do something like this
std::cout<<"Hello";
Last edited by joshmo : Jul 24th, 2008 at 2:43 pm.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Hybrid Mode