2014-11-28 5 views
-2

저는 C++을 처음 사용하고 테스트 스코어링 시스템 프로그램을 만들려고합니다. 먼저 테스트 C++ 테스트 스코어링 시스템 프로그램 - 벡터 첨자가 범위를 벗어남 오류

  • 에서 질문의 수를 요청

    1. 그런 다음 각 질문에 대한 정확한 답변을 요청합니다.
    2. 학생들의 수를 질문하고 각 학생을 이름을 물어보고 처리 한 다음 학생의 대답을 묻는 질문을 반복합니다.
    3. 각 점수를 기록하십시오.
    4. 마지막 질문 후 학생 점수를 계산하고 "학생 '삽입 학생 이름'이 20 또는 50 % 중 10 점을 표시합니다."
    5. 모든 학생들이 채점 될 때까지 반복하십시오.

    이것은 내가 지금까지 가지고있는 것입니다. & 학생의 답을 적어 놓을 때까지 작동합니다. 그 이름이 어떤 이유로 응답을 요구할 때 어떤 이유로 첫 글자를 떨어 뜨리고 내가 입력 한 후에 "Vector subscript out of range"오류가 발생합니다. 내가 어디로 잘못 가고 있는지 모르겠다. 나는 너무 오래 꼼짝 않고 바라 보았다고 생각한다.

    #include "stdafx.h" 
    #include <iostream> 
    #include <string> 
    #include <vector> 
    
    using namespace std; 
    
    
    int questions=0; 
    char correctAns; 
    char studentAns; 
    int students=0; 
    double score = 0; 
    double final=0; 
    string studentName; 
    vector<char> stuAnsVec; // answers input by the students 
    vector<char> corrAnsVec; // correct answers 
    vector<string> studNameVec; // student names 
    void quiz(); 
    void output(); 
    void calculate(); 
    
    
    int main() 
    
    { 
        std::cout << "How many questions would you like?" << endl; 
        std::cin >> questions; // Gets the total number of questions 
    
        for (int k = 0; k < questions; k++) 
        { 
         std::cout << "What is the answer for question " << k+1 << endl; 
         std::cin >> correctAns; // Gets each answer 
         corrAnsVec.push_back (correctAns); // stores the answer 
        } 
    
        std::cout << "How many students are there?" << endl; 
        std::cin >> students; // Gets the total number of students 
    
        for (int i = 0; i < students; i++) 
        { 
    
         std::cout << "What is the name of student " << i+1<< "?"<< endl; 
         cin.ignore(); 
         std::getline(cin, studentName); 
         studNameVec.push_back (studentName); 
        } 
    
        quiz(); 
        calculate(); 
        final = score/questions; 
    
        output(); 
    
    
        return 0; 
    } 
    
    void quiz() 
    
    { // This is the function to determine the student's answers 
        for(int i = 0; i < students+1; i ++) 
         { 
          for (int k=0; k < questions; k++) 
          { 
         std::cout << "\nWhat is " << studNameVec[i] << "'s answer for question " << k+1 << endl; 
         std::cin >> studentAns; // Gets the student's answer for the question 
         stuAnsVec.push_back (studentAns); // Stores the answer in the vector 
          } 
         } 
    
    } 
    
    
    void calculate() 
    { // This is meant to actually calculate student scores 
    
        for (int i = 0; i < students+1; i++) 
        { 
         if (stuAnsVec[i] == corrAnsVec[i]) 
    
          score = (score + 1); 
         else 
          score = --score; 
        } 
    } 
    
    void output() 
    { // My output function 
    
        for (int i = 0; i < students; i++) 
        { 
         std::cout << studNameVec[i] << " got " << score << " right out of " << questions << " total." << endl; 
         std::cout << "The percentage for " << studNameVec[i] << " is "; 
         if (final = 1) 
          std::cout<< "100 \n"; 
         else 
         std::cout << final << endl; 
    
         system ("pause"); 
        } 
    } 
    
  • 답변

    1
    for(int i = 0; i < students+1; i ++) 
    

    이 잘못

    for(int i = 0; i < students; i ++) 
    
    0

    cin.ignore로 변경을() 학생 이름의 첫 글자를 먹고있다. 그리고 BufBills가 말한 내용.

    +0

    cin.ignore()를 제거하면 학생 1의 이름에 값을 입력 할 수 없습니다. "학생 1의 이름은 무엇입니까? 학생 2의 이름은 무엇입니까?" – kay1212

    관련 문제