comparing arrays/string????

April 9, 2011 at 13:29:02
Specs: Windows XP
I am trying to print out a student's score (0 or 1) from two different files. An answer file. And a student's answer file. I have no problem printing out the student number and his answers but it doesn't print out the 0 or 1 from student's score. only prints out hex. I need to compare the answer file and answers' from the student file, if they match it prints a 1 if not then 0. answers are in either T or F. studentanswers, studentscore, and studentnumber are character arrray's. Any help????? here is my code so far

void display_data(results data_results[],int size)
cout << "Display Data - nothing yet";

int i,j,last;
int max; //globalize/pass at end of project
char temp1[11];
//int result;

//char temp2[11]; //compares anwer key and student answer


ifstream answer_file;"Answers");

answer_file >> temp1;


cout << "Student Number Student Answer Student Score" << endl;
cout << "\t\t" << temp1 << end;
for(i = 1; i <= 10; i++)
cout << data_results[i].student_number
<< "\t" << data_results[i].student_answers
<< "\t" << data_results[i].student_score
//<< "\t" <<result
<< endl;

See More: comparing arrays/string????

Report •

April 10, 2011 at 11:16:57
It *looks* like you'd want to compare data_results[i].student_answers to temp1 inside of your loop that's printing out student data. use strcmp; if either string is a member of the string class, use string_obj.c_str()

if(strcmp(data_results[i].student_answers, temp1) == 0) {
// they're equal, do something

or if a member of the string class

if(strcmp(data_results[i].student_answers.c_str(), temp1) == 0) {
// they're equal, do something

Note, strcmp is case sensitive; that is, "the" and "THE" aren't the same to the function... if case is an issue, convert both strings to either upper or lower case before calling strcmp.


Report •
Related Solutions

Ask Question