2016-04-07 39 views
-1

Kodun çoğunu (matematik meselelerinin yanı sıra) çalışıyorum, matematiğin nerede yanlış gittiğini bilmiyorum. İlk veri seti için doğru değerleri verir, ancak ikinci set yol dışıdır. İşteDiziler ve sınıflar ile ilgili matematiksel hata

#include <iostream> 
#include <string> 
#include <iomanip> 
using namespace std; 

class TestGrader 

{ 
private: 
    char correct_ans[20]; 
    int correct = 0; 
public: 
    void setCorrect_answers(char cans[]); 
    void grade(char ans[]); 
}; 

İşte benim .cpp dosyası

#include "TestGrader.h" 

using namespace std; 

/******************************************************* 
*      setCorrect_answers    * 
* This member function receives a 20-character string * 
* and copies its information into the answers array. * 
*******************************************************/ 
void TestGrader::setCorrect_answers(char cans[]) 
{ 
    for (int i = 0; i< 20 ; i++) 
     correct_ans[i] = cans[i]; 
} 

/******************************************************* 
*      grade       * 
* The grade function receives a 20-character array * 
* holding the test taker's answers and compares each * 
* of their answers to the correct one. The function * 
* then calculates and displays the results.   * 
*******************************************************/ 
void TestGrader::grade(char ans[]) 
{ 
    cout << "User answer\t" << "Correct answer\t" << endl; 
    for (int i = 0; i< 20; i++) 
    { 
     cout << endl; 
     cout << ans[i] << "\t\t\t" << correct_ans[i] << "\t\t\t"; 
     if (ans[i] == correct_ans[i]) 
     { 
      correct++; 
     } 
     else 
      cout << "X"; 
    } 
    cout << "\n"; 
    if (correct > 15) 
     cout << "Congrats! You have passed the exam" << endl; 
    else 
     cout << "You have failed the exam. Better luck next time!" << endl; 
    cout << "Number of correct answers: " << correct << endl; 
    cout << "Number of wrong answers: " << 20 - correct << endl; 
} 

Ve burada (ana içerir) benim tester benim başlık dosyasıdır. Öğretmenim bunu sağladı, burada hiçbir şey değiştirilmemeli.

// Chapter 8 - Programming Challenge 11, Driver's License Exam 
// This program utilizes a TestGrader class to grade the written portion of a 
// driver's license exam. The class grade function compares the applicant's 
// answers, stored in one array, to the correct answers, stored in another array. 
#include <iostream> 
#include <string> 
#include <iomanip> 
using namespace std; 
#include "TestGrader.h" 

// ***************** Class tester *************** 

int main() 
{ 
    const int NUM_QUESTIONS = 20; 
    int correct = 0; 



    // Create a TestGrader object & set its key with correct answers 
    char correct_ans[] = { 'B', 'D', 'A', 'A', 'C', 
     'A', 'B', 'A', 'C', 'D', 
     'B', 'C', 'D', 'A', 'D', 
     'C', 'C', 'B', 'D', 'A' }; 

    // Array to hold test taker's answers 
    char user1_ans[20] = { 'A', 'D', 'A', 'A', 'C', 
     'A', 'B', 'A', 'C', 'D', 
     'A', 'C', 'D', 'A', 'D', 
     'A', 'C', 'B', 'D', 'A' }; 

    char user2_ans[20] = { 'A', 'D', 'D', 'A', 'C', 
     'A', 'B', 'D', 'C', 'D', 
     'A', 'C', 'D', 'C', 'C', 
     'A', 'A', 'B', 'D', 'A' }; 

    TestGrader DMVexam; 

    DMVexam.setCorrect_answers(correct_ans); 

    //------------- Grade grade User 1's answers --------- 
    cout << "------------ User # 1 -------------\n"; 
    DMVexam.grade(user1_ans); 
    //------------- Grade grade User 2's answers --------- 
    cout << "\n------------ User # 2 -------------\n"; 
    DMVexam.grade(user2_ans); 
    system("pause"); 
    return 0; 
} 
+0

Sınıflandırma öğrencileri arasında "0" değerini sıfırlamıyorsunuz. – AndyG

+0

Notlarımı nasıl sıfırlayacağımı araştırıyorum ve hiçbir şey bulamıyorum. Nereye gideceğiniz konusunda herhangi bir öneriniz var mı, çünkü nasıl sıfırlanacağını öğrettiğimizi sanmıyorum, bu yüzden nasıl öğreneceğimi öğrenmek istiyorum. –

+0

"Sıfırlama" "atama" için İngilizce idi; – AndyG

cevap

1

Sen grade() yöntemin başında 0'a correct sıfırlamak gerekir.

+0

Ayrıca, setCorrect_answers() işlevinde 'memcpy()' yi kullanmalısınız, bir döngü değil. –