gpt4 book ai didi

c++ - 输入负数时如何显示消息?

转载 作者:行者123 更新时间:2023-11-30 02:15:57 27 4
gpt4 key购买 nike

我遇到了一个问题,当输入负数时会显示一条错误消息。 libc++abi.dylib:以 std::bad_alloc 类型的未捕获异常终止:std::bad_alloc。有没有一种方法可以在负数输入时检测到该错误并输入一条消息:输入 0 或更大。所以,任何人都可以帮我解决这个问题。感谢您的帮助。

我试过这样做:

int students = 0;
while (students <= 0) {
cout << "How many students? ";
cin >> students;
if (students <= 0) {
cout << "Enter 0 or greater";
}
}

int testsScore = 0;
while (testsScore <= 0) {
cout << "How many tests per student? ";
cin >> testsScore;
if (testsScore <= 0) {
cout << "Enter 0 or greater";
}
}

主要代码:

#include <iostream>  // input out stream library. 
#include <iomanip> // parametic manipulators library for our table.


using namespace std;

//MARK: Structure to use the following data given in the instructions.
struct student_information {

//MARK: Student name.
string studentName;

//MARK: Student ID number.
int id;

//MARK: Pointer to an array of test scores.
int *tests;

//MARK:Average test score.
double average;

//MARK: Course grade.
char grade;

};

//MARK: The main() method should keep a list of test scores for group of students.
int main() {

// MARK: Declare variables.
int numberOfStudents;
int testScores;

//MARK: So, in our logic it should ask the user how many test scores there are to be,
// and how many students there are.
// It should then dynamically allocate an array of structures. However,
// each structure tests member should point to a dynamically allocated array
// that will hold the test scores according to our instructions.

cout << "How many students?";
cin >> numberOfStudents;
cout << endl;

cout << "How many tests per student? ";
cin >> testScores;
cout << endl;





student_information *pointerToArray = new student_information[numberOfStudents];

for (int i = 0; i < numberOfStudents; i++) {

pointerToArray[i].tests = new int[testScores];

}




//MARK: So, this is when the array has been allocated,
//the program should ask for the student ID number and all the scores for each
// every student.
for (int i = 0; i < numberOfStudents; i++) {

cout << "Student Name: ";
cin >> pointerToArray[i].studentName;
cout << endl;



cout << "ID Number: ";
cin >> pointerToArray[i].id;
cout << endl;





for (int v = 0; v < testScores; v++) {
cout << " Test # " << (v + 1) << " : ";


//MARK: Checking to see all the data is entered for each student / member.
cin >> pointerToArray[i].tests[v];
cout << endl;

// MARK: Checking if negative numbers are entered for any test scores.
while(pointerToArray[i].tests[v] < 0 || pointerToArray[i].tests[v] > 100) {

cout << "Enter 0 or greater";
cin >> pointerToArray[i].tests[v];
cout << endl;

}















}
}
//MARK: The average test score should be calculated and stored in the average member of each structure.
//The course grade should be computed on the basis of the following grading scale:
// Average Test Grade Course Grade
// 91-100 A
// 81-90 B
// 71-80 C
// 61-70 D
// 60 or below F

for (int i = 0; i < numberOfStudents; i++) {

double add = 0;

for(int x=0; x < testScores; x++)
add = add + pointerToArray[i].tests[x];
pointerToArray[i].average = add / testScores;

//MARK: The course grade will be stored in the grade member structure.
//After the data entry has been calculated.
if(pointerToArray[i].average >= 91 && pointerToArray[i].average <= 100)
pointerToArray[i].grade = 'A';

else if(pointerToArray[i].average >= 81 && pointerToArray[i].average <= 90)
pointerToArray[i].grade = 'B';

else if(pointerToArray[i].average >= 71 && pointerToArray[i].average <= 80)
pointerToArray[i].grade = 'C';

else if(pointerToArray[i].average >= 61 && pointerToArray[i].average <= 70)
pointerToArray[i].grade = 'D';

else if(pointerToArray[i].average < 60)
pointerToArray[i].grade = 'F';



//MARK: Displaying the table to list each students name, ID number,
//average test score and the course grade.

for(int i=0; i < numberOfStudents; i++)
{

cout << "Student Name: " << setw(19) << left << pointerToArray[i].studentName <<
" ID Number: " << setw(19) << pointerToArray[i].id <<
" Average test score: " << setw(15) << pointerToArray[i].average <<
" Grade: " << setw(15) << pointerToArray[i].grade << endl;

}


return 0;

}



}

最佳答案

如果为 numberOfStudentstestsScore 输入负数,则

student_information *pointerToArray = new student_information[numberOfStudents];

pointerToArray[i].tests = new int[testScores];

尝试分配大量内存。当这失败时 std::bad_allocthrown 并且 OP 不会 catch 它。因此,它在 OPs main() 之外被捕获。

new[] 中的数组大小需要一个 size_t → 一个无符号值并静默转换提供的有符号值。例如。 -1 变为 0xffffffff(32 位)或更大。

#include <iostream>
#include <iomanip>

int main(int argc, char* argv[])
{
int value = -1;
size_t size = value;
std::cout << "size: " << size << " = 0x" << std::hex << size << '\n';
try {
int *mem = new int[size];
} catch (const std::exception &error) {
std::cerr << error.what() << '\n';
}
return 0;
}

输出:

size: 18446744073709551615 = 0xffffffffffffffff
std::bad_array_new_length

Live Demo on coliru

OP 应该检查输入数字是否为 > 0


我的样本 throw std::bad_array_new_length(而不是 OP 报告的 std::bad_alloc 让我有点困惑).我对此进行了一些调查

  1. 它以这种方式精确记录(例如在 cppreference.com 中)

    Otherwise, the new-expression does not call the allocation function, and instead throws an exception of type std::bad_array_new_length or derived from it

  2. std::bad_array_new_length派生自 std::bad_alloc

关于c++ - 输入负数时如何显示消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55605064/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com