It says the variable 'i' is used without being initialized.
The problem was not what iHutch105 said but because in line 40 when you use i, it has not been given a sensible value and is possibly indexing out of bounds of your array.
i must have a value before this line is executed. cin >> scores[i];
In this loop the user will be asked "More" 10 times and their response is never acted upon.
1 2 3 4 5 6 7 8
for (int i = 0; i < SIZE; i++)
{
cout << "Score " << i + 1 << ": ";
cin >> scores[i];
cout<<"More? :";
cin>>more;
}
Remove return from the else branch and place on a line before the closing brace of main.
The problem was not what iHutch105 said but because in line 40 when you use i, it has not been given a sensible value and is possibly indexing out of bounds of your array.
i must have a value before this line is executed.
i does have a value at this point. It's assigned 0 at the first iteration of the loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int main()
{
int i;
for (i=0; i<5; i++) // i initialised in the first statement
{
cout << "i is: " << i << endl;
}
cout << "Final value of i is: " << i << endl;
}
Results
i is: 0
i is: 1
i is: 2
i is: 3
i is: 4
Final value of i is: 5
Yes but in OPs code i is not assigned before it is used in line 40.
Oh, I see what you mean. I didn't spot that scores[i] at line 40, I was looking at the one on line 55. Yeah, this will cause problems. It should compile fine, but at that point i will hold nothing but junk.
Redeclaring i in for (int i = 0; i < SIZE; i++) is no problem since it is a different variable.
However, this isn't true. If i already exists in the scope of this loop then this would be considered a redefinition of i. Should result in a compilation error.
However, this isn't true. If i already exists in the scope of this loop then this would be considered a redefinition of i. Should result in a compilation error.
Did some searching on this and it may be depend on the compiler. I had assumed it was a different variable due to it only having scope in the loop.
The following code compiled in my version of CodeBlocks (gcc).
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
usingnamespace std;
int main (int argc, char *argv[])
{
int i;
cout<<i<<" "<<&i<<endl;
for (int i=0;i<5;i++){cout<<i<<" "<<&i<<endl;}
cout<<i<<" "<<&i<<endl;
return 0;
}