java无法输出_无法输出任何内容 . [关闭]

这是一个C++编程项目,旨在实现学生数据管理。项目要求包括使用try-catch错误处理,创建Student结构,获取用户输入,以及一系列辅助函数如StudentData、ChangeData、GetStudents和PrintStudents。在实现过程中,注意了处理cin缓冲区的问题,以确保正确读取用户输入。然而,代码似乎缺少输出功能,可能导致无法看到预期的输出。
摘要由CSDN通过智能技术生成

我正在为我的c班做最后的项目 . 说明不明确但在任何一种情况下,我编译了我的代码,但它不会输出任何东西 . 能否请您查看我的代码,让我知道我错过了什么?以下是她的要求:

该程序将包含try和catch的错误捕获 .

在每个获取用户输入的函数中抛出一个抛出,如果输入0的Major,则抛出一个字符串“Bad Major” . 输入功能在下面的2,4和7中指定 .

创建一个全局结构如下:

struct Student

{

char Name[30];

float GPA;

int Major;

};

在主要创建该结构的2个实例中 . 称他们为S1和S2 .

创建并调用名为StudentData的函数:S2 = StudentData(S1); //这是对函数的调用函数作为参数接收对结构的引用(原型设计将处理这个)并将返回对结构的引用 . 使用couts和cins从用户那里获取数据 . 出于测试目的,更改S1中的数据,使GPA为3.5,Major为2.由于您要使用cins从用户获取数据,您就是用户,只需输入这些值即可 . 在调用该函数后,S1和S2都将包含相同的数据 .

使用cout主要打印存储在结构S1和S2中的数据 .

使用指向S2的指针作为参数调用名为ChangeData的函数:ChangeData(&S2); //这是对函数的调用更改S2中的数据,使GPA为3.0,Major为1.(使用这些值进行测试......)

使用cout返回主结构存储在结构S2中的数据 .

现在在main中创建一个包含2个结构的数组 . 调用数组学生 .

创建一个函数GetStudents,它将接收数组和一个表示元素数量的int(2) . 在函数中,循环遍历数据并使用cin,cin.getline和cout语句从用户获取所有三个字段 . 组织如下:

for(...........){cout提示用户cin.getline提示名cout提示用户cin提示GPA cout提示用户cin为major cin.ignore(1); }

问题是,数字值的cin会将ENTER键保留在键盘缓冲区中,对于cin和其他数字,但没有字符串,这是正常的,因此我们必须自行删除它 . cin.ignore应该为我们处理这件事 .

从main调用函数GetStudents .

创建一个函数PrintStudents,它将获得与GetStudents相同的参数 . 它将打印出两行学生,每个学生一行 .

现在这是我的代码:

#include

#include

using namespace std;

//Global variable to determine size of name

const int Name_Size = 30;

//Structure

struct Student

{

char Name[30];

float GPA;

int Major;

};

//Function Prototypes

Student* studentData(Student &S1);

Student* changeData(Student &S2);

Student getStudent(Student array[], int size);

Student printStudents(Student array[], int size);

int main()

{

Student S1; // structure instance called S1.

Student S2; // structure instance called S2.

Student student[2]; //array of 2 structures.

S2 = *studentData(S1); //Calls the studentData function.

changeData(S2); //Calls the changeData function.

getStudent(&S1, 2); //Calls the getStudent function.

printStudents(&S1, 2); //Calls the printStudents function.

system("pause");

return 0;

}

//Function Definitions

//studentData function

Student* studentData(Student &S1)

{

cout << "Student 1" << endl;

cout << "_________" << endl;

cout << "Name: " << S1.Name << endl;

cout << "GPA: " << S1.GPA << endl;

cout << "Major: " << S1.Major << endl;

cout << endl;

if (S1.Major == 0)

{

throw "Bad Major!\n";

}

else

return &S1;

}

Student* changeData(Student &S2) // Changes the data.

{

cout << "Name: " << S2.Name << endl;

cout << "GPA: " << S2.GPA << endl;

cout << "Major: " << S2.Major << endl;

if (S2.Major == 0)

{

throw "Bad Major!\n";

}

else

return &S2;

}

Student getStudent(Student array[], int size) //Function and loop to receive the array and an int representing the number of elements(2).

{

for (int index = 0; index < size; index++)

{

cout << "Please enter students full name: ";

cin.getline(array[index].Name, 30);

cout << "Please enter students GPA: ";

cin >> array[index].GPA;

cout << "Please enter students major: ";

cin >> array[index].Major;

cin.ignore(1);

}

if (array[1].Major == 0 || array[2].Major == 0)

{

throw "Bad Major!\n";

}

else

return array[size];

}

Student printStudents(Student array[], int size)

{

for (int index = 0; index < size; index++)

{

cout << array[index].Name << " " << array[index].GPA << " " << array[index].Major << endl;

return array[size];

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值