运行程序时引用向量导致错误

时间:2018-12-02 04:27:04

标签: c++ vector

我正在尝试制作一个程序,该程序需要用户输入才能形成多种形式。我一直试图使向量(将由用户创建的表单类的对象填充)以在其他函数中可用。当我使用地址运算符(&)时,当程序允许用户将数据输入到对象时,它会给我这个错误。 This is the screen capture of the program and the error.

#include "pch.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std;


class Form {
    public:
        string Fname;
        string Lname;
        string City;
        string Street;
        string State;
        string ZipCode;

};




void menuMain();
void menu1st(vector<Form> &Fvect);

void menu1st(vector<Form> &Fvect)
{
    int MainM;
    int n;

    cout << "NEW FORM(s)" << endl;
    cout << "Enter the number of forms you would like to make (Maximum of 5): "; cin >> n; cout << endl;
    for (int i = 0; i < n; i++)
    {
        cout << "First Name: "; cin >> Fvect[i].Fname; cout << endl;
        cout << "Last Name: "; cin >> Fvect[i].Lname; cout << endl;
        cout << "City: "; cin >> Fvect[i].City; cout << endl;
        cout << "Street: "; cin >> Fvect[i].Street; cout << endl;
        cout << "State: "; cin >> Fvect[i].State; cout << endl;
        cout << "Zip Code: "; cin >> Fvect[i].ZipCode; cout << endl;
    }

    cout << "Enter 1 to go back to main: "; cin >> MainM;
    if (MainM == 1)
    {
        menuMain();
    }
    else
    {
        cout << "Error not a correct input." << endl;
    }
}

void menu2nd()
{
    int MainM;
    //int Fnum;

    vector<Form> Fvect;
    cout << "EDIT A FORM" << endl;
    cout << Fvect[1].Fname;
    cout << "Enter the ";
    cout << "Enter 1 to go back to main: "; cin >> MainM;

    if (MainM == 1)
    {
        menuMain();
    }
    else
    {
        cout << "Error not a correct input." << endl;
    }

}

void menuMain()
{

    int Pnum;


    cout << "INFORMATION FORMATTING PROGRAM" << endl;
    cout << "1. Create new form's." << endl;
    cout << "2. Edit a form." << endl;
    cout << "3. Print forms." << endl;
    cout << "4. Erase a form." << endl;
    cout << "5. Exit Program." << endl;
    cout << "Enter the action you want to take (1-5): "; cin >> Pnum;

    vector<Form> Fvect;

    if (Pnum == 1)
    {
        menu1st(Fvect);
    }
    if (Pnum == 2)
    {
        menu2nd();
    }
    else
    {
        cout << "Error not a correct input." << endl;
    }
}

int main()
{

    menuMain();

}

1 个答案:

答案 0 :(得分:0)

您在以下几行中使用无效的索引访问Fvect

    cout << "First Name: "; cin >> Fvect[i].Fname; cout << endl;
    cout << "Last Name: "; cin >> Fvect[i].Lname; cout << endl;
    cout << "City: "; cin >> Fvect[i].City; cout << endl;
    cout << "Street: "; cin >> Fvect[i].Street; cout << endl;
    cout << "State: "; cin >> Fvect[i].State; cout << endl;
    cout << "Zip Code: "; cin >> Fvect[i].ZipCode; cout << endl;

因此,您的程序具有未定义的行为。

您需要先在std::vector中包含项目,然后才能使用数组语法从项目中访问项目。您需要做的是:

  1. 将数据读取到类型为Form的对象中。
  2. 将该对象添加到std::vector

将这些行替换为:

Form form;

cout << "First Name: "; cin >> form.Fname; cout << endl;
cout << "Last Name: "; cin >> form.Lname; cout << endl;
cout << "City: "; cin >> form.City; cout << endl;
cout << "Street: "; cin >> form.Street; cout << endl;
cout << "State: "; cin >> form.State; cout << endl;
cout << "Zip Code: "; cin >> form.ZipCode; cout << endl;

Fvect.push_back(form);

PS

我不确定为什么在这些行中有cout << endl;。您不需要它们。足够使用:

cout << "First Name: "; cin >> form.Fname;
cout << "Last Name: "; cin >> form.Lname;
cout << "City: "; cin >> form.City;
cout << "Street: "; cin >> form.Street;
cout << "State: "; cin >> form.State;
cout << "Zip Code: "; cin >> form.ZipCode;