#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(){
}
我不明白我做错了什么。我已经花了几个小时看这段代码,真的需要一些帮助。我收到错误“Segmentation Fault(core dumped)”当我运行代码并尝试输入名称时。我觉得这可能很简单,但在线解释都没有帮助我。 :(
答案 0 :(得分:3)
取消引用未初始化的指针。在你打电话之前
cin.getline(stu->name,20);
您需要先使用new
student* stu = new student;
答案 1 :(得分:2)
当您尝试访问不允许的内存位置(读取或写入)区域时,通常会导致Seg错误。在下面的行中,在toAdd()方法中:
# https://matplotlib.org/api/dates_api.html
# https://matplotlib.org/examples/api/date_demo.html
import datetime as dt
import matplotlib.dates as mdates
import matplotlib.cbook as cbook
import matplotlib.dates as mpd
years = mdates.YearLocator() # every year
months = mdates.MonthLocator() # every month
yearsFmt = mdates.DateFormatter('%Y')
majorLocator = years
majorFormatter = yearsFmt #FormatStrFormatter('%d')
minorLocator = months
y1 = np.arange(100)*0.14+1
y2 = -(np.arange(100)*0.04)+12
"""neither of these indices works"""
x = pd.date_range(start='4/1/2012', periods=len(y1))
#x = map(mpd.date2num, pd.date_range(start='4/1/2012', periods=len(y1)))
fig, ax = plt.subplots()
ax.plot(x,y1)
ax.plot(x,y2)
ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(yearsFmt)
ax.xaxis.set_minor_locator(months)
datemin = x[0]
datemax = x[-1]
ax.set_xlim(datemin, datemax)
fig.autofmt_xdate()
plt.show()
stu-&gt; name,它等效于:(* stu).name试图取消引用未初始化的指针。
此外,建议使用智能指针(Wrapper用于原始(常规)指针),因为它允许您更有效地管理为该指针分配的内存。您可以选择唯一或共享智能指针。