Invalid conversion char to char* - Copying char in string array to another string array
I'm a beginner in C++ Programming language. I wanted to write a program that take the alphabets in a string array called str, and copy it in a new array called str_alpha.
And the same goes to numbers, the program copies it from str array to str_digit array.
There's my humble code, it might be full of errors and stuff. But this is what I could do now with my very little experience.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
using namespace std;
char str[100], str_alpha[100], str_digit[100];
int main()
{
gets(str);
for (int i=0 ; str[i] ; i++)
{
if (isalpha(str[i]))
{
strcpy (str_alpha[i] , str[i]);
}
else if (isdigit(str[i]))
{
strcpy (str_digit[i] , str[i]);
}
}
cout << "Alpha is " << str_alpha << endl ;
cout << "Number is : " << str_digit << endl ;
return 0;
}
And it gives my those errors :
F:\C++Progs\string\main.cpp||In function `int main()':|
F:\C++Progs\string\main.cpp|18|error: invalid conversion from `char' to `char*'|
F:\C++Progs\string\main.cpp|18|error: initializing argument 1 of `char* strcpy(char*, const char*)'|
F:\C++Progs\string\main.cpp|18|error: invalid conversion from `char' to `const char*'|
F:\C++Progs\string\main.cpp|18|error: initializing argument 2 of `char* strcpy(char*, const char*)'|
F:\C++Progs\string\main.cpp|22|error: invalid conversion from `char' to `char*'|
F:\C++Progs\string\main.cpp|22|error: initializing argument 1 of `char* strcpy(char*, const char*)'|
F:\C++Progs\str开发者_运维知识库ing\main.cpp|22|error: invalid conversion from `char' to `const char*'|
F:\C++Progs\string\main.cpp|22|error: initializing argument 2 of `char* strcpy(char*, const char*)'|
||=== Build finished: 8 errors, 0 warnings ===|
Help me please. Thanks in advance.
First of all, strcpy copies C strings (character arrays) not char
s. Additionally, the lines strcpy(str_digit[i],str[i])
and strcpy(str_alpha[i], str[i])
would still probably be wrong even if this wasn't the case. Since you haven't initialised the arrays str_digit and str_alpha, you'll get a lot of garbage values while printing them and if any of those garbage values happen to be 0x00
, the cout
statements will fail to print the whole string. As already mentioned, you really should be using std::string
rather than char[]
or char*
. Having said that, here are corrected versions of your code for both char[]
and std::string
.
Using gets
is bad practice and you might consider using std::cin
instead. And you might want to use an iterator rather than a simple for
loop.
//using char[]
#include <iostream>
using namespace std;
int main()
{
char str[100] , str_alpha[100] , str_digit[100] ;
int alpha_counter=0, digit_counter=0;
cin.get(str, 99);
for (int i=0 ; str[i] ; i++)
{
if(isalpha(str[i]))
{
str_alpha[alpha_counter] = str[i];
alpha_counter++;
}
else if (isdigit(str[i]))
{
str_digit[digit_counter] = str[i];
digit_counter++;
}
}
str_alpha[alpha_counter] = 0;
str_digit[digit_counter] = 0;
cout << "Alpha is " << str_alpha << endl ;
cout << "Number is : " << str_digit << endl ;
return 0;
}
And the version using std::string
:
//using std::string
#include <iostream>
using namespace std;
int main()
{
string str, str_alpha , str_digit;
cin >> str ;
for (string::iterator it = str.begin();it<str.end();it++)
{
if(isalpha(*it))
{
str_alpha += *it;
}
else if (isdigit(*it))
{
str_digit += *it;
}
}
cout << "Alpha is " << str_alpha << endl ;
cout << "Number is : " << str_digit << endl ;
return 0;
}
Both versions compile without warnings on g++ 4.2.1 with -Wall
and -pedantic
.
I'm a beginner in C++ Programming language.
The do not use char*
and the likes; use C++ predefined types for this, in your case string
:
string str;
cin >> str;
string alpha;
string digit;
for (unsigned int i = 0; i < str.length(); ++i) {
char chr = str[i];
if (isalpha(chr))
alpha.push_back(chr);
else if (isdigit(chr))
digit.push_back(chr);
}
Furthermore, strcpy
is, as the name says, used to copy strings, not individual char
s. You can copy those directly by assigning them. No function call needed.
The problem is that strcpy
is intended to copy a whole string. In this case, you're copying individual characters, not entire strings. For individual characters, you can use a simple assignment.
There are a few other things to deal with though. First of all, you have:
char str[100];
outside any function. That defines an array of 100 char's, and initializes all of them to 0. You then loop through that, trying to find characters that are classified as alphabetical or digits -- which will never happen, because a zero byte ('\0', not '0') is never either one.
In fact, as far as a I can tell, you don't need str
at all. What you (apparently) want is roughly like:
for (i in 0 to 127)
if (alpha(i))
add i to str_alpha
else if (digit(i))
add i to str_digit
As Konrad Rudolph already pointed out, you'd probably be much better off making str_digit and str_alpha into std::string's than arrays of char. Arrays can do the job, but they'll be more work to get really correct.
strcpy
copies a whole string. Use plain assignment (= operator) for copying characters.
You're copying characters over, even though strcpy is designed to copy over strings (arrays of characters). The compiler is therefore complaining because you're supplying 'char' rather than 'char *'. If you're copying characters over then just do:
str_alpha[i] = str[i];
精彩评论