Hi everyone, here a program to evaluate the weighted average of a given set of marks and cretits. I would really appreciate a feedback on this, I want to understand if it's written properly. Thanks in advance!
p.s. I've just started C++ programming, maybe this program would be better with dynamics arrays or other stuff, but for now I'm sticking with the basics.
int main () {
double average=0;
double num=0, den=0;
unsignedint m, cfu;
while(true){
cout << "Insert a mark (zero if you want to close the program): " << endl;
cin >> m;
if(m==0) {
cout << "You've just insert 0! " << endl;
break;
}
cout << "insert the related credits: " << endl;
cin >> cfu;
num+=m*cfu;
den+=cfu;
}
average=num/den;
cout << "The average is: " << average;
return 0;
}
#include <iostream>
int main() {
double num {}, den {};
while (true) {
unsigned m {}, cfu {};
std::cout << "Insert a mark (zero if you want to close the program): ";
std::cin >> m;
if (m == 0) {
std::cout << "You've just insert 0!\n";
break;
}
std::cout << "insert the related credits: ";
std::cin >> cfu;
num += m * cfu;
den += cfu;
}
std::cout << "The average is: " << num / den;
}
Wow, thanks, I don't know how the "curly brackets's variables" works, I'll do some research about them. Thanks for the feedback man, I very much appreciate it.
Meantime, I've ugraded the program to this:
int main () {
double average=0;
double num=0, den=0;
unsignedint m, cfu;
while(true){
cout << "Insert a mark (zero if you want to close the program): " << endl;
cin >> m;
if(m==0) {
cout << "You've just insert 0! " << endl;
break;
}
if((m<18)||(m>30)){
cout << endl << "Mark not valid! Try to insert a number between 18 and 30 " << endl;
continue;
}
cout << "insert the related credits: " << endl;
cin >> cfu;
num+=m*cfu;
den+=cfu;
}
average=num/den;
cout << "The average is: " << average << endl;
return 0;
}
wow, there are things obscure to me(like that for function), but it's beautiful! I would problably let the user choose the number of marks. Thanks man!
@zhylian. C++ is a large language and is considered a somewhat complex language. There are often more than one way to accomplish the requirement. As you continue your journey into learning C++, you'll start to understand more and more re the language. It's important to make sure you understand what you are being taught before moving on to new topics.