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
Qt Code:
  1. QString functions [] = { "food()", "calorie" };
  2. for(int i=0;i<2;i++){
  3. output << entrynum->functions[i]
  4. }
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. =/
Qt Code:
  1. for(int i = 0; i < 9; i++){
  2. string filename = "/";
  3. string number;
  4. stringstream convert;
  5. convert << i;
  6. number = convert.str();
  7. filename.append(number);
  8. filename.append(".txt");
  9. string entrynum = "entry";
  10. entrynum.append(number);
  11.  
  12. //cout << "The filename is: " << filename << endl;
  13. QString filename2 = QString::fromStdString(filename);
  14. QFile i(QDir::homePath() + filename2);
  15. if (i.open(QIODevice::WriteOnly)) {
  16. QTextStream output(&i);
  17. output.setCodec("UTF-8");
  18.  
  19. for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) {
  20. ABItem *entrynum =
  21. static_cast<ABItem *>(treeWidget->topLevelItem(i));
  22. if(filename == "/0.txt"){
  23. output << entrynum->food() << "\r\n" << endl;
  24. output << endl;
  25. } else if(filename == "/1.txt"){
  26. output << entrynum->calorie() << "\r\n" << endl;
  27. output << endl;
  28. } else if(filename == "/2.txt"){
  29. output << entrynum->protein() << "\r\n" << endl;
  30. output << endl;
  31. } else if(filename == "/3.txt"){
  32. output << entrynum->carb() << "\r\n" << endl;
  33. output << endl;
  34. } else if(filename == "/4.txt"){
  35. output << entrynum->carb() << "\r\n" << endl;
  36. output << endl;
  37. } else if(filename == "/5.txt"){
  38. output << entrynum->fat() << "\r\n" << endl;
  39. output << endl;
  40. } else if(filename == "/6.txt"){
  41. output << entrynum->sugar() << "\r\n" << endl;
  42. output << endl;
  43. } else if(filename == "/7.txt"){
  44. output << entrynum->sodium() << "\r\n" << endl;
  45. output << endl;
  46. } else if(filename == "/8.txt"){
  47. output << entrynum->fiber() << "\r\n" << endl;
  48. output << endl;
  49. }
  50. }
  51. }
  52. }
To copy to clipboard, switch view to plain text mode