Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Here is the code:
bool Vehicle::checkID(std::string id)
{
std::vector<int> digits;
for (char c : id)
{
if(std::isdigit(c))
{
digits.push_back(atoi(c));
}
else
{
digits.push_back(int(c));
}
}
I don't know why he throws this error for "digits.push_back(atoi(c))".
I'm a very beginner, I know this will be not that difficult for you.
You can't do:
atoi(c)
atoi() expects a char *. You probably want
digits.push_back(c - '0');
The function atoi() takes a single const char * type as a parameter.
You're calling it with a char parameter. The compiler doesn't know how to convert from one to the other.
atoi actually wants a string as input, you can't call it with a single character. A string always needs a zero character as a terminator.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I need to compare a character in a string that is in an array with another string. This is a functioning but simple version of my problem:
#include <iostream>
using namespace std;
int main() {
string a_ray[1] = {"asd"};
if (a_ray[0][0] == "a") {
bool a;
}
return 0;
}
Error message: ISO C++ forbids comparison between pointer and integer [-fpermissive]|
What causes this? And how can I do what I want to do in the correct way?
Thank you in advance!
Since you are comparing against a character, your code should be
if (a_ray[0][0] == 'a')
You are trying to compare a character with a character array, hence the error message.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
This is my code:
bool State::operator==(const State& s) const
{
bool flag=true;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(s.GetboardEl(i,j)!=board[i][j]){
flag=false;
}
}
}
return flag;
}
getBoardEl is a function in State class which returns an element from board(int[][]). I get this ERROR even if i'm using const before the brackets({)
:
error: passing 'const State' as 'this' argument of 'int State::GetboardEl(int, int)' discards qualifiers [-fpermissive]|
It appears that the member function getBoardEl is a non-const member function.
You should definitely change that to a const member function since the name implies it is a get function not a set function.
That would resolve the compiler error.
Another way to resolve the error is to use the member variable directly, as you have for this.
if ( s.board[i][j] != this->board[i][j] ) {
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a user defined class, one of whose members is a char* type. When I try to initialize it in the constructor I get an error saying error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.
However, when I changed strcpy to strcpy_s, it would still give the following error IntelliSense: no instance of overloaded function "strcpy_s" matches the argument list argument types are: (char *, char *)
Let's say Student is the class and char* name; is one of the data members.So, my constructor is like:
Student (char* s = NULL) {
if (s != NULL) {
name = new char[strlen(s) + 1];
//strcpy(name,s);
strcpy_s(name,s);
}
}
It's because strcpy_s requires an additional parameter to specify how many bytes to copy.
See here: http://www.cplusplus.com/forum/beginner/118771/
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
if char is only 1 byte (8bit, 255 max size), how 65534th char can be saved in char ?
#include <iostream>
int main() {
unsigned wchar_t a = 65534;
char b = (char)a;
std::cout << b << std::endl;
return 0;
}
also in java you can write byte b = 5000;
do anyone knows why ?
For the C++ code, the wchar_t value gets truncated to fit in the char variable. Normally there would be a warning from the compiler about this loss of data, but by using the C-style cast you've told the compiler "don't worry, I know what I'm doing" and taken responsibility for the consequences of your (questionable) actions.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am trying to find the occurrence of characters of one string(s1) in other string(s2).
This is part of my code.
for(;i<strlen(s1);i++)
{
int x=strchr(s2,s1[i]);
if(x>0)
count++;
}
But on compiling I get an error pointing to strchr() and says
error: invalid conversion from ‘char*’ to ‘int’ [-fpermissive]
Anyone explain what is problem in using strchr() function.
Assignment is wrong strchr doesn't returns int but returns address of first char in string found:
int x=strchr(s2,s1[i]);
should be:
char* x = strchr(s2, s1[i]);
// ^ returns char*
Read manual
char *strchr(const char *s, int c);
RETURN VALUE
The strchr() and strrchr() functions
return a pointer to the matched character or NULL if the character
is not found. The terminating null byte is considered part of the
string, so that if c is specified as '\0', these functions return a
pointer to the terminator.
And so:
if(x>0)
should be:
if(x != NULL)
or just if(x)