Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The purpose of this assignment is to process rat exam data and calculate a ratIQ

ID: 3666718 • Letter: T

Question

The purpose of this assignment is to process rat exam data and calculate a ratIQ for each rat. Input: The user will be put the data for the rats in a text file named "test.txt" (here is an example that you can use). The first line of the input file will be the number of rats who took the test. Each line after that will contain a name (string) a number of correct test answers (int) and the number of seconds it took to take the exam (int). Each piece of data will be separated by one blank space. For example: 4 George 24 34 Jane 24 17 Joan 30 32 Phil 25 29 Processing: First the program will read in the number of tests. Then for each test the program will read in the name and calculate the rat's IQ score using the following criteria. The rat gets 10 points for each correct answer and an additional 5 points if he/she took less than 30 seconds to complete the exam. For the data above, George would have a ratIQ of 240 and Jane would have a ratIQ of 245. Output: The program will print one line of output for each rat that contains the rat's name and ratIQ score.For the above input, the output could look like this: George has a ratIQ of 240. Jane has a ratIQ of 245. Joan has a ratIQ of 300. Phil has a ratIQ of 255. Notes: Your source program file must be named prog3_xxxxxx.cpp where xxxxxx is your Texas State net ID Appropriate values should be stored in constant variables. You must use a function to calculate the ratIQ from the given data. We discuss this in detail in class. You must use a function to print the results for each rat to the screen. We will discuss this in detail in class. You will need to use an "if" statement to determine if the rat gets the additional 5 ratIQ points. This "if"statement will be in the function that calculates the ratIQ. We will discuss the process of getting the data into the program in class. Be sure to follow the documentation standards for the course. Each variable must be declared on a separate line with a brief comment following the declaration Use meaningful variable names Turn in: Source file printout. (hand in at the beginning of class on 2/16 ) Submit: using this link, the source file (.cpp) updated: 2/2/2016

Explanation / Answer

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <iterator>
using namespace std;

int str2int (const string &str) {
stringstream ss(str);
int num;
if((ss >> num).fail())
{
//ERROR
}
return num;
}


int main() {
   ios::sync_with_stdio(false);
   vector<string> lines;
   string line;
   ifstream file("test.txt");
   if (file.is_open()) {
       while (getline(file, line)) {
           lines.push_back(line);
       }
   }

   else {
       cout << "Could not open file" << endl;
   }

   for (int i = 0; i < lines.size(); i++) {
       vector<string> tokens;
       int correctAnswers = 0, timeTaken = 0, totalScore = 0;
       istringstream iss(lines[i]);
       copy(istream_iterator<string>(iss),
           istream_iterator<string>(),
           back_inserter(tokens));
       correctAnswers = str2int(tokens[1]);
       timeTaken = str2int(tokens[2]);
       if (timeTaken < 30)
           totalScore = 5;
       totalScore += correctAnswers*10;
       cout << tokens[0] << " has a ratIQ of " << totalScore << endl;
   }
}