gpt4 book ai didi

c++ - 运行程序时引用 vector 导致错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:42:21 24 4
gpt4 key购买 nike

我正在尝试制作一个程序,该程序将接受用户输入以制作多种形式。我坚持尝试让 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();

}

最佳答案

您正在访问 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;

关于c++ - 运行程序时引用 vector 导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53577397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com