Thread: Helppp!!

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    18

    Helppp!!

    The assignment is to write a program that reads the contents of the correctanswers.txt file into a one dimensional char array and then reads the contents of another file, containing a student's answers, into a second char array. The program should determine the number of questions that the student missed, and then display the following: (1) a list of questions missed by the students showing the correct answers and the incorrect answers provided by the student for each missed (2) the total number of questions missed (3) the percentage of the questions answered correctly (4) if the percentage of correctly answered questions is 70% or greater, the program should indicate that the student passed the exam. Otherwise, it should indicate that the student failed the exam.


    This is what I have so far for the assignment, I just started. However, I am not if there is a way to compare two text files line by line. This is where I am stuck at. So may someone please help me?!!?


    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    using namespace std;
    
    int main ()
    {
    	const int SIZE=21;
    	char correctAnswers[SIZE]; 
    	char studentAnswers[SIZE];
        ifstream inFile;
    	ifstream newInFile;
        
        //Open the file CorrectAnswers.
        inFile.open("CorrectAnswers.txt");
    
        //Read the 20 answers from the file CorrectAnswers
    	    for (int count=0; count < SIZE; count++)
        {
            inFile >> correctAnswers;
    	}
    	
    	//close the file.
        inFile.close();
    		
    	
    	//Open the file StudentAnswers.
        newInFile.open("StudentAnswers.txt");
    
        //Read the 20 answers from the file StudentAnswers
    		for (int count=0; count < SIZE; count++)
        {
            newInFile >> studentAnswers;
        }
    
    	//close the file.
        newInFile.close();
    
    return 0;
    }

  2. #2
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Code:
    	    for (int count=0; count < SIZE; count++)
        {
            inFile >> correctAnswers;
    	}
    It looks like you misunderstand how arrays work. In particular, char arrays are interpreted as strings, and so this will put the next block of non-whitespace characters into correctAnswers along with a terminating null character. In the next iteration of the loop, this process will simply be repeated and the previous data will be overwritten. If you want to input char by char, you have to specify the index of correctAnswers each time, e.g.

    Code:
    for (int count=0; count < SIZE; ++count)
    {
        inFile >> correctAnswers[count];
    }
    By the way, could you show us what CorrectAnswers.txt looks like? It makes a difference to what your code should be.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    18
    correct answers.txt
    A
    D
    B
    C
    A
    A
    D
    B
    D
    C
    A
    D
    B
    A
    C
    C
    D
    B
    C
    A

    student answers.txt
    A
    D
    B
    D
    A
    A
    D
    B
    D
    C
    A
    A
    B
    A
    C
    C
    C
    B
    C
    A


    And my question is how to compare two text files line by line?

    And why would I want the process to be repeated and the previous data overwritten?

  4. #4
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    When I said

    In particular, char arrays are interpreted as strings, and so this will put the next block of non-whitespace characters into correctAnswers along with a terminating null character. In the next iteration of the loop, this process will simply be repeated and the previous data will be overwritten.
    I was describing what your program currently does, not what you should be doing.

    If you want to read a file line by line, you can use istream::getline() (or better yet use std::string instead of char[] and use the string version of getline()), but you shouldn't need that here. If you use the extraction operator ">>" with a char, that char will be filled with the next non-whitespace char in the stream, so when reading answers.txt, it'll read 'A' then 'D' then 'B' then 'C' etc., which should suit this program perfectly.

    If you put the indexes on the arrays in those loops like I suggested, i.e.:

    Code:
    inFile >> correctAnswers[count];
    ...
    newInFile >> studentAnswers[count];
    Then correctAnswers will store 'A', 'D', 'B', 'C', etc., and studentAnswers will store 'A', 'D', 'B', 'D', etc. . You can then go through each array at the same time and compare them using another for loop.

    By the way, was the use of those char arrays your design or is it required by your homework/assignment? Because there's really no need to store these values and compare them later, they could easily be compared at the same time they are read in from the streams.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    18
    Well the assignment does ask "to write a program that reads the contents of the correctanswers.txt file into a one dimensional char array and then reads the contents of another file, containing a student's answers, into a second char array. So, i assumed that I needed the char arrays.

    And I used if/else statements to compare each reponse, like this:

    Code:
    if (correctAnswers[0]==studentAnswers[0])
    			cout << "Right\n";
    		else 
    			cout << "Wrong\n";
    then i have 20 if/else statements going from 0-19. It worked, but I do not think this is the best way to go about comparing each response.

    actually, instead of using all of those if/else statements, i brought it down to one using the for loop

    Code:
    for (count = 0; count < SIZE; count++)
    	{
    		if (correctAnswers[count]==studentAnswers[count])
    			cout << "Right\n";
    		else 
    			cout << "Wrong\n";
    	}
    Last edited by mrsmoss3791; 04-07-2011 at 08:20 AM.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Since your assignment specifically states to use two char arrays then that is what you need to use.

    When you go to printout the results why not use a loop? Then you can compare the correct answer to the student's answer inside this loop.

    Jim

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    18
    what do you mean? I mean the for loop I used does tell me whether the student is right or wrong, but I do not know how to show the correct response for each wrong answer or how to show the number of missed questions

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Since you edited you post , you already have the loop. Now inside th loop use a variable to keep track of each wrong answer. And instead of just printing out "Right" or "Wrong" print out the correct answer along with the students answer.

    Jim

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    18
    I am not sure how to do that for each specific wrong response...

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    What exactly don't you understand?

    Jim

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    18
    Well so far I changed it to this..

    Code:
    for (count = 0; count < SIZE; count++)
    	{
    		if (correctAnswers[count]==studentAnswers[count])
    
    			cout << "Right";
    		
    		else 
    			cout << "Wrong! The correct answer is " << correctAnswers[count];
    			
    		cout << endl;
    	}
    BTW. I really appreciate you helping me out with this assignment. Thanks a lot!

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    And your specific question is? (I'm not very good at guessing.)

    Jim

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    18
    Now I changed it to this

    Code:
    for (count = 0; count < SIZE; count++)
    	{
    		if (correctAnswers[count]==studentAnswers[count])
    
    			cout << "Right! You chose " << studentAnswers[count];
    		
    		else 
    			cout << "Wrong. You chose " << studentAnswers[count]; 
    			cout <<". The correct answer is " << correctAnswers[count];
    			
    		cout << endl;
    	}
    Sorry. Now I am asking how do I get the program to calculate all the wrong responses?

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You need to add braces {} to your if/else statement, otherwise the second line in your else will always be executed. If you need to keep track of how many wrong answers the student has, this is the place to do it.

    Until you become more familiar with the language I suggest that you always use braces in all if, else, or any loop constructs.

    Jim

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    18
    So I am done and everything works fine except for one little part. For some reason my program does not properly calculate the percent the student received on the exam..this is my code:

    all identifiers are already declared in my code and everything compiles, but for some reason it always give 0% for the grade.

    Code:
    // Calculates and shows the percent the student received
    	percent = (correct/20) * 100;
    	cout << "You got a " << percent;
    	cout << "%\n\n";

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.get() Helppp!!!
    By yukapuka in forum C++ Programming
    Replies: 6
    Last Post: 05-03-2008, 11:57 AM
  2. helppp in bank databse
    By neha007 in forum C++ Programming
    Replies: 1
    Last Post: 06-02-2007, 09:43 AM
  3. Helppp
    By GQgirl in forum C Programming
    Replies: 6
    Last Post: 11-23-2006, 05:03 PM
  4. Modem programming helppp amateur!!
    By Siagal in forum Windows Programming
    Replies: 4
    Last Post: 10-12-2001, 03:02 AM
  5. Modem programming helppp amateur!!
    By Siagal in forum C Programming
    Replies: 4
    Last Post: 10-10-2001, 05:12 AM