thats what a computer science major was helping me with, problem is syntax for me just doesnt click, i was told the 40 was to cover the 31, essentially it could be anything as long as it covered my parameters of 31? |
In the case of your code, there are only 31 numbers inside the file you're reading, but you're trying to read 40. That means the input operation that was inside the loop
fin >> nmbr
puts the ifstream into an error state and wouldn't do anything for the last 9 iterations. Now, that doesn't really matter in this case, since you close the file after this loop anyway, but you should remember for future i/o operations.
The other problem which does matter in this case is that you output
nmbr each iteration. So assuming the numbers were 1 through 31, your output would look like this:
1
2
...
30
31
31
31
31
31
31
31
31
31 |
confused on how to do what is asked in the quotes.. |
Well, you know about variables and scope right? If not, the short explanation is that anything in between brackets {} is a local scope. Any variable declared in the local scope only exists while the program is going through the code in the scope. As soon as the program leaves the scope, the variable no longer exists.
You are expected to fill an array that is declared in the
main function by using the
getList function. The functions have their own scope so in order for
getList to access the array, it needs to be passed as a parameter.
Here's an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
void changeArray(int * a, int length)
{
for (int i = 0; i < length; ++i)
a[i] = i;
}
int main()
{
int array[10] = { 0 };
std::cout << "Before function call:\n\t";
for (int i = 0; i < 10; ++i)
std::cout << array[i] << ' ';
changeArray(array, 10);
std::cout << "\n\nAfter function call:\n\t";
for (int i = 0; i < 10; ++i)
std::cout << array[i] << ' ';
return 0;
}
|
Before function call:
0 0 0 0 0 0 0 0 0 0
After function call:
0 1 2 3 4 5 6 7 8 9 |
I hope that example gives you some ideas on how you should change your code too, because as it is, your code doesn't work.