D
Deniz Dogan
Hello.
I am rather new to C/C++ and the only programming/scripting languages
that I know (well) is Java, Ruby, Python and similar high-level
languages. Just so you know where I'm coming from.
I have a really simple problem. I have this piece of code:
// Array of "numerical" characters.
char numbers [] = {'0','1','2','3','4','5','6','7','8','9'};
// Checks whether a char is a numerical character (0-9).
bool isnumber(char x) {
for (int i = 0; i < sizeof(numbers); ++i) {
if (x == numbers) {
return true;
}
}
return false;
}
int main() {
string mystring;
getline(cin, mystring);
string x = "", y = "";
for (int i = 0; i < sizeof(mystring); ++i) {
if (isnumber(mystring))
x << mystring;
else
y << mystring
}
return 0;
}
As you can see, I'd like to have one string x containing all of the
numerical characters from the input, and all the non-numerical ones in a
string y.
The compilation error that I get is the following:
addressbook.cpp: In function `int main()':
addressbook.cpp:40: error: no match for 'operator<<' in 'x <<
(&mystr)->std::basic_string<_CharT, _Traits, _Alloc>:perator[] [with
_CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>](((unsigned int)i))'
addressbook.cpp:47: error: no match for 'operator<<' in 'y <<
(&mystr)->std::basic_string<_CharT, _Traits, _Alloc>:perator[] [with
_CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>](((unsigned int)i))'
My guess is that this means that I am disallowed to use the << operator,
for some reason.
Any help is highly appreciated and I apologize for not being able to
solve this problem myself, as it probably is a very simple error.
-- Deniz Dogan
I am rather new to C/C++ and the only programming/scripting languages
that I know (well) is Java, Ruby, Python and similar high-level
languages. Just so you know where I'm coming from.
I have a really simple problem. I have this piece of code:
// Array of "numerical" characters.
char numbers [] = {'0','1','2','3','4','5','6','7','8','9'};
// Checks whether a char is a numerical character (0-9).
bool isnumber(char x) {
for (int i = 0; i < sizeof(numbers); ++i) {
if (x == numbers) {
return true;
}
}
return false;
}
int main() {
string mystring;
getline(cin, mystring);
string x = "", y = "";
for (int i = 0; i < sizeof(mystring); ++i) {
if (isnumber(mystring))
x << mystring;
else
y << mystring
}
return 0;
}
As you can see, I'd like to have one string x containing all of the
numerical characters from the input, and all the non-numerical ones in a
string y.
The compilation error that I get is the following:
addressbook.cpp: In function `int main()':
addressbook.cpp:40: error: no match for 'operator<<' in 'x <<
(&mystr)->std::basic_string<_CharT, _Traits, _Alloc>:perator[] [with
_CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>](((unsigned int)i))'
addressbook.cpp:47: error: no match for 'operator<<' in 'y <<
(&mystr)->std::basic_string<_CharT, _Traits, _Alloc>:perator[] [with
_CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>](((unsigned int)i))'
My guess is that this means that I am disallowed to use the << operator,
for some reason.
Any help is highly appreciated and I apologize for not being able to
solve this problem myself, as it probably is a very simple error.
-- Deniz Dogan