getline(std::cin, temp) 跳过一行的问题

当在使用getline()之前调用cin读取输入时,getline会忽略一行。为解决此问题,可以加入std::cin.ignore()函数,清除 cin 缓冲区的剩余字符。示例代码中,通过std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ' ')确保getline能正确读取下一行。" 110226552,10295161,Apple Pay游戏退款风险揭示,"['iOS', '游戏退款', '网络安全', '消费者行为', '隐私保护']

问题: 如果在getline 函数前 调用 cin 读取变量,则 getline会忽略一行?????

贴上代码:

片段一:

#include <string>
#include <iostream>


int main(int argc, char *argv[])
{

    std::string str;
    std::cin >> str;
    std::cout << "str = " << str << std::endl;
    std::string temp;
    getline(std::cin, temp);
    std::cout << "temp = " << temp << std::endl;

    return 0;
}

操作: 从终端输入 ronnie , 回车

           程序输出如下:

           ronnie

           str = ronnie

           temp =

分析:  居然直接跳过了getline 的输入,注意:如果在getline前 调用了 std::cin 输入,则 getline会跳过一行,那么,怎么来处理这个问题?看片段二

 

片段二:

#include <string>
#include <iostream>


int main(int argc, char *argv[])
{

    std::string str;
    std::cin >> str;
    std::cout << "str = " << str << std::endl;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::string temp;
    getline(std::cin, temp);
    std::cout << "temp = " << temp << std::endl;

    return 0;
}

操作: 从终端输入 ronnie , 回车

           程序输出如下:

           ronnie

           str = ronnie

           此时没有显示  temp =

           再输入 o'sullivan,回车

           程序输出如下:

           temp = o'sullivan

分析:  比较片段一 和 片段二 发现,片段二添加了一行代码(蓝体字) ,片段二中getline不会跳过,会按照我们既定的思路运行。

总结: 如果在 getline 前,调用cin输入,则在getline 使用前,添加一行代码,即可解决问题, std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

 

           

 

#include <iostream> #include <string> #include <vector> #include <map> // 添加 map 支持 #include <fstream> // 添加文件操作支持 #include <sstream> // 添加字符串流支持 #include <algorithm> // 添加 sort 支持 using namespace std; struct Student { string id; string name; float chinese; float math; float english; float total; float average; }; vector<Student> students; map<string, Student> studentMap; // 声明在buildMap之前 // 前置声明 void buildMap(); void addStudent() { Student s; cout << "请输入学号: "; cin >> s.id; cout << "请输入姓名: "; cin >> s.name; cout << "请输入语文成绩: "; cin >> s.chinese; cout << "请输入数学成绩: "; cin >> s.math; cout << "请输入英语成绩: "; cin >> s.english; s.total = s.chinese + s.math + s.english; s.average = s.total / 3.0f; students.push_back(s); } void buildMap() { studentMap.clear(); for (const auto& s : students) { studentMap[s.id] = s; } } void searchStudent(const std::string& id) { if (studentMap.find(id) != studentMap.end()) { const auto& s = studentMap[id]; std::cout << "学号: " << s.id << ", 姓名: " << s.name << ", 语文: " << s.chinese << ", 数学: " << s.math << ", 英语: " << s.english << ", 总分: " << s.total << ", 平均分: " << s.average << std::endl; } else { std::cout << "未找到该学生的信息。" << std::endl; } } void findMaxScore(const std::string& subject) { if (students.empty()) { std::cout << "没有学生数据。" << std::endl; return; } const Student* maxStudent = &students[0]; for (const auto& s : students) { if ((subject == "语文" && s.chinese > maxStudent->chinese) || (subject == "数学" && s.math > maxStudent->math) || (subject == "英语" && s.english > maxStudent->english)) { maxStudent = &s; } } std::cout << "最高分学生信息:" << std::endl; std::cout << "学号: " << maxStudent->id << ", 姓名: " << maxStudent->name << ", 科目: " << subject << ", 分数: "; if (subject == "语文") std::cout << maxStudent->chinese; else if (subject == "数学") std::cout << maxStudent->math; else if (subject == "英语") std::cout << maxStudent->english; std::cout << std::endl; } float calculateOverallAverage() { if (students.empty()) return 0.0f; float sum = 0.0f; for (const auto& s : students) { sum += s.average; } return sum / students.size(); } bool compareStudents(const Student& a, const Student& b) { if (a.total != b.total) return a.total > b.total; return a.chinese > b.chinese; } void sortStudents() { std::sort(students.begin(), students.end(), compareStudents); } void saveToCSV(const std::string& filename) { std::ofstream file(filename); if (!file.is_open()) { std::cerr << "无法打开文件进行写入。" << std::endl; return; } file << "学号,姓名,语文,数学,英语,总分,平均分\n"; for (const auto& s : students) { file << s.id << "," << s.name << "," << s.chinese << "," << s.math << "," << s.english << "," << s.total << "," << s.average << "\n"; } file.close(); } void loadFromCSV(const std::string& filename) { std::ifstream file(filename); if (!file.is_open()) { std::cerr << "无法打开文件进行读取。" << std::endl; return; } std::string line; std::getline(file, line); // 跳过标题行 while (std::getline(file, line)) { std::stringstream ss(line); Student s; std::getline(ss, s.id, ','); std::getline(ss, s.name, ','); ss >> s.chinese; ss.ignore(); ss >> s.math; ss.ignore(); ss >> s.english; ss.ignore(); ss >> s.total; ss.ignore(); ss >> s.average; students.push_back(s); } file.close(); buildMap(); // 更新map } void analyzeScoreDistribution(const std::string& subject) { int grades[5] = {0}; // 对应五个分数段 for (const auto& s : students) { float score = 0.0f; if (subject == "语文") score = s.chinese; else if (subject == "数学") score = s.math; else if (subject == "英语") score = s.english; if (score >= 0 && score < 60) grades[0]++; else if (score < 70) grades[1]++; else if (score < 80) grades[2]++; else if (score < 90) grades[3]++; else if (score <= 100) grades[4]++; } std::cout << "成绩分布统计(" << subject << "):" << std::endl; std::cout << "不及格(0-59): " << grades[0] << "人" << std::endl; std::cout << "及格(60-69): " << grades[1] << "人" << std::endl; std::cout << "中等(70-79): " << grades[2] << "人" << std::endl; std::cout << "良好(80-89): " << grades[3] << "人" << std::endl; std::cout << "优秀(90-100): " << grades[4] << "人" << std::endl; } void showMenu() { std::cout << "\n===== 学生成绩管理系统 =====\n"; std::cout << "1. 录入学生成绩\n"; std::cout << "2. 查询学生成绩\n"; std::cout << "3. 查找单科最高分\n"; std::cout << "4. 显示所有学生成绩\n"; std::cout << "5. 排序学生成绩\n"; std::cout << "6. 保存数据到CSV文件\n"; std::cout << "7. 从CSV文件加载数据\n"; std::cout << "8. 统计成绩分布\n"; std::cout << "9. 计算整体平均分\n"; std::cout << "0. 退出系统\n"; std::cout << "=============================\n"; std::cout << "请选择操作: "; } int main() { buildMap(); // 现在可以正确调用 int choice; do { showMenu(); std::cin >> choice; switch (choice) { case 1: addStudent(); buildMap(); // 更新map break; case 2: { std::string id; std::cout << "请输入要查询的学生学号: "; std::cin >> id; searchStudent(id); break; } case 3: { std::string subject; std::cout << "请输入科目(语文/数学/英语): "; std::cin >> subject; findMaxScore(subject); break; } case 4: for (const auto& s : students) { std::cout << "学号: " << s.id << ", 姓名: " << s.name << ", 语文: " << s.chinese << ", 数学: " << s.math << ", 英语: " << s.english << ", 总分: " << s.total << ", 平均分: " << s.average << std::endl; } break; case 5: sortStudents(); std::cout << "已按总分排序。" << std::endl; break; case 6: { std::string filename; std::cout << "请输入保存文件名: "; std::cin >> filename; saveToCSV(filename); std::cout << "数据已保存到" << filename << "。" << std::endl; break; } case 7: { std::string filename; std::cout << "请输入加载文件名: "; std::cin >> filename; students.clear(); loadFromCSV(filename); std::cout << "数据已从" << filename << "加载。" << std::endl; break; } case 8: { std::string subject; std::cout << "请输入科目(语文/数学/英语): "; std::cin >> subject; analyzeScoreDistribution(subject); break; } case 9: std::cout << "所有学生的平均分为: " << calculateOverallAverage() << std::endl; break; case 0: std::cout << "感谢使用本系统,再见!" << std::endl; break; default: std::cout << "无效的选择,请重新输入。" << std::endl; } } while (choice != 0); return 0; } 为什么还是报错
06-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值