如何解决在我的 switch case 之后,程序在用户输入后退出
我一直在完成一项任务,在编写代码并运行它之后,我注意到在我输入第一个用户输入后,我的程序退出。
我四处询问并发现这是由于我的结构/指针上的内存分配造成的,并尝试在我的 int main 上初始化 new 但它仍然退出而不运行整个程序。我是这方面的完全初学者,会寻求帮助。
我的代码发布在下面,感谢任何帮助,非常感谢!
//struct declaration
struct Students {
std::string givenname,lastName;
int studentType,NumSize,labUnits,lecUnits,totalUnits;
char paymentMode;
long long courseCode;
};
//Students studs[100]; //global struct declaration
//function prototype
void
infoStudent();
void
studentCode();
void
computation();
//global variables
bool inputerror;
Students stud1;
Students* studP;
void
infoStudent() {
studP = new Students[size]; //allocates memory
delete [] studP;
std::cout << "\nInput data for Student #" << ctr << ": " << std::endl;
std::cout << "Student's Last Name: ";
getline( std::cin,studP[ctr].lastName );
std::cout << "Student's Given Name: ";
getline( std::cin,studP[ctr].givenname );
do {
std::cout << "Student Number: ";
std::cin >> studP[ctr].courseCode;
inputerror = std::cin.fail();
if ( inputerror || ( studP[ctr].courseCode < 2000000000 || studP[ctr].courseCode > 2999999999 ) ) {
std::cout << std::endl << "Please enter a proper Student Number." << std::endl << std::endl;
std::cin.clear();
std::cin.ignore( 5,'\n' );
}
} while ( inputerror || ( studP[ctr].courseCode < 2000000000 || studP[ctr].courseCode > 2999999999 ) );
do {
do {
std::cout << "Number of Laboratory Units: ";
std::cin >> studP[ctr].labUnits;
inputerror = std::cin.fail();
if ( inputerror || ( studP[ctr].labUnits < 0 || studP[ctr].labUnits > 15 ) ) {
std::cout << std::endl << "Please enter a proper amount of Laboratory Units." << std::endl << std::endl;
std::cin.clear();
std::cin.ignore( 5,'\n' );
}
} while ( inputerror || ( studP[ctr].labUnits < 0 || studP[ctr].labUnits > 15 ) );
do {
std::cout << "Number of Lecture Units: ";
std::cin >> studP[ctr].lecUnits;
inputerror = std::cin.fail();
if ( inputerror || ( studP[ctr].lecUnits < 0 || studP[ctr].lecUnits > 15 ) ) {
std::cout << std::endl << "Please enter a proper amount of Lecture Units." << std::endl << std::endl;
std::cin.clear();
std::cin.ignore( 5,'\n' );
}
} while ( inputerror || ( studP[ctr].lecUnits < 0 || studP[ctr].lecUnits > 15 ) );
studP[ctr].totalUnits = studP[ctr].labUnits + studP[ctr].lecUnits;
if ( studP[ctr].totalUnits <= 15 ) {
std::cout << std::endl << "You have " << studP[ctr].totalUnits << " Total Units.\n";
} else if ( studP[ctr].totalUnits > 15 ) {
std::cout << std::endl
<< "Please enter a proper amount of Laboratory and Lecture Units.\n(Must not exceed over 15 units.)\n"
<< std::endl;
}
} while ( studP[ctr].totalUnits > 15 );
do {
std::cout << std::endl << "Mode of Payment ([F/f] Full or [I/i] Installment): ";
std::cin >> (studP[ctr].paymentMode );;
studP[ctr].paymentMode = std::tolower( studP[ctr].paymentMode );
if ( studP[ctr].paymentMode == 'f' ) {
std::cout << "Your chosen Mode of Payment is Full" << std::endl;
} else if ( studP[ctr].paymentMode == 'i' ) {
std::cout << "Your chosen Mode of Payment is Installment" << std::endl;
}
if ( studP[ctr].paymentMode != 'f' && studP[ctr].paymentMode != 'i' ) {
std::cout << std::endl << "Please enter a proper mode of payment." << std::endl;
}
} while ( studP[ctr].paymentMode != 'f' && studP[ctr].paymentMode != 'i' );
}
解决方法
您正在分配 studP
,然后立即释放内存。之后,您有一个悬空指针,然后您正在使用这个无效指针(全部在函数 infoStudent
中)。
我不得不提到你有一个 switch-case 并且你在每种情况下都调用相同的函数而没有参数,所以这是毫无意义的。
我还应该提到,你的代码中有几个幻数,你不应该那样使用全局变量。还要确保初始化所有变量。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。