For loop only executing once? [closed] - c++

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 don't know if I'm just being stupid, but Visual Studio says that my for loop is only executing once, and it does seem to be the case. However I can't figure out why. I'd really appreciate it if anyone could tell me what I'm doing wrong.
for (int i = (nbToVerify - 1); i == 1; i--)
{
if (nbToVerify % i == 0)
{
nbIsPrime = false;
break;
}
else
{
nbIsPrime = true;
}
}
Thanks!

i == 1 should be i != 1, that's it.

Related

Why this code gives run time error on mid calculation? [closed]

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
int main() {
int start = 0;
int end = 2;
int ret1 = 0;
int mid = start + (end-start)/2;
ret1 = mid;
return ret1;
}
Why is this code giving runtime error?
https://ideone.com/iAdQO0
A nonzero return code from main() indicates an error to many operating systems.
Because any return from main that is not 0 indicates a runtime error.

C++ comparing string elements to an int [closed]

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 4 years ago.
Improve this question
I have number lying in a string variable.
I wanna check if every one of its elements is equal to some value, so I use the for loop to loop over every element and use if:
int zera = 0, jedynki = 0;
for (int i = 0; i < liczba.length(); i++) {
if (liczba[i] == 0) zera ++;
else if (liczba[i] == 1) jedynki ++;
}
liczba is a string.
I know now that I can't do that. I tried to convert this int into char but still, nothing happened.
What's wrong here? What should I do?
you're comparing int with char
should be:
if (liczba[i] == '0') {}
else if (liczba[i] == '1') {}
should be used:
if (liczba[i] == '0')
or using atoi:
if (atoi(liczba[i]) == 0)

Project Euler #5 - Why won't this while loop finish? [closed]

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 5 years ago.
Improve this question
If I change the code line "num++" to "num+=2520", the code runs fine and returns the correct answer, but I'd like to know why it doesn't run as is, primarily because I didn't think of the fact that the number must be a multiple at 2520 before looking the answer up, and I don't see why my own code isn't correctly giving the answer without that change. To me, it seems correct. Unfortunately, the while loop never ends.
My guess is it has something to do with how long the correct number is (232792560), because if I lower the requirements even a little bit (from 9 to 8, per se), the while loop manages to finish.
long long int num = 1;
int div_counter = 1;
bool check = false;
while(!check)
{
for(int i = 2; i < 21; i++)
{
if(num % i == 0)
{
div_counter++;
}
}
if(div_counter == 20)
{
check = true;
}
else
{
num++;
div_counter = 0;
}
}
return num;
You have to reset div_counter to 1 instead of 0.
Your for loop only runs from 2 to 20 inclusive, so if div_counter starts at 0 the max value it can reach is 19.

Variable not declared in this scope [closed]

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
Sorry if this is very basic question. I'm a Java guy and new to CPP. As per logic everything looks fine to me. But the compiler still throwing the error
"result was not declared in this scope " What might be the reason ?
And line causing error is
ns1__changePinTestResponse* result= soap_new_ns1__ChangePinResponse(this);
Here are my codes.
int ATLServerService::changePinTest(_ns1__changePinTest *ns1__changePinTest, _ns1__changePinTestResponse *ns1__changePinTestResponse)
{
LOG_DEBUG_MSG("ATLServerService::changePassword()");
char* accountID = (char*)ns1__changePinTest->accountID.c_str();
int accountPin = ns1__changePinTest->accountPin;
char* newPassword = (char*)ns1__changePinTest->newPassword.c_str();
char* yourSerialNumber = (char*)ns1__changePinTest->yourSerialNumber.c_str();
tfaddress transactionFileAddress;
int returnValue = 0;
returnValue = Account::changePassword(toteGateway, myGdi,accountID, accountPin,
newPassword, yourSerialNumber, &transactionFileAddress);
ns1__changePinTestResponse* result= soap_new_ns1__ChangePinResponse(this);
result->code = returnValue;
if (returnValue == ESUCCESS) {
result->message = &SUCCESS;
result->transactionFileAddress = transactionFileAddress;
} else {
result->message = &ERROR;
}
ns1__changePinTestResponse->changePinTestReturn= result;
return SOAP_OK;
}
You are missing the first underscore. It should be:
_ns1__changePinTestResponse
not
ns1__changePinTestResponse

Binary Search Tree, height [closed]

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
this is my height function in bst. cpp
int IntBinaryTree::getHeight(TreeNode * nodePtr)
{
if(nodePtr = NULL)
return 0;
else
return (max(1+getHeight(nodePtr->left), 1+getHeight(nodePtr->right)));
}
When I call it in main(). I got an error.
this is my main()
int main {
IntBinaryTree tree;
....
tree. getHeight();
return 0;
}
You didn't say what error, but looks like changing:
if(nodePtr = NULL)
to
if(nodePtr == NULL)
^^
is what you need.