CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 19th, 2009, 11:14 PM
    thebeast91 thebeast91 is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 5
    thebeast91 is an unknown quantity at this point (<10)
    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;
    }
    Reply With Quote
      #2    
    Old November 20th, 2009, 06:33 AM
    Skizmo's Avatar
    Skizmo Skizmo is offline
    Elite Member
     
    Join Date: Sep 2004
    Location: Holland (land of the dope)
    Posts: 2,822
    Skizmo is a name known to all (1000+) Skizmo is a name known to all (1000+) Skizmo is a name known to all (1000+) Skizmo is a name known to all (1000+) Skizmo is a name known to all (1000+) Skizmo is a name known to all (1000+) Skizmo is a name known to all (1000+) Skizmo is a name known to all (1000+) Skizmo is a name known to all (1000+)
    Re: scoring Students

    Quote:
    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.
    Reply With Quote
      #3    
    Old November 20th, 2009, 01:16 PM
    thebeast91 thebeast91 is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 5
    thebeast91 is an unknown quantity at this point (<10)
    Re: scoring Students

    actually I wrote my code and I know what I did wrong only I dont know how to change it.
    Reply With Quote
      #4    
    Old November 20th, 2009, 01:28 PM
    GCDEF GCDEF is offline
    Elite Member
    Power Poster
     
    Join Date: Nov 2003
    Posts: 8,168
    GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+)
    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.
    Reply With Quote
      #5    
    Old November 21st, 2009, 10:47 PM
    thebeast91 thebeast91 is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 5
    thebeast91 is an unknown quantity at this point (<10)
    Re: scoring Students

    I think I am doing something wrong in the while loop
    Reply With Quote
      #6    
    Old November 21st, 2009, 11:43 PM
    GCDEF GCDEF is offline
    Elite Member
    Power Poster
     
    Join Date: Nov 2003
    Posts: 8,168
    GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+)
    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
    Reply With Quote
      #7    
    Old November 22nd, 2009, 11:09 PM
    thebeast91 thebeast91 is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 5
    thebeast91 is an unknown quantity at this point (<10)
    Re: scoring Students

    Quote:
    Originally Posted by GCDEF View Post
    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;
    }
    Reply With Quote
      #8    
    Old November 23rd, 2009, 08:05 AM
    GCDEF GCDEF is offline
    Elite Member
    Power Poster
     
    Join Date: Nov 2003
    Posts: 8,168
    GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+) GCDEF has a brilliant future (2000+)
    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.
    Reply With Quote
      #9    
    Old November 24th, 2009, 01:11 AM
    thebeast91 thebeast91 is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 5
    thebeast91 is an unknown quantity at this point (<10)
    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
    Reply With Quote
      #10    
    Old November 24th, 2009, 08:28 AM
    hoxsiew hoxsiew is offline
    Senior Member
     
    Join Date: Feb 2005
    Posts: 1,476
    hoxsiew is a glorious beacon of light (400+) hoxsiew is a glorious beacon of light (400+) hoxsiew is a glorious beacon of light (400+) hoxsiew is a glorious beacon of light (400+) hoxsiew is a glorious beacon of light (400+)
    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.
    Reply With Quote
    Reply

    Bookmarks

    Tags
    arrays , strings
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:16 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.