Is there an easier way to write this code by using like an array to store the names of functions?
for example something like this
QString functions
[] = { "food()",
"calorie" };
for(int i=0;i<2;i++){
output << entrynum->functions[i]
}
QString functions [] = { "food()", "calorie" };
for(int i=0;i<2;i++){
output << entrynum->functions[i]
}
To copy to clipboard, switch view to plain text mode
I tried doing something like this but doesn't work for me because of the ->
The code I have which works as I wish but doesn't look very neat unfortunately. =/
for(int i = 0; i < 9; i++){
string filename = "/";
string number;
stringstream convert;
convert << i;
number = convert.str();
filename.append(number);
filename.append(".txt");
string entrynum = "entry";
entrynum.append(number);
//cout << "The filename is: " << filename << endl;
output.setCodec("UTF-8");
for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) {
ABItem *entrynum =
static_cast<ABItem *>(treeWidget->topLevelItem(i));
if(filename == "/0.txt"){
output << entrynum->food() << "\r\n" << endl;
output << endl;
} else if(filename == "/1.txt"){
output << entrynum->calorie() << "\r\n" << endl;
output << endl;
} else if(filename == "/2.txt"){
output << entrynum->protein() << "\r\n" << endl;
output << endl;
} else if(filename == "/3.txt"){
output << entrynum->carb() << "\r\n" << endl;
output << endl;
} else if(filename == "/4.txt"){
output << entrynum->carb() << "\r\n" << endl;
output << endl;
} else if(filename == "/5.txt"){
output << entrynum->fat() << "\r\n" << endl;
output << endl;
} else if(filename == "/6.txt"){
output << entrynum->sugar() << "\r\n" << endl;
output << endl;
} else if(filename == "/7.txt"){
output << entrynum->sodium() << "\r\n" << endl;
output << endl;
} else if(filename == "/8.txt"){
output << entrynum->fiber() << "\r\n" << endl;
output << endl;
}
}
}
}
for(int i = 0; i < 9; i++){
string filename = "/";
string number;
stringstream convert;
convert << i;
number = convert.str();
filename.append(number);
filename.append(".txt");
string entrynum = "entry";
entrynum.append(number);
//cout << "The filename is: " << filename << endl;
QString filename2 = QString::fromStdString(filename);
QFile i(QDir::homePath() + filename2);
if (i.open(QIODevice::WriteOnly)) {
QTextStream output(&i);
output.setCodec("UTF-8");
for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) {
ABItem *entrynum =
static_cast<ABItem *>(treeWidget->topLevelItem(i));
if(filename == "/0.txt"){
output << entrynum->food() << "\r\n" << endl;
output << endl;
} else if(filename == "/1.txt"){
output << entrynum->calorie() << "\r\n" << endl;
output << endl;
} else if(filename == "/2.txt"){
output << entrynum->protein() << "\r\n" << endl;
output << endl;
} else if(filename == "/3.txt"){
output << entrynum->carb() << "\r\n" << endl;
output << endl;
} else if(filename == "/4.txt"){
output << entrynum->carb() << "\r\n" << endl;
output << endl;
} else if(filename == "/5.txt"){
output << entrynum->fat() << "\r\n" << endl;
output << endl;
} else if(filename == "/6.txt"){
output << entrynum->sugar() << "\r\n" << endl;
output << endl;
} else if(filename == "/7.txt"){
output << entrynum->sodium() << "\r\n" << endl;
output << endl;
} else if(filename == "/8.txt"){
output << entrynum->fiber() << "\r\n" << endl;
output << endl;
}
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks