Array index out of bounds error Pawn - pawn

I am using spawn points but when it compiles I'm getting this error:
Array index out of bounds
On this line is the error
for(new i =0 ; i < 5 ;i++) {
SetPlayerPos(playerid, spawnpoints[i][0], spawnpoints[i][1], spawnpoints[i][2]);
}
Hoping somebody knows the solution to the error.

Your array spawnpoints has either less than 5 entries or one of the arrays (spawnpoints[0], spawnpoints[1], spawnpoints[2], spawnpoints[3], spawnpoints[4]) has less than 3 entries. Try debugging your code.

Replace 5 with sizeof(spawnpoints). If you still get the error after this, then your spawnpoints array doesn't contain an x, y and z coordinate (and so is incorrectly structured.)

SetPlayerPos(playerid, Float:x, Float:y, Float:z);

Are spawnpoints defined with Float?
new Float:OldPos[MAX_PLAYERS][3];
Try with this example:
new Float:OldPos[MAX_PLAYERS][3];
GetPlayerPos(i, OldPos[i][0], OldPos[i][1], OldPos[i][2]);

Related

Addition Operation from ints in a String

I set up a string filled solely with numbers and using a for loop iterated through it in order to add them together mathematically (Wanted to see if the language would allow this), as a result I got some weird Numbers as the result. Can someone explain why this is happening?
int main()
{
std::string word = "2355412";
for (int i = 0; i<word.size(); i++){
int sum = word[i]+word[i+1];
std::cout << sum << std::endl;
}
return 0;
}
The code when run results in:
101
104
106
105
101
99
50
Due to the way I wrote my code I also believe that it should have resulted in an out of bounds error due word[i+1] on the final value resulting in the calling of a value that does not exist. Can someone explain why it did not throw an error?
The value you get is not what you expect because it is the sum of the ascii code corresponding to the characters you are summing, it's not converted into their value by default.
Also, as mentioned by other, string::operator[] doesn't check if you are trying to reach an out of bound value. In this case, you read 0 because you reached the string termination character \0 which happen to be 0.
it should have resulted in an out of bounds error
string::operator[] doesn't check bounds, it assumes you have. If you call it with an out of bounds index, the entire behaviour of your program is undefined, i.e. anything can happen.
It sounds like you want string::at, which does check bounds, and will throw std::out_of_range

SAS Array subscript out of range

I am getting the Array subscript out of range error:
ERROR: Array subscript out of range at line 408 column 169.
SYM_ROOT=FSV DATE=. TIME_M=. BID=. BIDSIZ=. ASK=. ASKSIZ=. EXN=.
FIRST.SYM_ROOT=1 LAST.SYM_ROOT=1 FIRST.DATE=1 LAST.DATE=1 FIRST.TIME_M=1
LAST.TIME_M=1 nexb1=. nexb2=. nexb3=. nexb4=. nexb5=. nexb6=. nexb7=. nexb8=.
nexb9=. nexb10=. nexb11=. nexb12=. nexb13=. nexb14=. nexb15=. nexb16=. nexb17=.
nexo1=. nexo2=. nexo3=. nexo4=. nexo5=. nexo6=. nexo7=. nexo8=. nexo9=. nexo10=.
nexo11=. nexo12=. nexo13=. nexo14=. nexo15=. nexo16=. nexo17=. sexb1=. sexb2=.
sexb3=. sexb4=. sexb5=. sexb6=. sexb7=. sexb8=. sexb9=. sexb10=. sexb11=.
sexb12=. sexb13=. sexb14=. sexb15=. sexb16=. sexb17=. sexo1=. sexo2=. sexo3=.
sexo4=. sexo5=. sexo6=. sexo7=. sexo8=. sexo9=. sexo10=. sexo11=. sexo12=.
sexo13=. sexo14=. sexo15=. sexo16=. sexo17=. _I_=. i=18 BB=. BO=. MIDPRICE=.
BBSize=. BOSize=. NUMEX=. _ERROR_=1 _N_=6417740
However, I am not sure what happened, because the code has previously worked on a different dataset.
The only thing that I can think of is that, because the dataset I am having problem with is the subset of the original one (which worked), it might not have the complete range of exn (I am using a variable named exn as the index of the array).
I defined the array as:
array nexb nexb:; array nexo nexo:; array sexb sexb:; array sexo sexo:;
The variable I am talking about is called exn, and it is used to reference the array:
nexb(exn)=bid;nexo(exn)=ofr;sexb(exn)=bidsiz;sexo(exn)=ofrsiz;
The arrays are initialized in the following way:
do i=1 to 17;
nexb(i)=.; nexo(i)=.; sexb(i)=.; sexo(i)=.;
end;
Originally exn spans from 1 to 17. Now I think some of the numbers in between might be missing in the dataset. But why is that a problem? They are initialized anyways.
You cannot use a missing value as the index into an array. Your log shows that EXN is missing.

Error when compiling, error in array

float premios[20]={500.00, 700.00, 800.00, 900.00, 1200.00, 1500.00, 1800.00, 2000.00, 2100.00, 2300.00, 2800.00, 3000.00, 3200.00, 3500.00, 4000.00, 10,000.00, 100,000.00, 200,000.00, 500,000.00, 1,000,000.00 };
Look at the code, when i try to compile it, it gives me the error "[Error] too many initializers for 'float [20]' ", it has exactly 20 values, tried to correct it by setting it to 21 values but it didnt work. Then i set the array to an empty array and it worked, can anybody explain me why did it happen?
The "," between each value count as a value. so I think 1,000,000.00 for example count as 3 values. eg. [1, 0, 0]
I believe you were trying to do 1000000.00 instead of 1,000,000.00
Your initializer contains 26 elements.
Adds: Using > float premios[] = ... does not mean it's an empty array -- it means the number of elements in the array is deduced from the initializer, so it will turn out a float[26].

Program crash while using if statement inside a for loop

I am trying to give a shot at Project Euler problem 3 until codeblocks or whatever caused it pissed me off. This is my code, What is wrong with it? I guess more of a bug?
#include <iostream>
using namespace std;
int main()
{
int x=0;
for(int y=0;y<=10;y++)
{
if(13195%x==0)
{
cout<<"I don't know why the program crashes!";
}
}
}
You can't use a 0 as the second operand while doing / or %. What you're essentially saying is "Hey divide by 0 and give me the remainder." Please see the following:
Can't Mod Zero?
Modulus operator divides it by zero and next finds the remainder, thus you will get divide by zero error
x must not be equal to 0 otherwise division by zero.
Just think how many zeroes in 13195?
x = 0. Dividing a number with zero will crash your code. Make sure x is not 0 before 13195 % x.
The operation A modulo B is defined as: the remainder of the division of A by B.
In your code, you have B=0 which means you are trying to divide by zero.

Last array printing 0 C++

I'm making a dice game in C++, and in my program I have some arrays.
die[5] = { (rand()%6)+1, (rand()%6)+1, (rand()%6)+1, (rand()%6)+1, (rand()%6)+1 };
And then I use the arrays with
cout<<"First die: "<< die[0] <<"\n"
etc
But, when I run the program, the last array will always print 0, is there a way I can fix this?
You're not really giving much information, but here's my guess:
You're stepping too far. The last spot in the array is die[4], and chances are you're using die[5], which means you're accessing memory you're not supposed to. On some systems, this will automatically initialize as "0".
Arrays of size N always include N elements ranging from 0 to N-1. Using array[N] accesses memory beyond the range of the array. This could be unused memory (best case) or memory assigned to something else. The result is TROUBLE. Do not do this.
In your code you have this line:
54. cout<<"Sixth die: " << die[5] <<"\n";
which is an invalid access as die has only 5 elements thus 0 to 4 are valid indexes.
This is actually "undefined behaviour". Your program might core-dump / give an access violation but it doesn't have to. It can instead just output some random number or zero...