I have this part in the program
char size_input[5]={'1','0','4','-'};
for (int i=0;i<6;i++){
cin >> size_input[i];
if(size_input[i]!=char(45)){
valid_size_characters++;
}else{
i=6;
}
}
It compiles with no error in both windows and linux but in windows when the program reaches that part it just crashes and I have no idea why
It's an off-by-one error because your array has a size of 5 while the loop runs 6 times resulting in writing out of the array bounds causing undefined behavior. So it should be i<5
instead of i<6
.
Also avoid exiting the loop by manipulating the loop index i
, instead you could use break
.