繁体   English   中英

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

[英]Referencing vector causes error when running program

我正在尝试制作一个程序,它将需要用户输入以形成多种形式。 我一直试图使向量(将由用户创建的表单类的对象填充)以在其他函数中可用。 当我使用地址运算符(&)时,当程序允许用户将数据输入到对象时,它会给我这个错误。 这是程序和错误的屏幕截图。

#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();

}

您正在使用以下行中的无效索引访问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);

聚苯乙烯

我不知道为什么你有这个提示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;

暂无
暂无

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

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