Why this code gives run time error on mid calculation? [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
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.

Related

For loop only executing once? [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
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.

Runtime error: addition of unsigned offset [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 2 years ago.
Improve this question
I was solving a question on Leetcode(Trapping rain water) and i wrote my solution which had been tested on my local machine as well as on GeeksForGeeks where it passed all TC's. the code is:
int trap(vector<int>& height) {
int size = height.size();
int i,units;
vector<int> l(size),r(size);
l[0] = height[0];
r[size-1] = height[size-1];
for(i=1; i<size; i++){
l[i] = max(height[i-1],l[i-1]);
}
for(i=size-1; i>=0; i--){
r[i-1] = max(r[i],height[i]);
}
for(i=0; i<size; i++){
if((min(l[i],r[i]) == 0) || (min(l[i],r[i])-height[i]<0))
continue;
else{
units +=min(l[i],r[i])-height[i];
}
}
return units;
}
This is the only part i had to edit. And upon running it i'm getting the following error
Line 928: Char 34: runtime error: addition of unsigned offset to 0x6040000000d0 overflowed to 0x6040000000cc (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_vector.h:933:34
I need some help finding where I'm experiencing a buffer overflow. Cheers in advance.
It was a typo where it should have just been i>0 instead of i>=0.

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.

Why do i get this wrong result? [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
I'M trying to make a code that is able to increase int 'd' by one each time the a+b reach 20.
and if there still any number less than 20 then this will be the int 'c'.
but instead of getting the right result in my next program which is
49-0
i get this wrong answer
47-40
what should i do ?
#include <iostream>
using namespace std;
int main(){
int a=50;
int b=18;
int c=a+b;
int d=0;
int i;
for(i=0;i<c;i++)
{
while(c>20)
{
d+=1;
c=c-20;
break;
}}
cout<<d<<"-"<<c;
return 0;
}
The problem is in your while loop:
while(c > 20)
{
d+=1;
c=c-20;
break;
}
The loop will only execute once because of your break statement.