hello-

total c++ n00b here and i'm having problems with an assignment where we're supposed to compare an array of exam answers to an array containg a predetermined answer key and then indicate whether the exam taker has passed or failed. my program compiles just fine, but i'm not getting any output when the program runs. i'm working in visual studio 2010 and have created a win32 console application for my project. when the program runs, the console window pops up but only displays a blinking cursor and nothing else. please see the following code and attached screenshot, thanks in advance!

#include "TestGrader.h"
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

void TestGrader::setKey(char key[])
{
    for(int i = 0; i < NUM_QUESTIONS; i++)
    {
        char key[] = {"BDAACABACDBCDADCCBDA"};
    }
}
void TestGrader::grade(char answers[20])
{
    int num_correct = 0;
    int num_incorrect = 0;
    int questions_wrong[NUM_QUESTIONS] = {0};

    for(int i = 0; i < NUM_QUESTIONS; i++)
    {
        if(!( questions_wrong[NUM_QUESTIONS] == answers[20]))
        {
            num_incorrect++;
        }
        else
        {
            num_correct++;
        }
    }
    if(num_correct >= 15)
        cout << "Applicant Passed" << endl;
    else
    {
        cout << "Applicant Failed" << endl;
    }
    cout << "Correct" << num_correct << endl;
    cout << "Incorrect" << num_incorrect << endl;

}

a1f71f7772a60a888e7fdc3ea802c38d

Edited 3 Years Ago by buug

okay i just fgured out that the command prompt is simply waiting for input. once 20 chars are entered it spits out the results.

Edited 3 Years Ago by buug

In the future, you'd do better to show us all the code. This code, for example, doesn't contain the main function, so we have no idea where your code starts executing and we don't even know if your code even calls the functions you've shown.

Edited 3 Years Ago by Moschops

thanks for the heads up. can you explain or point to any examples here on how to post all the code when it includes multiple .cpp files and a header file? i'm inclinded to post something like below, but am not sure if this conforms to the usual practices here. i'm also now having a problem with receiving input from the randomly generated array of answers. the goal here is to have the program run without any user input and just grade the randomly generated answer array. i can start a new article, if that's preffered.

A) TestGrader.h

#pragma once
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int NUM_QUESTIONS = 20;
class TestGrader
{
private:
    char answers[20];
public:
    void setKey(char []);
    void grade(char []);

};

B) asn4_tester.cpp

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std; 
#include "testGrader.h"
// ***************** Client program that uses the class ***************

int main()
{ 

const int NUM_QUESTIONS = 20;
string name; // Test taker's name
char testTakerAnswers[20]; // Array to hold test taker's answers
TestGrader DMVexam; // Create a TestGrader object & set its key 
DMVexam.setKey("BDAACABACDBCDADCCBDA");
/**********************************************
Generate a response 20 times. Each response is
generated as follows:
- generate a random number between 0-4
- Add 65 to it. 65 is the numeric value for 'A'
- This generates a character 'A', 'B', 'C', or 'D'
**********************************************/
for (int i = 0; i < NUM_QUESTIONS; i++)
{
    testTakerAnswers[i] = rand()% 4 + 65;
    cin >> testTakerAnswers[i];
    bool valid = false;
    while(!(valid))
    {
        if(testTakerAnswers[i] == 'A'&& testTakerAnswers[i] == 'B' && testTakerAnswers[i] == 'C' && testTakerAnswers[i] == 'D')
        {
            cout << "Only A, B, C, or D may be entered" << endl;
            testTakerAnswers[i] = rand()% 4 + 65;
            cin >> testTakerAnswers[i];
        }
        else
        {
            valid = true;
        }
    }
}

// Call class function to grade the exam
cout << "Results \n\n";
DMVexam.grade(testTakerAnswers);
system("pause");
 return 0;
 }

and C) asn4.cpp

#include "TestGrader.h"
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

void TestGrader::setKey(char key[])
{
    for(int i = 0; i < NUM_QUESTIONS; i++)
    {
        char key[] = {"BDAACABACDBCDADCCBDA"};
    }
}
void TestGrader::grade(char answers[20])
{
    int num_correct = 0;
    int num_incorrect = 0;
    int questions_wrong[NUM_QUESTIONS] = {0};

    for(int i = 0; i < NUM_QUESTIONS; i++)
    {
        if(!( questions_wrong[NUM_QUESTIONS] == answers[20]))
        {
            num_incorrect++;
        }
        else
        {
            num_correct++;
        }
    }
    if(num_correct >= 15)
        cout << "Applicant Passed" << endl;
    else
    {
        cout << "Applicant Failed" << endl;
    }
    cout << "Correct" << num_correct << endl;
    cout << "Incorrect" << num_incorrect << endl;

}

Edited 3 Years Ago by buug

This article has been dead for over six months. Start a new discussion instead.