int与double类型使用方法及C++代码计算异常问题求助
关于C++中int和double的使用及代码计算异常问题解答
一、int和double类型的正确使用
- int类型:专门用来存储整数(正整数、负整数、0),适合计数、序号、不需要小数的数值场景。它占用内存小、运算速度快,取值范围通常是
-2^31到2^31-1(具体取决于系统)。- 示例:
int student_count = 30;用来统计学生数量,完全不需要小数。
- 示例:
- double类型:属于浮点数类型,用于存储带小数的数值,或者需要高精度的计算场景(比如货币、科学计算)。它的精度比float更高,能表示的数值范围也更大。
- 示例:
double product_price = 99.99;表示商品价格,需要保留小数。
- 示例:
- 注意要点:
- 浮点数存在精度误差,如果你需要处理精确的货币计算,建议用整数存储最小单位(比如把RM50.00存为5000分),再转换为小数显示,或者使用专门的货币处理类。
- 当int和double混合运算时,int会自动转换为double,最终结果是double类型。比如
int a=2; double b=3.5; double c = a*b;结果为7.0。
二、你的代码计算结果异常的原因及修正
你看到的2.42092e-322是未初始化的double变量产生的垃圾值,我们来拆解代码里的问题:
问题1:member变量未初始化
在calcFee()函数里,你声明了double total_fee = 0, member;,这里total_fee初始化为0,但member没有设置初始值。如果输入过程中出现错误(比如输入category时不小心输入了数字,导致cin进入失败状态,后续的cin >> type和cin >> member都不会执行),member就会是一个随机的垃圾值。用这个垃圾值乘以固定费用,就会得到类似2.42092e-322的异常结果。
问题2:缺乏输入合法性校验
你的代码没有对用户输入的category(是否是S/T)、type(是否是1/2)、member(是否是正整数)做校验。如果用户输入不符合要求的值,程序会进入分支外的逻辑,total_fee保持0,或者用错误的type/category导致计算错误,甚至触发未初始化变量的问题。
修正后的代码
#include <iostream> #include <iomanip> #include <limits> // 用于清空输入缓冲区 using namespace std; void detail(); double calcFee(); int main() { double total_fee; detail(); total_fee = calcFee(); // 设置输出格式,用固定小数位显示,避免科学计数法 cout << fixed << setprecision(2) << "The total fee is RM " << total_fee << endl; return 0; } void detail() { cout << "\t\t___________________________________________________________________________________" << endl; cout << "\t\t| Participant Category\t|\tParticipant Type\t| Fee per Member(RM) |" << endl; cout << "\t\t|_______________________|_______________________________|_________________________|" << endl; cout << "\t\t|\t S\t\t|\t\t1\t\t|\t 50.00\t |" << endl; cout << "\t\t|\t\t\t|_______________________________|_________________________|" << endl; cout << "\t\t|\t\t\t|\t\t2\t\t|\t 75.00\t |" << endl; cout << "\t\t|_______________________|_______________________________|_________________________|" << endl; cout << "\t\t|\t T\t\t|\t\t1\t\t|\t 100.00\t |" << endl; cout << "\t\t|\t\t\t|_______________________________|_________________________|" << endl; cout << "\t\t|\t\t\t|\t\t2\t\t|\t 150.00\t |" << endl; cout << "\t\t|_______________________|_______________________________|_________________________|" << endl; } double calcFee() { double total_fee = 0, member = 0; // 初始化member为0,避免垃圾值 char category; int type; // 校验category输入,必须是S/T while (true) { cout << endl << "Enter your category (S/T): "; cin >> category; if (cin.fail() || !(category == 'S' || category == 's' || category == 'T' || category == 't')) { cout << "Invalid input! Please enter S or T." << endl; cin.clear(); // 重置输入错误状态 cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清空缓冲区的错误输入 } else { break; } } // 校验type输入,必须是1/2 while (true) { cout << "Enter your type (1/2): "; cin >> type; if (cin.fail() || !(type == 1 || type == 2)) { cout << "Invalid input! Please enter 1 or 2." << endl; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); } else { break; } } // 校验member输入,必须是正整数 while (true) { cout << "Enter number of participants: "; cin >> member; if (cin.fail() || member <= 0 || member != static_cast<int>(member)) { cout << "Invalid input! Please enter a positive integer." << endl; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); } else { break; } } if(category == 'S' || category == 's') { switch(type) { case 1: total_fee = 50.00 * member; break; case 2: total_fee = 75.00 * member; break; } } else if(category == 'T' || category == 't') { switch(type) { case 1: total_fee = 100.00 * member; break; case 2: total_fee = 150.00 * member; break; } } return total_fee; }
修正说明
- 初始化了
member变量为0,彻底避免未初始化的垃圾值问题。 - 添加了输入合法性校验,确保用户输入的内容符合要求,同时处理了输入失败的情况(比如输入非数字),提升程序的健壮性。
- 在
main函数中添加了fixed << setprecision(2),让费用以两位小数的格式显示,更符合货币的阅读习惯,也避免了科学计数法的输出。
内容的提问来源于stack exchange,提问作者MN Haris
相关产品推荐
相关产品推荐

