Topic is array. User enters in 100 numbers, but If they type in zero I want the display to be Try again and make the user enter the value again. My code is not functioning in this case. May anyone help me give me an idea of how to solve this issue?
1 2 3 4 5 6 7 8 9
int list[100];
int i;
for (i=0; i<100;i++)
{
cin>>list[i];
if (list[i]=0)
cout<<"Try Again";
}
I'm an idiot. I wasn't thinking. You have to initialize the array to 0 otherwise it holds whatever is in memory when you create it.
You need to do:
1 2 3 4 5 6 7 8 9
int list[100] = {0}; // initialize all elements to 0
for (int i = 0; i < 100; i++)
{
do{
cin >> list[i];
if(list[i] == 0)
cout << "Try Again";
}while(list[i] == 0);
}
[EDIT] Wanted to elaborate if this is found in archive searches later. Not initializing the array to 0 made it so the array had a random number according to whatever was in memory so it was failing on if(list[i]==0) because technically it wasn't empty but had a number (positive or negative).