I don't understand why this code breaks the loop when tot is equal or less than 10 and it doesn't when it is more than 10.
I can't believe I still can't manage this basic thing.. help me please!
do {
tot = 0;
system("CLS");
printf("Inserisci 10 sfere tra rosse, blu e verdi");
printf("\n\n Rosse : ");
scanf(" %d" , &rosse);
tot += rosse;
for (i = 0; i < rosse; i++) {
sfere[i] = 1;
}
printf("\n Verdi : ");
scanf(" %d", &verdi);
tot += verdi;
for (i = 0; i < verdi; i++) {
sfere[i+rosse] = 2;
}
printf("\n Blu : ");
scanf(" %d", &blu);
tot += blu;
for (i = 0; i < blu; i++) {
sfere[i+rosse+verdi] = 3;
}
if (tot == 10) {
break;
}
} while (true);
I tried as well doing it like this but i get the same result..
while (tot != 10);
I underline that i am using scanf and printf because my university course wants to, to make us understand better how the memory works and keep safe from errors in future.
if (tot == 10) { break; }- That ends the loop whentotis 10sfere, but given the way you index the array by adding multiple indexes together, you may well be over-running the array, and possibly changing the value oftotvia undefined behavior.scanfis always going to bite you.srand(time(NULL))instead ofsrand(time(&t))