Thread: Help! I cannot figure out what I'm doing wrong here!

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    1

    Help! I cannot figure out what I'm doing wrong here!

    For this, it is supposed to read in the answers entered by the user and compare it to the array containing the correct answers. It reads them in, but I can't get it to compare in the QuestonsCorrect function. Any help is greatly appreciated!


    Code:
    #include <iostream>                 // include standard I/O library
    #include <string>                   // include C++ string class handling
    #include <iomanip>					// include input/output function
    #include <cmath>					// include C++ math function
    #include <cstdlib>					// include standard utilities
    using namespace std;
    
    /* ========================================================================== */
    /* GLOBAL CONSTANTS */
    const int NUMBER_OF_ANSWERS = 20;
    const int CORRECT_TO_PASS = 15;
    int numCorrect = 0;
    int numIncorrect = 0;
    
    
    /* ========================================================================== */
    /* FUNCTION PROTOTYPES */
    void PrintIntro();
    int AskForScores();
    void QuestionsCorrect();
    void PassExam ();
    void QuestionList();
    void PrintClosing();
    /* ========================================================================== */
    
    typedef char Answers [NUMBER_OF_ANSWERS];
    
    int main ()							//begin main function
    {
    	int correctAnswer [NUMBER_OF_ANSWERS];
    	//Answers correct[] = {"BDAACABACDBCDCCBDA"};
    	char correct[] = {"BDAACABACDBCDCCBDA"};
    
    	PrintIntro();
    	AskForScores();
    	PassExam();
    	QuestionList();
    	PrintClosing();
    
    }
    
    
    void PrintIntro()
    {
    /* print an overall output title for program; print overall title for output
       set width of each line*/
    	cout << setw (47) << "====================================="  <<  endl;
    	cout << setw (40) << "DMV Driver's License Exam" << endl;
    	cout << setw (47) << "====================================="  <<  endl 
    		<< endl;
    	
    }
    
    int AskForScores()
    {
    int i;
    char answerChoices[NUMBER_OF_ANSWERS];
    
    for (i=0; i<NUMBER_OF_ANSWERS; i++)
    {
    		cout << "Please enter answer " << i+1 << " in CAPITAL LETTERS." << endl;
    		cin >> answerChoices[i];
    
    		while (answerChoices[i] < 'A' || answerChoices[i] > 'D') 
    		{
    			cout << "Invalid input. Please re-enter answer" << i+1 << " in CAPITAL LETTERS." << endl;
    			cin >> answerChoices[i];
    		}
    	
    
    }
    cout << answerChoices << endl;
    return 0;
    }
    int QuestionsCorrect (char answerChoices[], char i, char correct[])
    {
    	
    for(int i=0; i < NUMBER_OF_ANSWERS; i++)
    { 
                if ( (answerChoices,correct,i) == false )
    			{ 
                     cout << "Answer " << i + 1 << " is incorrect."; 
                }  
        } 
        return 0; 
    }
    
    void PassExam()
    {
    
    	cout << " You answered " << numCorrect << " questions correctly and " 
    		<< numIncorrect << " questions incorrectly." << endl << endl;
    
    
    	if (numCorrect >= CORRECT_TO_PASS)
    		{
    			cout << "Congratulations! You passed the driving exam!" << endl << endl;
    		}
    	else 
    	
    			cout << "We're sorry, but you have not passed the driving exam." << endl << endl;
    
    
    }
    
    void QuestionList()
    {
    
    	cout << "Please review the questions below so that you are able to pass the exam next time." << endl << endl;
    
    }
    
    void PrintClosing()
    {
    	cout << "This is the end of the DMV Driving Exam. Goodbye!!" << endl;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    This doesn't do what you might expect
    Code:
                if ( (answerChoices,correct,i) == false )
    (answerChoices,correct,i) is a list of expressions that evaluates to the rightmost expression.
    That means that you compare the loop counter i to false.
    Kurt

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    Here's a simple C example, should work just fine but not really tested.

    Code:
    #include <stdio.h>
    
    /* You had this set to 20 but I believe 
     * you only had 18 answers actually set
     */
    #define NUM_ANSWERS 18
    #define MIN_CORRECT 15
    
    void print_title()
    {
    	printf("    =====================================\n");
    	printf("          DMV Driver's License Exam      \n");
    	printf("    =====================================\n");
    }
    
    int tally(char * key, char * answers)
    {
    	int correct = 0;
    	int i;
    	for(i = 0; i < NUM_ANSWERS; i++)
    		if(key[i] == answers[i])
    			correct++;
    
    	return correct;
    }
    
    int main(void)
    {
    	int i = 0;
    	int e = 0;
    	int correct;
    	char buf[4];
    	char ans[NUM_ANSWERS];
    	char key[] = "BDAACABACDBCDCCBDA";
    	char * p;
    
    	print_title();
    	
    	while(i++ < NUM_ANSWERS)	
    	{
    		if(e == 0)
    			printf("Please enter answer %d in CAPITAL LETTERS.\n>", i);
    		p = fgets(buf, 3, stdin);
    		
    		if(p[1] != '\n')
    		{
    			printf("Invalid input - too many characters. Please re-enter answer %d in CAPITAL LETTERS.\n>", i);
    			e = 1;
    			i--;
    			while(p[0] != '\n') /* I'm on linux... not a portable check */
    				p[0] = getchar();
    			
    			continue;
    
    		} else if(*p < 'A' || *p > 'D') {
    			printf("Invalid input. Please re-enter answer %d in CAPITAL LETTERS.\n>", i);
    			e = 1;
    			i--;
    			continue;
    		}	
    		e = 0;
    		ans[i-1] = *p;
    	}
    	correct = tally(key, ans);
    
    	if(correct >= MIN_CORRECT)
    		printf("\nCongratulations! You passed the driving exam!\n");
    	else
    		printf("\nWe're sorry, but you have not passed the driving exam.\n");
    
    	printf("You answered %d questions correctly and %d incorrectly.\n", correct, NUM_ANSWERS-correct);
    	printf("\nThis is the end of the DMV Driving Exam. Goodbye!!\n");
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Any suggestions on whats wrong with this C Program?
    By thephreak6 in forum C Programming
    Replies: 3
    Last Post: 10-19-2002, 07:50 PM
  2. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM
  3. please help, i can't figure out what's wrong
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2002, 09:13 PM
  4. What is wrong here?????
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2002, 10:51 AM
  5. What did i do wrong? Code inside
    By The Rookie in forum C Programming
    Replies: 2
    Last Post: 05-18-2002, 08:14 AM