【发布时间】:2017-11-12 03:26:37
【问题描述】:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
struct student
{
char name[50];
char lname[50];
int id;
float GPA;
};
void toAdd(vector<student*> *plist);
void toDelete(vector<student*>* plist);
void toPrint(vector<student*>* plist);
int main(){
vector<student*> list;
vector<student*>* plist = &list;
char input[80];
bool running = true;
while (running == true){
cout << "What would you like to do?" << endl;
cin.getline (input,80);
if(strcmp (input, "ADD") == 0){
toAdd(plist);
}
else if(strcmp (input, "DELETE") == 0){
}
else if(strcmp (input, "PRINT") == 0){
}
else{
cout << "That is not a valid response!" << endl;
}
}
}
void toAdd(vector<student*> *plist){
student* stu;
cout << "What a test!" << endl;
cout << "First Name: ";
cin.getline(stu->name,20);
cout << "Last Name: ";
cin.getline(stu->lname,20);
cout << "ID: ";
cin.getline(stu->id);
cout << "GPA: ";
cin.getline(stu->GPA);
plist->push_back(stu);
}
void toDelete(){
}
void toPrint(){
}
我不明白我做错了什么。我一直在研究这段代码几个小时,真的需要一些帮助。当我运行代码并尝试输入名称时,我收到错误“分段错误(核心转储)”。我觉得这可能真的很简单,但是网上的解释都没有帮助我。 :(
【问题讨论】:
-
你不需要那么多星星。首选字符串而不是字符数组。
-
你为什么使用指针?
-
toAdd取消引用未初始化的指针stu,因此您的程序表现出未定义的行为。 -
因为他们没有指向任何东西?
-
一:不用
plist,了解references。二:没有必要将 pointers 存储到向量中的结构中。三:了解std::string。四:了解std::getline。五:要么开始在课堂上更加专注,要么get better beginners books。
标签: c++ pointers vector segmentation-fault