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.

Recommended Answers

All 13 Replies

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.

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()!

Rush after prev post (an example and the answer to the question):

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:

char strarray[100];
string str = "Hello";
int arrayLength = str.length();
for (int i = 0; i < arrayLength; i++)
    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.

string str = "Hello";
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.

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? >_>

char strarray[100] = {0};
string str = "Hello";
int arrayLength = str.length();
for (int i = 0; i < arrayLength; i++)
    strarray[i] = str[i];

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

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.

Thanks guys (or girls). Didn't understand everything that was said but very helpful

Thanks guys (or girls). Didn't understand everything that was said but very helpful

What didnt you understand?

What didnt you understand?

Erm, I am a bit confused about this bit (hope I have quoted the code properly)

CPP Syntax (Toggle Plain Text)
string str = "Hello";char *cstr = &str[0];string str = "Hello";
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).

I can remember reading something about only needing the first element of a character array when using pointers, or something similar and I think that is important here but I am more confused about char vs char* in general. I don't understand why it is necessary to use pointers here since to my untrained eye strings and arrays seem to be almost equivalent apart from the [] and the null pointer.


Also if someone could give me a quick explanation of that std:"something here" thing. I see it all over and it seems to be pretty fundamental but I haven't come across it any of the things I was taught or the beginners information I have read. From what I can see (probably wrong) it seems to be like an alternative to a header file. But isn't a header file more convenient? You only have to type it once and the code is much easier to follow (imo).

Sorry for jabbering on a bit. Everyone here seems very helpful :)

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

std::cout<<"Hello";

pointers and arrays and similar

link
scroll down around half-way to the section called "Pointers and arrays"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.