•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 401,439 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,873 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 490 | Replies: 13
![]() |
•
•
Join Date: Jul 2008
Posts: 19
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 2:57 pm.
•
•
Join Date: Jan 2008
Location: USA East Cost
Posts: 386
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 3:09 pm.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
•
•
Join Date: Jul 2008
Posts: 370
Reputation:
Rep Power: 3
Solved Threads: 57
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 6:47 pm.
Approximately 298.60465116279069767441860465116 days until I get my first star x]
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,691
Reputation:
Rep Power: 36
Solved Threads: 877
•
•
•
•
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.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
•
•
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: 10,691
Reputation:
Rep Power: 36
Solved Threads: 877
•
•
•
•
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 8:06 pm.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- please help learning converting string to int (C++)
- converting from char to ASCII to char (C++)
- Splitting words from string into character array (C++)
- convert char* to double (C++)
- Stuck a bit on input (C)
- Converting without additional headers (C)
- Convert first character from lowercase to uppercase? (C)
- Help with Class, stuck. (C++)
- Help with fstream (C++)
- reading a file into code (Java)
Other Threads in the C++ Forum
- Previous Thread: queue ?
- Next Thread: *.stl files



Linear Mode