:?: I am attempting to write a C++ program that has the results of a T/F test. It takes the information from a data file and then it compares the students answers to the correct answers. I have two problems. First the data file has the first 8 characters as the Student ID(Letters and Numbers), then it has a blank space followed by the student answers. Can someone give me some ideas about how I would maybe use strcmp or would I use a method of comparing Arrays?
The output would be the student ID followed by the student answers and then a Grade.
I just need help getting started in the right direction. So far my ideas have been bad...

Recommended Answers

All 11 Replies

By sounds of things you've sort of headed in the right direction with strcmp, and your ideas may not be as bad as you think. Give us an example of what you've come up with so far and take if from there.

Remember to enclose code using code tags.

This is a start to what I had in mind:

/* Preprocessor Directives:  ********************************************************/

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;


/* Begin Main *********************************************************/

int main()
{
    char a[21]={'T','F','F','T','F','F','T','T','T','T','F','F',
                'T','F','T','F','T','F','T','\0'};
    char str[30];



    ifstream inFile;
    inFile.open ("grades.dat.c_str"); 
    if (!inFile)
    {
        cout << "Could not open file.\n";
        return 1;
    }   
    while (inFile)
    {
        cin.get(str[30]);
        strcpy(str, "grades.dat.c_str");
        if (strcmp (a[0], str[9])<0)
        {
            cout << strcmp;
        }   
        cout << str[0,1,2,3,4,5,6,7] << endl;
    }

    inFile.close();
    return 0;   
}

An example of the input file would be:

ABC54301 TFTFTFTT TFTFTFFTTFT

An example of the input file would be:

ABC54301 TFTFTFTT TFTFTFFTTFT

notice that one question would be unanswered in this example and the input starts with the Student ID and seperated only by a space.

I gather it will be safe to assume students results will always start at position 10. This is just one example of how it can be done.

char *Answer = &str [9];
int Pos = 0, Correct = 0;
 
str [8] = 0;							 // Terminator at end of Students ID
 
while (Answer [Pos]) {			 // This will look after terminating at end of string
	if (Answer [Pos] == ' ') continue; // Ignore blanks in input
	if (Answer [Pos] == a[Pos]) Correct++; // Increment on match
	Pos++;
}
 
single Ratio = (single (Answer) / single (Pos)) * 100
cout << str << " " << Answer << " " << Ratio << endl;

There are many variations of this same method and there may even be better ones. Simply, think about the application conceptually (how you would calcuate the result manually) and then code accordingly.

I'm not sure I understand the line:
single Ratio = (single (Answer) / single (Pos)) * 100

I'm not sure I understand the line:
single Ratio = (single (Answer) / single (Pos)) * 100

The method here is referred to as typecasting. In this case if you were to divide 19 by 21 lets say and leave them as integers you would get 0. Actually as integers anything other than 21 would yeild 0. You must coherse them to singles first and then the result will be .9047 multiplied by 100 you get 90.47%. I could have declared Answer & Pos as singles to begin with, but this causes unnecessary overhead for code as singles are not the natural data type for X86 CPU's

So, what would I declare Single and Ratio as?
char single
char ration
maybe...

single, double, float, int, short, long are all type specifiers so in the example

single Ratio =

The specifier single tells the complier Ratio is a single precision value. You may be accustomed to seeing

single Ratio;
 
Ratio = (single (Answer) / single (Pos)) * 100;

Most often within a statement block I'll combine the declration with statement

single, double, float, int, short, long are all type specifiers so in the example

For what language(s) is single a type?

For what language(s) is single a type?

Sure, let's have a party. Mix a little VB with C++ maybe some HTML too. Sorry klmbrt. Bad enough learning something new without having the issue confused. Single is a data type of VB. Any place I used single, replace with float.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.