Posted By

Sleaker on 02/10/11


Tagged

data file simple to read a from just program specific


Versions (?)

filereader


 / Published in: C++
 

Reads a line from a specified txt file in format: Name Gender School Score Gender is M/F, school is UN/CC and Score is Percentage.

  1. // FIleReader.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. // Includes - iostream for input/output to console
  5. // fstream for reading from the file
  6. // string for ease of use string manipulation
  7. // iomanip to format outputs to 2 decimal places.
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11. #include <string>
  12. #include <iomanip>
  13. using namespace std;
  14.  
  15. int main( )
  16. {
  17. //string declarations
  18. string filename;
  19. string currentline;
  20. //declare the input file stream
  21. ifstream scores;
  22. //declaration of numbers for tracking scores and # of students
  23. int male = 0;
  24. int female = 0;
  25. int un = 0;
  26. int cc = 0;
  27. float mtotal = 0;
  28. float ftotal = 0;
  29. float untotal = 0;
  30. float cctotal = 0;
  31. int temptotal;
  32. //Character declarations to check gender, school, and if the program should continue running.
  33. char gender;
  34. char school[3];
  35. char cont = ' ';
  36.  
  37. // While continue is not No then keep running the program.
  38. while ( cont != 'n' && cont != 'N' )
  39. {
  40. //ask user to input location and name of file. - Format should be C:\folder\filename.ext
  41. cout << "Please type the location and name of the file to read" << endl;
  42. cin >> filename;
  43. // Open the file
  44. scores.open (filename);
  45.  
  46. //check to make sure the user entered the name of the file properly, and the file could be opened.
  47. while ( !scores.is_open() )
  48. {
  49. cout << endl << "The file could not be opened properly, please check for proper spelling & location and type again" << endl;
  50. cin >> filename;
  51. scores.open (filename);
  52. }
  53. cout << endl;
  54. //Once the file is open loop through it getting each line till we read the end.
  55. while ( scores.good() )
  56. {
  57. //Get the next line set it as the current line and output it.
  58. getline (scores, currentline);
  59. cout << currentline << endl;
  60. //scan the current line for gender, school type, and score, but ignore name.
  61. sscanf (currentline.c_str(), "%*s %c %s %i", &gender, &school, &temptotal );
  62. //Add up totals for different categories and increment the number of student of each type found for the current line.
  63. if ( gender == 'F' || gender == 'f' )
  64. {
  65. female++;
  66. ftotal += temptotal;
  67. }
  68. else
  69. {
  70. male++;
  71. mtotal += temptotal;
  72. }
  73. if ( strcmp(school, "CC") == 0 )
  74. {
  75. cc++;
  76. cctotal += temptotal;
  77. }
  78. else
  79. {
  80. un++;
  81. untotal += temptotal;
  82. }
  83.  
  84. }
  85.  
  86. //Set decimal precision to 2 places, then output all required data.
  87. cout << endl << fixed << setprecision(2);
  88. cout << "Average score of male students: " << mtotal / male << endl;
  89. cout << "Average score of female students: " << ftotal / female << endl;
  90. cout << "Average score of UN students: " << untotal / un << endl;
  91. cout << "Average score of CC sutdents: " << cctotal / cc << endl;
  92. cout << "Overall average scores: " << (ftotal + mtotal) / (male + female) << endl;
  93.  
  94. //prompt user to continue the program or exit.
  95. cout << "Would you like to continue reading another file? (y/n): ";
  96. cin >> cont;
  97. cout << endl << endl << endl;
  98. }
  99.  
  100. return 0;
  101. }

Report this snippet  

You need to login to post a comment.