简体   繁体   English

我的程序返回错误“向量下标超出范围”。

[英]My program returns the error “vector subscript out of range.”

I'm not sure which vector is causing the error or where the issue is coming from. 我不确定哪个向量导致了错误或问题出在哪里。 I'm trying to input names and birthdates from a file and set them equal to a part in the vector. 我正在尝试从文件中输入名称和生日,并将其设置为向量中的一部分。 The file is: 该文件是:

Mark,12/21/1992
Jay,9/29/1974
Amy Lynn,3/17/2010
Bill,12/18/1985
Julie,7/10/1980
Debbie,5/21/1976
Paul,1/3/2001
Ian,2/29/1980
Josh,10/31/2003
Karen,8/24/2011

I am not even sure if my code accomplishes this because of the error. 由于错误,我什至不确定我的代码是否能做到这一点。 I tried reading more into stringstream but I did not understand how to correctly implement it. 我尝试将更多内容读入stringstream,但我不明白如何正确实现它。 The date class mentioned can be provided if necessary, but it is quite long. 如有必要,可以提供提到的日期类,但是它很长。 Any input on improving the program and why the issue is happening is greatly appreciated. 感谢您提供有关改进程序以及问题发生原因的任何意见。 Here is my code: 这是我的代码:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include "c://cpp/classes/Date.cpp"
using namespace std;

struct person {
    vector<string> name; // One name string vector
    vector<Date> birthdate; // One birthdate vector
    vector<Date> birthday; // birthday vector
};

int main() {
    string input, input2; // Two string inputs
    const int PEOPLE_NUM = 10; // Amount of people

    vector<person> People(PEOPLE_NUM); // Define vector called People with 10 positions
    string test;
    ifstream inputFile("C:/Users/Taaha/Documents/CMSC226/Project 3/Names.txt", ios::in);
    for (int i = 0; i < PEOPLE_NUM; i++) {
        getline(inputFile, input, ','); // input the line, stop when a comma
        People[i].name[i] = input; // Add input into the vector 
        getline(inputFile, input2, ',');
        People[i].birthdate[i] = input2;
        cout << i;
    }
    inputFile.close(); // close file

    Date birthday;
    for (int i = 0; i < PEOPLE_NUM; i++) {
        Date birthday(People[i].birthday[i].getDay(), People[i].birthday[i].getMonth(), Date().getYear());
        People[i].birthday[i] = birthday;
    } // Not finished yet, but turns birthdate into birthday
    return 0;
}

Thanks again :] 再次感谢 :]

When People[i].name[i] = input; People[i].name[i] = input; in the for loop, People[i].name is still a empty vector, call operator[] on it will be UB. 在for循环中, People[i].name仍然是一个空向量,在其上调用operator[]将为UB。 You might resize them in advance, or use push_back . 您可以预先resize它们的resize ,或使用push_back

for (int i = 0; i < PEOPLE_NUM; i++) {
    getline(inputFile, input, ',');
    People[i].name[i] = input;        // People[i].name is still empty here 
    getline(inputFile, input2, ',');
    People[i].birthdate[i] = input2;  // People[i].birthdate is still empty here
    cout << i;
}

You might use push_back , such as, 您可以使用push_back ,例如,

for (int i = 0; i < PEOPLE_NUM; i++) {
    getline(inputFile, input, ',');
    People[i].name.push_back(input); 
    //            ~~~~~~~~~~
    getline(inputFile, input2, ',');
    People[i].birthdate.push_back(input2);
    //                 ~~~~~~~~~~
    cout << i;
}

And it's same for another for loop. 对于另一个for循环也是如此。

for (int i = 0; i < PEOPLE_NUM; i++) {
    Date birthday(People[i].birthdate[i].getDay(), People[i].birthdate[i].getMonth(), Date().getYear());  // typo of birthdate?
    //                      ~~~~~~~~~                        ~~~~~~~~~
    People[i].birthday.push_back(birthday);
    //                 ~~~~~~~~~~
} // Not finished yet, but turns birthdate into birthday

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM