Taking scores from text file to create averages

Write your question here.

Hi, making a quiz with 5 sections, 10 questions in each sections. There is a class made which takes a text file with questions and then this class has the functions to let the user take the quiz and then the class figures out total mark user gets for each sections and quiz.

I've another class that is trying to take answers(total-score for user) from questions class and then create averages for each section and then final average for quiz.

Can anyone help me figure out how to work out averages from strings? Each section in quiz is a string, so i'm unsure how to make averages per string and then total averages of all strings. (below is class, then instances)

#ifndef PRINT_SCORES_H
#define PRINT_SCORES_H

#include <iostream> // input/output data
#include <string> // working with strings
#include <fstream>//fstreaam: read + write from/to file
#include <conio.h> // getch function
#include <stdlib.h>
#include <time.h>// for time
#include <cstdlib> // for exit


using namespace std;

class Print_scores
{
private:

string USER_ID;
string user_Name;
string userTotal;
string userTotalmax;
string time_taken;

public:
void readResults();
};

#endif // PRINT_SCORES_H


function below


#include "Print_scores.h"
using namespace std;

void Print_scores::readResults()
{
ifstream ip("results.txt"); // create file object + open file in read mode
while(ip.good()){
getline(ip,USER_ID,',');
getline(ip,user_Name,',');
getline(ip,userTotal,',');
getline(ip,userTotalmax,',');
getline(ip,time_taken,',');
cout << USER_ID << "\t\t\t" << user_Name<< "\t\t\t" << userTotal<< "\t\t\t" << userTotalmax<< "\t" << time_taken << endl;
}
}

Hello sam1222,

Welcome to the forum.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Before I load up your program a couple of things I see:

Do not put yur include files in the header file this just leads to trouble. The "#include" files should be in the ".cpp" files that you create.

Never put using namespace std; in a header file. You should read this first: http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

I will get the program loaded up and see what else I find.

Hope that helps,

Andy
Hello sam1222,

After reviewing your code I found:

<conio.h> This header file is outdated and no longer used. The new compiler do not support this header file any more. If you want to use it for yourself that is OK, but it would be best to learn a work around for anything you need from this header file.

<stdlib.h> is partly OK. You should use <cstdlib> instead. Since you have <cstdlib> you should delete the <stdlib.h> line.

Again <time.h> should be <ctime>. I do not see anywhere yo are using this.

In your header file the last three variables would work better as numbers or you may have to change the string into a number when you need it. Which would depend on where you might take the program.

I would take the function you have posted and put in in its own ".cpp" file with the same name as the header file.

Since you did not post the input file I did my best to create one. I do not know what you have in mind for the data, so I do not know if my numbers would be what you intended.

Otherwise the program works.

On the last getline of the input I removed the delimiter as there should only be a new line there and the default delimiter is a new line character. I removed a couple of the tabs, but that is just me not wanting it spread out that much, nothing you need to worry about.

Hope that helps,

Andy
Topic archived. No new replies allowed.