Helllppp!!

This is a discussion on Helllppp!! within the C++ Programming forums, part of the General Programming Boards category; I need some help..here is what i am trying to make and here is the code One of your professors ...

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

    Helllppp!!

    I need some help..here is what i am trying to make and here is the code


    One of your professors has asked you to write a program to grade her final exams. which consist of only 20 multiple-choice questions. Each question has one of four possible answers: A, B, C, or D. The file CorrectAnswers.txt, which is on the Student CD, contains the correct answers for all of the questions, each answer is written on a separate line. The first line contains the answer to the first question, the second line contains the answer to the second question, and so forth.

    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 charr array. The Student CD has a file named StudentAnswer.txt that you can use for testing purposes. The program should determine the number of questions that the student missed, and then display the following:

    * A list of the questions missed by the student, showing the correct answer and the incorrect answer

    provided by the student for each missed question.

    * The total number of questions missed

    * The percentage of questions answered correctly. This can be calculated as:

    Correctly Answered Questions (divided by) Total Number of Questions

    *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.


    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main ()
    {
    	const int numOfAnswers= 20;
    	const int stringSize=1;
    	char correctAnswers[numOfAnswers][stringSize];
    	char studentAnswers[numOfAnswers][stringSize]; 
    	int totalMissed=0;
    	ifstream inputFile;
    	
    	//Open the file StudentAnswers.
    	inputFile.open("CorrectAnswers.txt");
    
    	//Read the 20 answers from the file CorrectAnswers into the char array correctAnswers.
    	for (int count=0; count < numOfAnswers; count++)
    	{
    		inputFile>> correctAnswers[count][stringSize];
    
    	}
    	
    	//close the file.
    	inputFile.close();
    	
    	//Open the file StudentAnswers.
    	inputFile.open("StudentAnswers.txt");
    
    	//Read the 20 answers from the file StudentAnswers into the char array studentAnswers.
    	for (int count=0; count < numOfAnswers; count++)
    	{
    		inputFile>> studentAnswers[count][stringSize];
    	}
    
    	//close the file.
    	inputFile.close();
    	
    	return 0;
    }
    
    for(x = 0; x <  20; x++)
           if(correct[x] != answer[x])
                 wrong[x]=false;
    
    for(x = 0; x < 20; x++) {
           if(!wrong[x]) {
              cout<<x<<". wrong"<<endl;
              total++;
    }
           else
              cout<<x<<". right"<<endl;
    }
    
    cout<<total<<"wrong, "<<(20-total)<<"right"<<endl;

    I do not know what is wrong with it!

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Don't write return 0 there, instead palce it in the end of main...
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    18
    I changed that, but it still will not compile

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438

    .

    Quote Originally Posted by mrsmoss3791 View Post
    I changed that, but it still will not compile
    So what are the compiler errors? To save people having to read through it all first...???

    none of these arrays are known to the compiler,

    Code:
    for(x = 0; x <  20; x++)
           if(correct[x] != answer[x])
                 wrong[x]=false;
    
    for(x = 0; x < 20; x++) {
           if(!wrong[x]) {
              cout<<x<<". wrong"<<endl;
              total++;
    Also its is a headache names and logic wise here:
    " if the correct value referenced is not the answer then the wrong answer referenced is not wrong???" or it is wrong no longer as it is false so its is now right, in the wrong array?

    And i cannot undserstand why you are trying to use 2d arrays in the way you are, it is redundant here and plain strange the way you are doing it, also this is C++, there are other containers that may be much more suitable for task here, like perhaps std::map, to store the answers at least.
    Last edited by rogster001; 03-22-2011 at 07:45 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main ()
    {
        const int numOfAnswers= 20;
        const int stringSize=1;
        char correctAnswers[numOfAnswers][stringSize];
        char studentAnswers[numOfAnswers][stringSize]; 
        int totalMissed=0;
        ifstream inputFile;
        
        //Open the file StudentAnswers.
        inputFile.open("CorrectAnswers.txt");
    
        //Read the 20 answers from the file CorrectAnswers into the char array correctAnswers.
        for (int count=0; count < numOfAnswers; count++)
        {
            inputFile>> correctAnswers[count][stringSize];
    
        }
        
        //close the file.
        inputFile.close();
        
        //Open the file StudentAnswers.
        inputFile.open("StudentAnswers.txt");
    
        //Read the 20 answers from the file StudentAnswers into the char array studentAnswers.
        for (int count=0; count < numOfAnswers; count++)
        {
            inputFile>> studentAnswers[count][stringSize];
        }
    
        //close the file.
        inputFile.close();
        
       
    
    for(x = 0; x <  20; x++){
           if(correct[x] != answer[x])
                 wrong[x]=false;
                 }
    
    for(x = 0; x < 20; x++) {
           if(!wrong[x]) {
              cout<<x<<". wrong"<<endl;
              total++;
    }
           else{
              cout<<x<<". right"<<endl;
              }
    }
    
    cout<<total<<"wrong, "<<(20-total)<<"right"<<endl;
    return 0;
    }
    Try this.... It will guaranteed compile...
    And if not, post errors generated by your compiler here...
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    The least you could do is change the char arrays to C++ strings.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    18
    I changed it to this and now I am getting the same error message four times:

    error C2109: subscript requires array or pointer type


    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main ()
    {
        const int numOfAnswers= 20;
        const int stringSize=1;
        char correctAnswers[numOfAnswers][stringSize];
        char studentAnswers[numOfAnswers][stringSize]; 
        int totalMissed=0;
    	int x, answer, correct, wrong, total;
        ifstream inputFile;
        
        //Open the file StudentAnswers.
        inputFile.open("CorrectAnswers.txt");
    
        //Read the 20 answers from the file CorrectAnswers into the char array correctAnswers.
        for (int count=0; count < numOfAnswers; count++)
        {
            inputFile>> correctAnswers[count][stringSize];
    
        }
        
        //close the file.
        inputFile.close();
        
        //Open the file StudentAnswers.
        inputFile.open("StudentAnswers.txt");
    
        //Read the 20 answers from the file StudentAnswers into the char array studentAnswers.
        for (int count=0; count < numOfAnswers; count++)
        {
            inputFile>> studentAnswers[count][stringSize];
        }
    
        //close the file.
        inputFile.close();
        
       
    
    for(x = 0; x <  20; x++){
           if(correct[x] != answer[x])
                 wrong[x]=false;
                 }
    
    for(x = 0; x < 20; x++) {
           if(!wrong[x]) {
              cout<<x<<". wrong"<<endl;
              total++;
    }
           else{
              cout<<x<<". right"<<endl;
              }
    }
    
    cout<<total<<"wrong, "<<(20-total)<<"right"<<endl;
    return 0;
    }

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438

    int not array

    dont just slavishly copy poor code, despite the 'guarantee', as i said earlier you do not have arrays for answer wrong correct, you have simple integers, thus the compiler wants to know why u are trying to use [] with them
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    > char correctAnswers[numOfAnswers][stringSize];
    Erm, stringSize is 1, so all you have is a single character (not much of a string)
    Make stringSize a reasonable amount.

    > inputFile>> correctAnswers[count][stringSize];
    This just writes a single character off the end of the array.
    Try something like
    inputFile>> correctAnswers[count];

    Repeating Elysia's comment - use std::string !
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Post your code and errors here so that we could have a look
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Helllppp!!! I/O and array problems
    By dorky in forum C++ Programming
    Replies: 3
    Last Post: 07-02-2005, 10:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21