+ 2
Why I got error at line 13
9 Antworten
+ 3
Girish Sahu
Line 13 fails because you cannot set an array size using a double, t should be an int.
Change that and your code runs, although it doesn't do what you want and you should format it more clearly.
+ 3
DavX
Given your great experience and skill level I think you may forget some here like myself are beginners and those issues are of no current concern (and shouldn't be) , they are outside the scope of this learning environment, and our focus should sometimes be on learning the tools being taught in the Sololearn courses.
There's probably a good reason those issues, for example VLA's (whatever they may be) or compatibility with 12 year old versions of C++, are not addressed in the Sololearn C++ courses. Walk before you run.
However pointing out array size cannot be a double is well within the scope of the C++ courses.
+ 2
You simply cannot do that.
You are using an array, which needs to know certain bits of information at compile time. The array expects a constant value for the number of items ([3] for example), however you are trying to initialize an array with input from the console.
You can avoid this by using a std::vector, give me 2 minutes and I will show you an example.
+ 2
followed on, here's a very quick example of your code using a vector:
#include <iostream>
#include <vector>
using namespace std;
int main() {
cout << "New Project!\n";
double own_mark;
int students;
cout << "Enter your marks: ";
cin >> own_mark;
cout << "Enter number of your class students: ";
cin >> students;
std::vector<double> student_marks;
for (int i = 1; i <= students; i++) {
double mark;
cout << "Enter student " << i << "'s mark: " << endl;
cin >> mark;
student_marks.push_back(mark);
}
double avg, sum;
for (auto mark : student_marks) {
sum += mark;
}
avg = sum / students;
cout << "Class total marks: " << sum << endl
<< "Class average mark: " << avg << endl;
return 0;
}
+ 2
Another method (if you really wanted to use an array of doubles) is to use a pointer, then you can use the keyword 'new' to allocate memory at runtime for the array. You just have to remember to un-allocate this memory afterwards.
Also you can avoid the second loop by adding the mark to the sum during the first loop. Hopefully this makes sense.
#include <iostream>
using namespace std;
int main() {
cout << "New Project!\n";
double own_mark;
int students;
double* student_marks = nullptr;
cout << "Enter your marks: ";
cin >> own_mark;
cout << "Enter number of your class students: ";
cin >> students;
student_marks = new double[students];
double avg, sum;
for (int i = 1; i <= students; i++) {
double mark;
cout << "Enter student " << i << "'s mark: " << endl;
cin >> mark;
student_marks[i-1] = mark;
sum += mark;
}
avg = sum / students;
cout << "Class total marks: " << sum << endl
<< "Class average mark: " << avg << endl;
delete [] student_marks;
return 0;
}
+ 2
Girish Sahu DavX
Actually it's quite easy to create an array upon initialization using cin. The first integer input is the number of students and will determine the array size. The following integers (doubles) are placed in the array by the loop.
For some beginner coders this is a viable option and a good opportunity to put newly learned arrays to use. This case is one example, where the teacher will know how many students they have and therefore the array size need not be altered.
While vectors are certainly worth learning they are not a part of the Sololearn curriculum and for some may seem like a bit of a leap forward.
https://code.sololearn.com/cshGto3MKi62/?ref=app
0
Scott,
That all depends on the compiler, C11 for example wouldn't allow this:
Line 17: error: ISO C++ forbids variable-size array 'marks'
As far as I was aware VLA's arn't even part of the C++ standards (C99) and can cause stack overflow problems...
0
Scott,
I understand completely where you are coming from and appreciate your kind comments.
As you said, the Sololearn course doesn't even touch on VLA's (which was your own suggestion, creating an array from a variable is a VLA). However the course does teach dynamic memory (my suggestion when using pointers).
It's good to know that VLA's are not a C++ standard, just compatibility added through C99 - If we are speaking about C then great, for C++ it should be avoided (it's a conditional feature that's implementation specific, C++ compilers do not have to provide support).
0
Scott,
Great to see your still editing away, it's not related to a 12 year old version of C++.
It's purely that fact that it's not C++, it's compatability added in by C99 and if you are learning C++ you don't need to be picking up bad habbits from the start.
C11 was added as an example as that uses C98 as a subset of C.