int main() {
// Open the numbers file
ifstream resultsFile(FILE_NAME);
// Ensure the file was opened successfully
if (resultsFile.is_open() == false) {
cout << "Error: could not open the file \"" << FILE_NAME << "\""
<< endl << "Quitting..." << endl;
exit(1); // Terminate the program
}
// Process the result's file
string studentID;
char gender; int counter_7 = 0;
int points;
int counter_8 = 0; int counter_9 = 0; int counter_10 = 0;
int counter_11= 0; int counter_12 = 0; int counter_13 = 0;
int counter_14 = 0; int counter_15 = 0; int counter_16 = 0;
int counter_17 = 0; int counter_18 = 0; int counter_19 = 0;
int counter_20 = 0; int counter_21 = 0; int counter_22 = 0;
int counter_23 = 0; int counter_24= 0; int counter_25 = 0;
int counter_26 = 0; int counter_27 = 0; int counter_28 = 0;
int counter_29 = 0; int counter_30 = 0;
while(resultsFile>> studentID>> gender>> points) {
cout << studentID << " " << gender << " " << points << endl;
// Counting the points
if (points == 7) counter_7++; if (points == 14) counter_14++;
if (points == 8) counter_8++; if (points == 9) counter_9++;
if (points == 10) counter_10++;if (points == 11) counter_11++;
if (points == 12) counter_12++;if (points == 13) counter_13++;
if (points == 15) counter_15++;if (points == 16) counter_16++;
if (points == 17) counter_17++;if (points == 18) counter_18++;
if (points == 19) counter_19++;if (points == 20) counter_20++;
if (points == 21) counter_21++;if (points == 22) counter_22++;
if (points == 23) counter_23++;if (points == 24) counter_24++;
if (points == 25) counter_25++;if (points == 26) counter_26++;
if (points == 27) counter_27++;if (points == 28) counter_28++;
if (points == 29) counter_29++;if (points == 30) counter_30++;
}
// Displaying the results
cout << "\n" ;
cout << "Total count of points" << endl;
cout << "\n";
if (counter_7 > 0) cout << "7 points: " << counter_7 << " "<<"students"<<endl;
if (counter_8 > 0) cout << "8 points: " << counter_8 << " "<<"students" <<endl;
if (counter_9 > 0) cout << "9 points: " << counter_9 << " "<<"students" <<endl;
if (counter_10> 0) cout << "10 points: " << counter_10<< " " <<"students"<<endl;
if (counter_11> 0) cout <<"11 points: " << counter_11<< " " <<"student" <<endl;
if (counter_12> 0) cout <<"12 points: " << counter_12<< " " <<"student" <<endl;
if (counter_13> 0) cout <<"13 points: " << counter_13<< " " <<"students"<<endl;
if (counter_14 > 0) cout <<"14 points: " << counter_14<< " " <<"students"<<endl;
if (counter_15> 0) cout <<"15 points: " << counter_15<< " " <<"students"<<endl;
if (counter_16> 0) cout <<"16 points: " << counter_16<< " " <<"students"<<endl;
if (counter_17> 0) cout <<"17 points: " << counter_17<< " " <<"students"<<endl;
if (counter_18> 0) cout <<"18 points: " << counter_18<< " " <<"students"<<endl;
if (counter_19> 0) cout <<"19 points: " << counter_19<< " " <<"students"<<endl;
if (counter_20> 0) cout <<"20 points: " << counter_20<< " " <<"students"<<endl;
if (counter_21> 0) cout <<"21 points: " << counter_21<< " " <<"students"<<endl;
if (counter_22> 0) cout <<"22 points: " << counter_22<< " " <<"students"<<endl;
if (counter_23> 0) cout <<"23 points: " << counter_23<< " " <<"students"<<endl;
if (counter_24> 0) cout <<"24 points: " << counter_24<< " " <<"students"<<endl;
if (counter_25> 0) cout <<"25 points: " << counter_25<< " " <<"students"<<endl;
if (counter_26> 0) cout <<"26 points: " << counter_26<< " " <<"students"<<endl;
if (counter_27> 0) cout <<"27 points: " << counter_27<< " " <<"students"<<endl;
if (counter_28> 0) cout <<"28 points: " << counter_28<< " " <<"students"<<endl;
if (counter_29> 0) cout <<"29 points: " << counter_29<< " " <<"students"<<endl;
if (counter_30> 0) cout <<"30 points: " << counter_30<< " " <<"student"<<endl;
// Close open resources
resultsFile.close();
> Apr 7, 2019 at 10:18am
> Apr 7, 2019 at 10:58am
You're going to remain a novice programmer if you're just going to give up after 20 minutes without even making an effort.
#include <iostream>
#include <fstream>
#include <string>
int main()
{
constint MIN_POINTS = 7 ;
constint MAX_POINTS = 30 ;
// we create an array of size MAX_POINTS+2
// we intend to store points less than MIN_POINTS in point_counts_array[0]
// and points greater than MAX_POINTS in point_counts_array[MAX_POINTS+1]
// point p in the interval [MIN_POINTS,MAX_POINTS] is stored in point_counts_array[p]
int point_counts_array[MAX_POINTS+2] = {0} ; // initialised to all zeroes
constchar file_name[] = "2018-CSEE-S0800.txt" ; // avoid the #define
std::ifstream file(file_name) ;
if( !file.is_open() )
{
std::cout << "could not open input file \"" << file_name << "\"\n" ;
return 1 ; // returning from main quits the program in an orderly manner
}
std::string student_id ;
char gender ;
int points ;
// read points and update the counts in point_counts_array
while( file >> student_id >> gender >> points ) // for each student info read
{
// note: though we read student_id and gender in this program,
// we do not use them after that
if( points < MIN_POINTS ) ++point_counts_array[0] ;
elseif( points > MAX_POINTS ) ++point_counts_array[MAX_POINTS+1] ;
else ++point_counts_array[points] ;
}
// print results
std::cout << "point counts\n---------------\n"
<< "less than " << MIN_POINTS << ": " << point_counts_array[0] << '\n' ;
for( int p = MIN_POINTS ; p <= MAX_POINTS ; ++p )
std::cout << p << " points: " << point_counts_array[p] << '\n' ;
std::cout << "greater than " << MAX_POINTS << ": " << point_counts_array[MAX_POINTS+1] << '\n' ;
// the file is automagically closed when the file object goes out of scope
// there is an implicit return 0 ; at the end of main
}
Sir,thanks but I think that there is misunderstanding.A file of results.On eachline: a student ID, gender and his/her NECTA
* points are given. Complete the provided program to count how many
* students got each of the points. That is, if available: howmany
* students got 7 points, how many got 8 points, etc. As an example,
* below is the count of the last four points.
*
* 25 points: 3 students
* 26 points: 3 students
* 29 points: 2 students
* 30 points: 1 student
The code below won't compile; it contains two (deliberately introduced) errors.
Try to understand the code, correct the errors, compile and run, and study the output.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>
// print all the student ids in the vector
void print_ids( const std::vector<std::string>& student_ids )
{
std::cout << "( " ;
// range base loop: http://www.stroustrup.com/C++11FAQ.html#for
// auto: http://www.stroustrup.com/C++11FAQ.html#autofor( constauto& id : student_ids ) std::cout << std::quoted(id) << ' ' ;
std::cout << ")\n" ;
}
int main()
{
constint MIN_POINTS = 7 ;
constint MAX_POINTS = 30 ;
// we create an array of size MAX_POINTS+2; each element of the array is a vector of student ids
// we intend to store ids of students with less than MIN_POINTS in student_ids_vector[0]
// and ids of students with greater than MAX_POINTS in student_ids_vector[MAX_POINTS+1]
// ids of a student with point p in the interval [MIN_POINTS,MAX_POINTS] is stored in student_ids_vector[p]
// vector: https://cal-linux.com/tutorials/vectors.html
std::vector<std::string> student_ids_vector[MAX_POINTS+2] ; // default initialised to empty vectors
constchar file_name[] = "2018-CSEE-S0800.txt" ; // avoid the #define
std::ifstream file(file_name) ;
if( !file.is_open() )
{
std::cout << "could not open input file \"" << file_name << "\"\n" ;
return 1 ; // returning from main quits the program in an orderly manner
}
std::string student_id ;
char gender ;
int points ;
// read id and points and update the counts in student_ids_vector
while( file >> student_id >> gender >> points ) // for each student info read
{
// note: though we read the gender, we do not use them after that
// push_back: appends a new item to the end of the sequence
if( points < MIN_POINTS ) student_ids_vector[0].push_back(student_id) ;
elseif( points > MAX_POINTS ) student_ids_vector[MAX_POINTS+1].push_back(student_id) ;
else student_ids_vector[points].push_back(points) ;
}
// print results
// note: the size() member of the vector gives the number of student ids in the vector
std::cout << "point counts\n---------------\n"
<< "less than " << MIN_POINTS << ": " << student_ids_vector[0].size() << " " ;
print_ids( student_ids_vector[0] ) ;
for( int p = MIN_POINTS ; p <= MAX_POINTS ; ++p )
{
std::cout << p << " points: " << student_ids_vector[p].size() << " " ;
}
print_ids( student_ids_vector[p] ) ;
std::cout << "greater than " << MAX_POINTS << ": " << student_ids_vector[MAX_POINTS+1].size() << " " ;
print_ids( student_ids_vector[MAX_POINTS+1] ) ;
// the file is automagically closed when the file object goes out of scope
// there is an implicit return 0 ; at the end of main
}