CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2009
    Posts
    6

    scoring Students

    I need to write a program using arrays and strings.Here is the question.
    A class of students takes a 20 question multiple-choice exam;each question has 5 choices(a,b,c,d,e)only one of them are correct.In "tests.dat"Each record of which consists of a student id, followed by a blank, followed by students 20 responses. In "answers.dat" which consists of a single record, namely the correct answers to the exam.Then It needs to produce a grade summary report like this.If the answer is correct it should put a "*" by the correct answer


    student-id number correct
    1231231212312 12
    1233424325435 25
    .... ...






    question A B C D E
    1 5 1 13* 3 1
    2 4 7* 5 12 7
    .
    .
    ...



    this is what i got so far.Thank you


    #include <fstream>
    #include <iostream>
    #include <string>
    #include <cstdlib>

    using namespace std;
    int question[20][5];
    int main()
    {
    ifstream fanswers, fcorrect_keys;

    char correct_keys[21];
    char student_id[100][12], answers[100][21];
    char id_buf[12], answers_buf[21];

    int student_count = 0;

    fcorrect_keys.open("Answers.dat");
    while(!fcorrect_keys.eof()) {
    fcorrect_keys >> answers_buf;
    strcpy(correct_keys, answers_buf);
    }
    // cout << correct_keys << endl;
    fcorrect_keys.close();
    fanswers.open("Tests.dat");
    while(!fanswers.eof()) {
    fanswers >> id_buf >> answers_buf;
    strcpy(student_id[student_count], id_buf);
    strcpy(answers[student_count],answers_buf);
    student_count++;
    }
    // for(int i=0; i < student_count;i++)
    // cout << student_id[i] << " " << answers[i] << endl;
    fanswers.close();// close the file
    cout << " student_id number_correct" << endl << endl;
    for(int i = 0; i < student_count; i++) {
    int correct_count = 0;
    for(int j = 0; j < 20; j++) {
    if (answers [i][j] == correct_keys[j])
    correct_count++;
    question[j][answers[i][j] - 'A']++;
    }
    printf("%11.11s %2d\n", student_id[i],correct_count);
    }
    cout << endl << endl << endl << "Number of students taking exam : " << student_count << endl;

    printf("\nquestion A B C D E\n");

    for(int k = 0; k < 20 ; k++) {
    printf(" %2d ",1+k);
    for(int l=0;l < 5; l++)
    printf(" %2d%c ", question[k][l], (l == (correct_keys[k] - 'A') ? '*' : ' '));
    printf("\n");
    }
    return 0;
    }

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: scoring Students

    I need to write a program using arrays and strings.Here is the question.
    No, that's you assignment... you didn't ask a question. Maybe you should read this first.

  3. #3
    Join Date
    Nov 2009
    Posts
    6

    Re: scoring Students

    actually I wrote my code and I know what I did wrong only I dont know how to change it.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: scoring Students

    Quote Originally Posted by thebeast91 View Post
    actually I wrote my code and I know what I did wrong only I dont know how to change it.
    Why don't you tell us then so we don't have to guess.

    Also, please edit your code and use code tags and proper indentation. Most of us won't even bother looking at unformatted code.

  5. #5
    Join Date
    Nov 2009
    Posts
    6

    Re: scoring Students

    I think I am doing something wrong in the while loop

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: scoring Students

    Quote Originally Posted by thebeast91 View Post
    I think I am doing something wrong in the while loop
    http://www.codeguru.com/forum/showpo...35&postcount=4

  7. #7
    Join Date
    Nov 2009
    Posts
    6

    Re: scoring Students

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    char question[20][5];
    int main()
    {
    	ofstream fanswers, fcorrect_keys;
    	
    	char correct_keys[21];
    	char student_id[12], answers[5][20];
    	char id_buf[12], answers_buf[20];
    	
    	char student_count = 0;
    	
    	fcorrect_keys.open("Answers.dat");
    	while(!fcorrect_keys.eof())  {		
    		fcorrect_keys >> answers_buf;
    		strcpy(correct_keys, answers_buf);
    	}
    //	cout << correct_keys << endl;
    	fcorrect_keys.close();
    	fanswers.open("Tests.dat");
    	while(!fanswers.eof())  {		
    		fanswers >> id_buf >> answers_buf;
    		strcpy(student_id[student_count], id_buf);
    		strcpy(answers[student_count],answers_buf);
    		student_count++;  		
       	}
    //	for(int i=0; i < student_count;i++)
    //		cout << student_id[i] << "  " << answers[i] << endl;
     	 fanswers.close();// close the file  
    	cout << " student_id      number_correct" << endl << endl;
    	for(int i = 0; i < student_count; i++)  {
    		int correct_count = 0;
    		for(int j = 0; j < 20; j++)  { 
    			if (answers [i][j] == correct_keys[j])
    				correct_count++;
    			question[j][answers[i][j] - 'A']++;
    		}
    		printf("%11.11s         %2d\n", student_id[i],correct_count);
    	}	
    	cout << endl << endl << endl << "Number of students taking exam : " << student_count << endl; 
    	
    	printf("\nquestion     A    B    C    D    E\n");
    	
    	for(int k = 0; k < 20 ; k++)  {
    		printf("   %2d      ",1+k);
    		for(int l=0;l < 5; l++)
    			printf(" %2d%c ", question[k][l], (l == (correct_keys[k] - 'A') ? '*' : ' '));
    		printf("\n");
    	}
    	return 0;
    }

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: scoring Students

    It's amazing how hard some people make it to help them.

    You've yet to ask a question or say what's happening that you need help with.

  9. #9
    Join Date
    Nov 2009
    Posts
    6

    Re: scoring Students

    Ok .. I am doing the first while loop wrong but I dont know what is wrong I am just asking for a little help for the while loop you dont have to tell me the code..I just need some help with it..And I appreciate your help

  10. #10
    Join Date
    Feb 2005
    Posts
    2,160

    Re: scoring Students

    Is "answers.dat" supposed to contain many lines of text? I ask because you have two variables, answers_buf and correct_keys that you read from a file, then write to the variables in a loop until the end of the file, but all this is going to give you is the very last line (you write over it with each iteration of the loop.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured