Need help to correct a function !
-
void calcMean(QVariant *rawdata){
double mean = 0.0;
// convert data to QMap
if (rawdata->type() == QVariant::Map)
{
QMap<QString, QVariant> rawdataMap = rawdata->toMap();
QList<QVariant> velocity = rawdataMap["velocity"].toList();
// calc mean value of velocity
for (int i = 0; i < velocity.size(); i++){
mean += velocity.at(i).toDouble();
}
mean = mean / velocity.size();
// write mean into file
QFile outputFile("outputData.txt");
outputFile.open(QIODevice::WriteOnly);
QTextStream stream(&outputFile);
stream << mean;
}
}How can be this function corrected ?
thank you in advance -
What is the problem ? We can help u to correct if we know the issue u r facing
-
There is high chance that you'll get a "Divide by 0 error" on line "mean = mean / velocity.size();" if your velocity.size() is 0.
-
What is the problem ? We can help u to correct if we know the issue u r facing
@dheerendra
That's an exercise , that i have to solve it
and that's the statement :
The errors must be intercepted. Given is the source code to a function. Identify locations where errors can occur and suggest a way to manage errors. Also identify places that could lead to undefined behavior of the software ! -
Hi,
So you are basically asking us to do your homework for you ?
-
@SGaist
looool
it's just an exercise that's not part of my university life!
because I'm not professional in Q.t and I always try to learn new things and solve some exercises :D
but thanks anyway -
Then it's always better to be very precise about what you are currently doing. That will allow people to help you in better ways.
-
Then it's always better to be very precise about what you are currently doing. That will allow people to help you in better ways.
@SGaist
I have to Identify locations where errors can occur and suggest a way to manage errors :/
but here I see no error
and I'am not sure -
@SGaist
I have to Identify locations where errors can occur and suggest a way to manage errors :/
but here I see no error
and I'am not sure@Lab_younes
Although there is no glaringly obvious fault in the code, I presume you are supposed to analyse it and think were it might have problems. One post above made a suggestion. Parameters are worth looking at. I/O can have problems. That sort of thing? -
Hi, do you mean what checks it needs to be production quality ?
void calcMean(QVariant *rawdata) { double mean = 0.0; // convert data to QMap if (rawdata->type() == QVariant::Map) { // ** no check if null before use QMap<QString, QVariant> rawdataMap = rawdata->toMap(); QList<QVariant> velocity = rawdataMap["velocity"].toList(); // ** no check if "velocity" exists. // calc mean value of velocity for (int i = 0; i < velocity.size(); i++) { mean += velocity.at(i).toDouble(); // ** no check if toDouble fails } mean = mean / velocity.size(); // ** no check if list is empty and would divide by zero // write mean into file QFile outputFile("outputData.txt"); outputFile.open(QIODevice::WriteOnly); // ** no check if file could be open QTextStream stream(&outputFile); stream << mean; } } -
Hi, do you mean what checks it needs to be production quality ?
void calcMean(QVariant *rawdata) { double mean = 0.0; // convert data to QMap if (rawdata->type() == QVariant::Map) { // ** no check if null before use QMap<QString, QVariant> rawdataMap = rawdata->toMap(); QList<QVariant> velocity = rawdataMap["velocity"].toList(); // ** no check if "velocity" exists. // calc mean value of velocity for (int i = 0; i < velocity.size(); i++) { mean += velocity.at(i).toDouble(); // ** no check if toDouble fails } mean = mean / velocity.size(); // ** no check if list is empty and would divide by zero // write mean into file QFile outputFile("outputData.txt"); outputFile.open(QIODevice::WriteOnly); // ** no check if file could be open QTextStream stream(&outputFile); stream << mean; } }@mrjj said
Hi
Here no try-catch blocks may be used. For the software developed to be robust, errors must be intercepted. -
@mrjj said
Hi
Here no try-catch blocks may be used. For the software developed to be robust, errors must be intercepted.@Lab_younes
You would not use exception for this function.
But it does no input validation , nor check the conversion. And also
do not check if the out put file could be open.
It should also return a value indication success or not. -
@Lab_younes
You would not use exception for this function.
But it does no input validation , nor check the conversion. And also
do not check if the out put file could be open.
It should also return a value indication success or not.@mrjj
Thank you very much !
it makes sense :D
and can be for example a possibility for error management suggested ? -
@mrjj
Thank you very much !
it makes sense :D
and can be for example a possibility for error management suggested ?@Lab_younes
Hi
It could be done in different ways but minimum it could report back to caller the status.enum calcMeanErros { Success = 1, NullInput, EmptyVelocityList, WrongMapFormat, ConvertError, FileOpenError }; constexpr const char *velocityStr = "velocity"; calcMeanErros calcMean(QVariant *rawdata) { if (! rawdata ) return NullInput; //-> check double mean = 0.0; // convert data to QMap if (rawdata->type() == QVariant::Map) { QMap<QString, QVariant> rawdataMap = rawdata->toMap(); if ( ! rawdataMap.contains(velocityStr) ) return WrongMapFormat; QList<QVariant> velocity = rawdataMap[velocityStr].toList(); if ( velocity.size() == 0 ) return EmptyVelocityList; //-> check // calc mean value of velocity for (int i = 0; i < velocity.size(); i++) { bool ok; mean += velocity.at(i).toDouble(&ok); if (!ok) return ConvertError; //-> check } mean = mean / velocity.size(); // write mean into file QFile outputFile("outputData.txt"); if ( ! outputFile.open(QIODevice::WriteOnly) ) return FileOpenError; //-> check QTextStream stream(&outputFile); stream << mean; return Success; } return WrongMapFormat; }