Question:
I need help helping my little brother with this C++ program to read data files. Im completely in the dark. thx?
anonymous
2009-05-29 21:20:23 UTC
ok it says.
Assuem that you are reading from a data file named progData11.txt which contains two lines. The first line contains the correct answers to a multiple choice test. Each answer is one of the letter A,B,C,D,E. The second line contains the answers of a test taker. Produce three lines of output. The first line is a list of correct ansers. The second is a list of the test taker's answers. The third line is a list of + or - symbols. If a question is answered correctly, a + sign should be printed. If a question is answered incorrectly, a - sign should be printed. Also, the percentage of correct answers should be printed.

Ex.

(data file reads)

ABCDEABCDE
EDCBAEDCBA

(output)
A B C D E A B C D E
E D C B A E D C B A
- - + - - - - + - -

percentage correct = 20


[[[I really appreciate any tips, not asking for an entire program just something to get us started because I dont remember any of this stuff. Its a tough nut but if you think you can crack it have at it. thanks guys]]]
Four answers:
cja
2009-05-30 11:17:47 UTC
Vectors can be very useful for this problem. See below for how I would do it.



#include

#include

#include

#include



using namespace std;



const char correct = '+';

const char incorrect = '-';

void printCharVec(const vector&);



int main(int argc, char *argv[]) {

    string fileName;



    cout << "Enter file name: ";

    getline(cin,fileName);

    ifstream file(fileName.c_str());

    if (file.is_open()) {

        string line;

        int correctCount = 0;



        // assuming 1st line is answer key, 2nd line

        // contains the answers, then EOF; would be

        // better to loop for any number of answer lines



        getline(file,line);

        vector key(line.begin(),line.end());

        vector scoreCard(key.size(),incorrect);

        getline(file,line);

        vector answers(line.begin(),line.end());

        vector::iterator i = key.begin(),

            j = answers.begin(),

            k = scoreCard.begin();

        while ((i != key.end()) && (j != answers.end())) {

            if (*i++ == *j++) {

                *k = correct;

                ++correctCount;

            }

            ++k;

        }

        printCharVec(key);

        printCharVec(answers);

        printCharVec(scoreCard);

        float grade =

            static_cast(correctCount) / key.size();

        cout << grade*100.0 << "%" << endl;

        file.close();

    } else {

        cout << fileName << " not found." << endl;

    }

    return 0;

}



void printCharVec(const vector &v) {

    vector::const_iterator i = v.begin();

    while (i != v.end()) {

        cout << *i++ << " ";

    }

    cout << endl;

}



#if 0



Sample run:



Enter file name: test.txt

A  B  C  D  E  A  B  C  D  E

E  D  C  B  A  E  D  C  B  A

 -   -   +   -   -   -   -   +   -   -

20%



#endif
anonymous
2016-10-04 15:36:49 UTC
C treats strings as an array of characters so which you will examine each and each string into 2 variables and right this moment output them to the show screen because of the fact the 1st 2 lines. Then analyze character positions one by using one and construct up a third string made up of + and - protecting a count form of the + whilst finished output the third string to the show screen observed by using the share calculation consequence.
sertrbl
2009-05-29 21:27:12 UTC
what you want to do, is read the first line and save it into it's own array, then do the same for the student's answers.

then, you print (perhaps with its own function, if thats what your teacher wants) each array on its own line.

then, you compare the two arrays piece by piece... print a + if the two match, or a - if they dont. and to get the percentage, just have a count of the correct answers and a count of total answers, and you can get a percentage.
anonymous
2009-05-29 23:32:25 UTC
include

#include

#include

#include

int main()

{

std::ifstream file("filename.txt");

std::vector vec;

std::string s;

while(std::getline(file,s))

vec.push_back(s);

std::cout << vec[0] << "\n" << vec[1] << "\n";

for(int i = 0; i < vec[0].size(); ++i)

{

if(vec[0].at(i) == vec[1].at(i) && vec[0].at(i) != ' ')

std::cout << "+";

else std::cout << "-";

}

}





Code is not tested, since dont have a compiler, and the code expects that the first and second lines are the same size,

I dont even think it does what it have to do,


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...