为什么输完姓名后,程序直接结束运行了呢? 如果不定义姓名,可以正常运行。

#include<iostream>
#include<string>
#include <windows.h>
int MAX;
using namespace std;
//光标定位在屏幕中间
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
void locate(int x, int y)
{
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(hout, coord);
};
void Menu()
{
    locate(45, 7);
    cout << "■■■■■■■■■■■" << endl;
    locate(45, 8);
    cout << "■  " << "1.添加学生信息" << "  ■" << endl;
    locate(45, 9);
    cout << "■  " << "2.显示学生信息" << "  ■" << endl;
    locate(45, 10);
    cout << "■  " << "3.查找学生信息" << "  ■" << endl;
    locate(45, 11);
    cout << "■  " << "0.退出管理系统" << "  ■" << endl;
    locate(45, 12);
    cout << "■■■■■■■■■■■" << endl;
}
//学生结构体
struct StudentIformation
{
    std::string m_Name;//姓名
    int m_Score;  //  分数
    double m_Studentnumber;//学号
};
StudentIformation* p = new StudentIformation[MAX];
//添加学生
void addStudent(StudentIformation* p)
{
    std::string name;
    for (int i = 0; i < MAX; i++)
        
    {
        cout << "请输入第" << i + 1 << "学生的姓名:" << endl;
        string M_name;
        cin >> M_name;
        p[i].m_Name = M_name;
        ////姓名
        //
        //cin >> name;
        //p[i].m_Name = name;
        //分数
        int score;
        cout << "请输入第" << i + 1 << "学生的分数:" << endl;
        cin >> score;
        p[i].m_Score = score;
        //学号
        double number;
        cout << "请输入第" << i + 1 << "学生的学号:" << endl;
        cin >> number;
        p[i].m_Studentnumber = number;
    }
}
//显示学生
void  showStudent(StudentIformation* p)
{
    for (int i = 0; i < MAX; i++)
    {
        cout << "姓名:" << p[i].m_Name << "\t";
        cout << "成绩:" << p[i].m_Score << "\t";
        cout << "学号:" << p[i].m_Studentnumber << "\t";
    }
    system("pause");//缓冲
    system("cls");//清屏
}
//查找学生
int isEsist(StudentIformation* p, int num)
{
    for (int i = 0; i < MAX; i++)
    {
        if (p[i].m_Studentnumber == num)
            return i;
    }
    return -1;
}
void findStudent(StudentIformation* p)
{
    cout << "请输入要查找的学生的学号" << endl;
    int number;
    cin >> number;
    isEsist(p, number);
    int ret = isEsist(p, number);
    if (ret != 1)
    {
        cout << "姓名:" << p[ret].m_Name << "\t";
        cout << "学号:" << p[ret].m_Studentnumber << "\t";
        cout << "成绩:" << p[ret].m_Score << endl;
        system("pause");
    }
    else
        cout << "查无此人" << endl;
    }
    int main()
    {
        cout << "请输入要录入学生人数" << endl;
        int max;
        cin >> max;
        MAX = max;
        while (true)
        {
            Menu();
            int n;
            cin >> n;
            switch (n)
            {
            case 1:
                system("cls");
                addStudent(p);
                system("cls");
                break;
            case 2:
                system("cls");
                showStudent(p);
                system("cls");
                break;
            case 3:
                system("cls");
                findStudent(p);
                system("cls");
                break;
            case 0:
                system("pause");
                return 0;
                break;
            default:
                break;
            }
        }
        delete[]p;
    }
 

 cin >> M_name;这个代码后面加一行代码

fflush(stdio);//清空缓存。

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632