Question

I need help to fix this program it is written in c++ and needs to run...

I need help to fix this program it is written in c++ and needs to run in visual studio. Program uses character strings from a file containing on the first line and answer key and every other line after is the students ID number followed by a space then their test answers.Program currently reads only test answer key and student ID and their answers of second line.  Sample data can be found below.

TFTTFTFFFTTTFFTTFFFT
HUB00123 TFTFFFTFTFTFTTTFTTTT
SMU12456 FFTFTTFFTTTTTTFFTTFT

#include
#include
#include
#include

using namespace std;
void readValues(ifstream&inF, char key[],char sID[],char sAns[]);
void computeScore( char key[],char sAns[],int& score);
void print(char key[], char sID[], char sAns[], int& score);
int main()
{
   char key[21];
   char sAnswer[21];
   char studentID[8];
   int size = 21;
   int p = 0;
   ifstream inFile;
   inFile.open("test.txt");
   ofstream outFile;
   outFile.open("results.txt");
   readValues(inFile, key,studentID,sAnswer);
   computeScore(key, sAnswer,p);
   print(key, studentID, sAnswer, p);

       system("pause");
  
}

void readValues(ifstream & inF, char key[], char sID[], char sAns[])
{
   char discard;
   inF.get(key, 21);
   int count=0;
   while (!inF.eof())
   {
       inF.get(discard);
       while (count < 3)
       {
           inF.get(sID, 9);
           for (int y = 0; y < 9; y++)
           {
               cout << sID[y] << " ";
           }

           inF.get(sAns, 21);
           for (int z = 0; z < 21; z++)
           {
               cout << sAns[z] << " ";
               if ((z) % 20 == 19)
                   cout << endl;
               count++;
               inF.get(discard);
           }
       }
       }
}


void computeScore(char key[], char sAns[], int& score)
{
   for (int i = 0; i < 21; i++)
   {
       if (key[i] == sAns[i])
           score++;

   }
}
void print(char key[], char sID[], char sAns[], int& score)
{
   cout << sID << " " << score << endl;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

uick Launch (Ci+a Mossuft Vsual Studio Fie Feit w Pmjert Build Dehug Team Toele Tet Anaye Windew Help //Header files 6 | -1#i---------------------------------------------------

Program

//Header files
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
//Function prototypes
void readValues(ifstream&inF, char *key, char *sID, char *sAns, int& score,ofstream& ouT);
void computeScore(char *key, char *sAns, int& score);
void print(char *key, char *sID, char *sAns, int& score, ofstream& ouT);
int main()
{
   //VAriables for file data storage
   char key[21];
   char sAnswer[21];
   char studentID[8];
   int size = 21;
   int p = 0;
   ifstream inFile;
   //Input file
   inFile.open("C:/Users/deept/Desktop/test.txt");
   ofstream outFile;
   //Output file
   outFile.open("C:/Users/deept/Desktop/results.txt", std::ios_base::app);
   //CAll method to get data
   readValues(inFile, key, studentID, sAnswer,p,outFile);
   system("pause");

}
//Read each line and call other functions to get result
void readValues(ifstream&inF, char *key, char *sID, char *sAns, int& score, ofstream& ouT)
{
   char discard;
   //Get key
   inF.get(key, 21);
   int count = 0;
   //Until end of the file
   while (inF.get(discard))
   {
       //Get student id
       inF.get(sID, 9);
       //Get answer key
       inF.get(sAns, 21);
       inF.get(discard);
       //Call method to get score
       computeScore(key, sAns,score);
       //print and output result
       print(key, sID, sAns, score,ouT);
       score = 0;
   }
}
//Method to calculate score
void computeScore(char *key, char *sAns, int& score)
{
   for (int i = 0; i < 20; i++)
   {
       if (key[i] == sAns[i])
           score++;
   }  
}
//Method to print result
void print(char *key, char *sID, char *sAns, int& score, ofstream& ouT)
{
   cout << sID << " " << score << endl;
   ouT << sID << " " << score << endl;
}

-----------------------------------------------------------------------------

Output

HUB00123 11
SMU12456 11

Add a comment
Know the answer?
Add Answer to:
I need help to fix this program it is written in c++ and needs to run...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • can someone help me fix this. The function that finds the minimum and maximum values of...

    can someone help me fix this. The function that finds the minimum and maximum values of the array and outputs the value and index doesnt find the maximum value of the array. #include <iostream> #include <fstream> #include<iomanip> using namespace std; void readArray(ifstream& inF, int arr[], int&ArrSize); void writeArray(ofstream & outF, int arr[], int & ArrSize); void MaxAndMin(ofstream & outF, int arr[], int & ArrSize, int &high, int &low); int main() {    int arr[100];    int i = 0;   ...

  • I have 2 issues with the C++ code below. The biggest concern is that the wrong...

    I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...

  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • Hi there! I need to fix the errors that this code is giving me and also...

    Hi there! I need to fix the errors that this code is giving me and also I neet to make it look better. thank you! #include <iostream> #include <windows.h> #include <ctime> #include <cstdio> #include <fstream> // file stream #include <string> #include <cstring> using namespace std; const int N=10000; int *freq =new int [N]; int *duration=new int [N]; char const*filename="filename.txt"; int songLength(140); char MENU(); // SHOWS USER CHOICE void execute(const char command); void About(); void Save( int freq[],int duration[],int songLength); void...

  • Code in C++: Please help me fix the error in function: void write_account(); //function to write...

    Code in C++: Please help me fix the error in function: void write_account(); //function to write record in binary file (NOTE: I able to store data to a txt file but however the data get error when I try to add on data the second time) void display_all(); //function to display all account details (NOTE: This function is to display all the info that save account.txt which is ID, Name, and Type) void modify_account(int); //function to modify record of file...

  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

  •    moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring>...

       moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring> using namespace std; typedef struct{ int id; char title[250]; int year; char rating[6]; int totalCopies; int rentedCopies; }movie; int loadData(ifstream &infile, movie movies[]); void printAll(movie movies[], int count); void printRated(movie movies[], int count); void printTitled(movie movies[], int count); void addMovie(movie movies[],int &count); void returnMovie(movie movies[],int count); void rentMovie(movie movies[],int count); void saveToFile(movie movies[], int count, char *filename); void printMovie(movie &m); int find(movie movies[], int...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT