Thread: Please help - Arrays and Functions

  1. #1
    Registered User madelinelise's Avatar
    Join Date
    Jul 2014
    Posts
    18

    Please help - Arrays and Functions

    This is the assignment:


    The local Driver's License Office has asked you to write a program that grades the written
    portion of the driver's license exam. The exam has 20 multiple choice questions. Here are the
    correct answers:
    1. B 6. A 11. B 16. C
    2. D 7. B 12. C 17. C
    3. A 8. A 13. D 18. D
    4. A 9. C 14. A 19. D
    5. C 10. D 15. D 20. A
    Your program should store the correct answers shown above in an array. It should ask the user
    to enter the student's answers for each of the 20 questions, and the answers should be stored in
    another array. After the student's answers have been entered, the program should display a list
    showing the question numbers of the incorrectly answered questions, the total number of
    correctly answered questions, and the total number of incorrectly answered questions. It should
    then display a message indicating whether the student passed or failed the exam. (A student must
    correctly answer 15 of the 20 questions to pass the exam.)
    Create a function named inputAnswers to obtain the 20 answers from the user.
    Create another function called checkAnswers to compare the user's answers to the correct
    answers. The function should display a list showing the question numbers of the inncorrectly
    answered questions and return the number of correct answers.
    Create 1 more function called displayResult that takes the number of correct answers as a
    parameter and determines if the user passed and displays the total number of correctly answered
    questions and the total number of incorrectly answered questions.
    Input Validation: Only accept the letters A, B, C, or D as answers. You may also allow lowercase
    letters.


    This is the problem:
    Everything runs fine aside from one issue. The very last part, accepting lower and uppercase. I can validate the input fine with the while loop, HOWEVER when the results show it does not show as correct because the array "answers" is all capitals. So I need a way for the output to compare the lower case to the array. Any ideas? You can see where I commented out. I experimented with a few things and no luck..

    This is the code:
    Code:
    // Madeline Schimenti
    // Summer 2014
    // Chapter 6 & 7 Program
    
    
    #include <iostream>
    using namespace std;
    
    
    
    
    
    
    
    
    int main()
    {
        
        // Constants and variables
        
        const int NUM_QUESTIONS = 20;
        const int MIN_CORRECT = 15;
        char answers[NUM_QUESTIONS] = {
            'B', 'D', 'A', 'A', 'C',
            'A', 'B', 'A', 'C', 'D',
            'B', 'C', 'D', 'A', 'D',
            'C', 'C', 'B', 'D', 'A'
        };
        
        
        // Array for input
        char studentAnswers[NUM_QUESTIONS];
        void inputAnswers(char[], int);
        void gradeAnswers(char[], char[], int, int);
        void displayAnswers(char[], char[], int);
    
    
        cout<< "Welcome to the Driver's License Test! \n";
        cout << "You must correctly answer at least 15\nout of the 20 questions to pass!\n";
        
        
    
    
        
        // call
        
        
        inputAnswers(answers, NUM_QUESTIONS);
        gradeAnswers(answers, studentAnswers, NUM_QUESTIONS, MIN_CORRECT);
        displayAnswers(studentAnswers, answers, NUM_QUESTIONS);
    
    
        
        return 0;
    }
    
    
    
    
    
    
    
    
    
    
    // INPUT FUNCTION
    void inputAnswers(char studentAnswers[], int NUM_QUESTIONS)
    {
        for (int index = 0; index < NUM_QUESTIONS; index++)
        {
            
            cout << "\nPlease enter your answer for question " << (index + 1) << ": ";
            cin >> studentAnswers[index];
            
            //Input validation of users answers
            while (studentAnswers[index] != 'A' && studentAnswers[index] != 'a' && studentAnswers[index] != 'B' && studentAnswers[index] != 'b' && studentAnswers[index] != 'C' && studentAnswers[index] != 'c' && studentAnswers[index] != 'D' && studentAnswers[index] != 'd')
            {
                cout << "You must enter A, B, C, or D\n";
                
                cout << "\nPlease enter your answer for question " << (index + 1) << ": ";
                cin >> studentAnswers[index];
            }
            
        }
    }
    
    
    
    
    // Function gradeAnswers
    void gradeAnswers(char CORRECTanswer[], char student_input_answer[], int NUM_QUESTIONS, int MIN_CORRECT)
    
    
    {
        
        int correctAnswers = 0;
        cout << "\nYou must have at least 15 correct to pass.";
        
        // Grade each answer
        for (int index = 0; index < NUM_QUESTIONS; index++)
        {
            
            using namespace std;
           
            
           // if (student_input_answer[index] < CORRECTanswer[index])
            // {int lower_int = CORRECTanswer[index] - 32;
            
            
            if ((CORRECTanswer[index] || lower_int) == (student_input_answer[index]))
                
            correctAnswers++;
            }
            }
            
            
            if (correctAnswers >= MIN_CORRECT)
            {
                cout << "\nCongratulations! You passed the exam!\n\n";
            }
            
            else
            {
                cout << "\nSorry, you did not pass the exam." << endl;
            }
            
        
        
        
        
        
    }
    
    
    
    
    // Display Function
    
    
    void displayAnswers(char student_input_answers[], char CORRECTanswer[], int NUM_QUESTIONS)
    {
        
        cout << "\n\nIncorrect Answers:\n"
        
        << " -----------------";
        
        for ( int index = 0; index < NUM_QUESTIONS; index++)
        {
            
            if (student_input_answers[index] != CORRECTanswer[index])
                cout << "\n" << index + 1 << "." << student_input_answers[index];
            
        }
        
        
        
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Since the answer key is in upper case, (CORRECTanswer array) the easy thing to do is to just compare a uppercase version of the student answer to it. Use the toupper function.

    Code:
     if ((CORRECTanswer[index] || lower_int) == (student_input_answer[index]))
    Comparisons like this will never work like you intend.

  3. #3
    Registered User madelinelise's Avatar
    Join Date
    Jul 2014
    Posts
    18
    Thank you, I think that probably would work.. but after ditching that if statement and adding the toupper, I realized by problem may not have been there.

    1.\250
    2.
    3.\300
    4._
    5.\377
    6.
    7.
    8.
    9.
    10.
    11.
    12.
    13.\377
    14.
    15.
    16.
    17.`
    18.\371
    19.\277

    I'm getting garbage as mystudent_input_answer output.

    I realized this started happening when started to use functions (I had to go back and change the program, after realizing I didn't follow directions) So I think it has to do with the function, I just can't see it...

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, your prototypes are technically in the wrong place.
    Code:
       void inputAnswers(char[], int);
        void gradeAnswers(char[], char[], int, int);
        void displayAnswers(char[], char[], int);
    These need to be placed above main(). Right now where they are only serves to locally declare them for main() (which is pointless because you cannot define functions inside of other functions). So technically these have no visible definition.

    Does that explain the problem? Well, I guess so. It seems like a rather odd problem to have and still not get warnings or errors from the compiler.

  5. #5
    Registered User madelinelise's Avatar
    Join Date
    Jul 2014
    Posts
    18
    That makes sense. That must have slipped by me, I fixed it but I'm still getting the same garbage..

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Post your current code.

    Jim

  7. #7
    Registered User madelinelise's Avatar
    Join Date
    Jul 2014
    Posts
    18
    Quote Originally Posted by jimblumberg View Post
    Post your current code.

    Jim

    Code:
    // Madeline Schimenti
    // Summer 2014
    // Chapter 6 & 7 Program
    
    
    #include <iostream>
    using namespace std;
    #include <cctype>
    
    
    
    
    void inputAnswers(char[], int);
    void gradeAnswers(char[], char[], int, int);
    void displayAnswers(char[], char[], int);
    
    
    int main()
    {
        
        // Constants and variables
        const int NUM_QUESTIONS = 20;
        const int MIN_CORRECT = 15;
        char answers[NUM_QUESTIONS] = {
            'B', 'D', 'A', 'A', 'C',
            'A', 'B', 'A', 'C', 'D',
            'B', 'C', 'D', 'A', 'D',
            'C', 'C', 'B', 'D', 'A'
        };
        
        char student_input_answers[NUM_QUESTIONS];
        
        char a = 'A';
        a=toupper(a);
        
        char b = 'B';
        b=toupper(b);
        
        char c = 'C';
        c=toupper(c);
        
        char d = 'D';
        d=toupper(d);
        
        
    
    
        // Array for input
        
        
        cout<< "Welcome to the Driver's License Test! \n";
        cout << "You must correctly answer at least 15\nout of the 20 questions to pass!\n";
        
        
        
        
        // call
        
        
        inputAnswers(answers, NUM_QUESTIONS);
        gradeAnswers(answers, student_input_answers, NUM_QUESTIONS, MIN_CORRECT);
        displayAnswers(student_input_answers, answers, NUM_QUESTIONS);
        
        
        return 0;
    }
    
    
    
    
    
    
    
    
    
    
    // INPUT FUNCTION
    void inputAnswers(char student_input_answers[], int NUM_QUESTIONS)
    {
        for (int index = 0; index < NUM_QUESTIONS; index++)
        {
            
            cout << "\nPlease enter your answer for question " << (index + 1) << ": ";
            cin >> student_input_answers[index];
            
            //Input validation of users answers
            while (student_input_answers[index] != 'A' && student_input_answers[index] != 'a' && student_input_answers[index] != 'B' && student_input_answers[index] != 'b' && student_input_answers[index] != 'C' && student_input_answers[index] != 'c' && student_input_answers[index] != 'D' && student_input_answers[index] != 'd')
            {
                cout << "You must enter A, B, C, or D\n";
                
                cout << "\nPlease enter your answer for question " << (index + 1) << ": ";
                cin >> student_input_answers[index];
            }
            
        }
    }
    
    
    
    
    // Function gradeAnswers
    void gradeAnswers(char CORRECTanswer[], char student_input_answers[], int NUM_QUESTIONS, int MIN_CORRECT)
    
    
    {
        
        int correctAnswers = 0;
        cout << "\nYou must have at least 15 correct to pass.";
        
        // Grade each answer
        for (int index = 0; index < NUM_QUESTIONS; index++)
        {
            
            using namespace std;
            
            
            if (student_input_answers[index] == (CORRECTanswer[index]))
                
                correctAnswers++;
            
            
            
        }
        if (correctAnswers >= MIN_CORRECT)
        {
            cout << "\nCongratulations! You passed the exam!\n\n";
        }
        
        else
        {
            cout << "\nSorry, you did not pass the exam." << endl;
        }
        
        
        
        
    }
    
    
    
    
    
    
    // Display Function
    
    
    void displayAnswers(char student_input_answers[], char CORRECTanswer[], int NUM_QUESTIONS)
    {
        int correctAnswers = 0;
        
        cout << "\n\nIncorrect Answers:\n"
        
        << " -----------------";
        
        for ( int index = 0; index < NUM_QUESTIONS; index++)
    	{
    		
    		if (student_input_answers[index] != CORRECTanswer[index])
                cout << "\n" << index + 1 << "." << student_input_answers[index];
        }
    	
        
        
        
    }

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Let's look at the following snippet:

    Code:
        char answers[NUM_QUESTIONS] = {
            'B', 'D', 'A', 'A', 'C',
            'A', 'B', 'A', 'C', 'D',
            'B', 'C', 'D', 'A', 'D',
            'C', 'C', 'B', 'D', 'A'
        };
    
        char student_input_answers[NUM_QUESTIONS];
    
    ...
    
        cout<< "Welcome to the Driver's License Test! \n";
        cout << "You must correctly answer at least 15\nout of the 20 questions to pass!\n";
    
        inputAnswers(answers, NUM_QUESTIONS);
    Why are you passing the answers array into the inputAnswers() function? This array is the test answers that you need to use to compare with the student input. If you pass this array into this function the array will be overwritten with the student's answers, is that really what you want?

    Jim

  9. #9
    Registered User madelinelise's Avatar
    Join Date
    Jul 2014
    Posts
    18
    Quote Originally Posted by jimblumberg View Post
    Let's look at the following snippet:

    Code:
        char answers[NUM_QUESTIONS] = {
            'B', 'D', 'A', 'A', 'C',
            'A', 'B', 'A', 'C', 'D',
            'B', 'C', 'D', 'A', 'D',
            'C', 'C', 'B', 'D', 'A'
        };
    
        char student_input_answers[NUM_QUESTIONS];
    
    ...
    
        cout<< "Welcome to the Driver's License Test! \n";
        cout << "You must correctly answer at least 15\nout of the 20 questions to pass!\n";
    
        inputAnswers(answers, NUM_QUESTIONS);
    Why are you passing the answers array into the inputAnswers() function? This array is the test answers that you need to use to compare with the student input. If you pass this array into this function the array will be overwritten with the student's answers, is that really what you want?

    Jim
    Thank you, that got rid of the garbage. Now I am back to my original problem. The output is checking all lowercase as wrong. I'm experimenting with the toupper, I've never used that before. I'm also going to try out a few different things.

  10. #10
    Registered User madelinelise's Avatar
    Join Date
    Jul 2014
    Posts
    18
    Please help (again).
    I'm having an issue with the counter for the correct answers, and which input to display.
    I have moved the correctAnswers = 0;, I have completely left it out, and tried a few things with that but it doesn't work.
    I either get "Congratulations you have passed" or "failed" everytime no matter what the correct total is (depending on the changes I made).
    I have tried debugging step by step by when I get to correctanswer++ I get
    "Unhandled exception at 0x003664C1 in ConsoleApplication11.exe: 0xC0000005: Access violation reading location 0x00000001."

    Here are some things I have tried..

    I have put the correctAnswers = 0 variable BEFORE the forloop, and dropped the second initialization, when I do that it will alway out "You have failed." I feel as if it is remember the 0, so it outputs that.
    Oddly enough, even though I know that the second initialization doesn't belong, it will ALWAYS execute "you have passed.." regardless of responses. Complete opposite.
    My instructor said that error is because an array is out of bounds, I can understand that. However I don't understand why I would get that error for when I go to that variable, I haven't set the correctAnswers variable to an array??


    I don't have much experience for debugging, for this I used visual studio.


    Here is my current code
    Code:
    
    
    Code:
    // Madeline Schimenti
    // Summer 2014
    // Chapter 6 & 7 Program
    
    
    
    
    #include <iostream>
    using namespace std;
    #include <cctype>
    
    
    const int MIN_CORRECT = 15;
    
    
    void inputAnswers(char[], int);
    void gradeAnswers(char[], char[], int, int);
    void displayAnswers(char[], char[], int);
    
    
    
    
    int main()
    {
        
        // Constants and variables
        const int NUM_QUESTIONS = 20;
        
        char answers[NUM_QUESTIONS] = {
            'B', 'D', 'A', 'A', 'C',
            'A', 'B', 'A', 'C', 'D',
            'B', 'C', 'D', 'A', 'D',
            'C', 'C', 'B', 'D', 'A'
        };
        char student_input_answers[NUM_QUESTIONS];
        
        
        // Array for input
        
        
        cout<< "Welcome to the Driver's License Test! \n";
        cout << "You must correctly answer at least 15\nout of the 20 questions to pass!\n";
        
        
        
        
        // Calls
        
        
        inputAnswers(student_input_answers, NUM_QUESTIONS);
        gradeAnswers(answers, student_input_answers, NUM_QUESTIONS, MIN_CORRECT);
        displayAnswers(student_input_answers, answers, NUM_QUESTIONS);
        return 0;
        
        std::cout<< "Press ENTER to continue..." << flush;
        
        cin.ignore(cin.rdbuf()->in_avail()+1);
        
    
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    // INPUT FUNCTION
    void inputAnswers(char student_input_answers[], int NUM_QUESTIONS)
    {
        for (int index = 0; index < NUM_QUESTIONS; index++)
        {
        
            
            cout << "\nPlease enter your answer for question " << (index + 1) << ": ";
            cin >> student_input_answers[index];
            
            //Input validation of users answers
            while (student_input_answers[index] != 'A' && student_input_answers[index] != 'a' && student_input_answers[index] != 'B' && student_input_answers[index] != 'b' && student_input_answers[index] != 'C' && student_input_answers[index] != 'c' && student_input_answers[index] != 'D' && student_input_answers[index] != 'd')
            {
               
                
                
                cout << "You must enter A, B, C, or D\n";
                
                cout << "\nPlease enter your answer for question " << (index + 1) << ": ";
                cin >> student_input_answers[index];
            }
            
        }
    }
    
    
    
    
    
    
    
    
    // Function gradeAnswers
    void gradeAnswers(char CORRECTanswer[], char student_input_answers[], int NUM_QUESTIONS, int MIN_CORRECT)
    
    
    
    
    {
        
     
    
    
       
        cout << "\nYou must have at least 15 correct to pass.\n";
    
    
    
    
        
        // Grade each answer
        for (int index = 0; index < NUM_QUESTIONS; index++)
          
        {
            int correctAnswers = 0;
            
            if (student_input_answers[index] == CORRECTanswer[index])
                
                    correctAnswers++;
            
        }
        
        int correctAnswers;
        
            if (correctAnswers >= MIN_CORRECT)
            {
                cout << "\nCongratulations! You passed the exam!\n\n";
            }
            
            else
            {
                cout << "\nSorry, you did not pass the exam." << endl;
                
                
                cout << "\n\nIncorrect Answers:\n"
                
                << " -----------------";
                
            }
    
    
            
        
        
    }
    
    
    
    
    
    
    
    
    // Display Function
    
    
    
    
    void displayAnswers(char student_input_answers[], char CORRECTanswer[], int NUM_QUESTIONS)
    {
       
        
        
        
        
        for ( int index = 0; index < NUM_QUESTIONS; index++)
        {
            
            for (int index = 0; index < NUM_QUESTIONS; index++) {
                
                if ('a' <= student_input_answers[index] && student_input_answers[index] <= 'd')
                    student_input_answers[index] = char(((int)student_input_answers[index]) - 32);
            }
            
            
            if (student_input_answers[index] != CORRECTanswer[index])
                cout << "\n" << index + 1 << "." << student_input_answers[index];
        }
        
        
    }

    Last edited by madelinelise; 07-18-2014 at 08:38 AM.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I would start by looking at the following warnings being generated by my compiler, fixing the problems and seeing if that solves some of the problems.

    ||=== test2, Debug ===|
    main.cpp||In function ‘void gradeAnswers(char*, char*, int, int)’:|
    main.cpp|107|warning: declaration of ‘MIN_CORRECT’ shadows a global declaration [-Wshadow]|
    main.cpp|13|warning: shadowed declaration is here [-Wshadow]|
    main.cpp||In function ‘void displayAnswers(char*, char*, int)’:|
    main.cpp|182|warning: declaration of ‘index’ shadows a previous local [-Wshadow]|
    main.cpp|179|warning: shadowed declaration is here [-Wshadow]|
    main.cpp|189|warning: name lookup of ‘index’ changed [enabled by default]|
    main.cpp|179|warning: matches this ‘index’ under ISO standard rules [enabled by default]|
    main.cpp|182|warning: matches this ‘index’ under old rules [enabled by default]|
    main.cpp||In function ‘void gradeAnswers(char*, char*, int, int)’:|
    main.cpp|138|warning: ‘correctAnswers’ is used uninitialized in this function [-Wuninitialized]|
    ||=== Build finished: 0 errors, 8 warnings (0 minutes, 1 seconds) ===|
    This one of the reasons I recommend against using global variables. Keep your variables local, and then properly pass them to and from your functions as required.


    Jim

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should also learn to use const to tell the compiler that some variables are not meant to be modified (useful when passing variables to functions).
    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.

  13. #13
    Registered User madelinelise's Avatar
    Join Date
    Jul 2014
    Posts
    18
    I got it fixed! The program is now running the way I want it. The last issue stemmed from my original issue, the program could not compare lowercase and uppercase input to see if it was correct. I had fixed this for the display, however did not include that in my gradeAnswers function. I fixed that and some other things such as moving the global variables back to main. Now it's working greatly. Thank you everyone for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-11-2013, 10:57 PM
  2. Using arrays in functions
    By Who in forum C Programming
    Replies: 2
    Last Post: 03-21-2013, 11:36 AM
  3. Using arrays in functions in C
    By ma_atie in forum C Programming
    Replies: 4
    Last Post: 11-10-2010, 10:13 AM
  4. Arrays and Functions
    By WellyJ in forum C Programming
    Replies: 6
    Last Post: 03-19-2003, 09:38 AM
  5. arrays and functions help
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 03-02-2002, 08:05 AM