在输出文件C++; 我现在正在编写一个C++菜单驱动程序,在输出文件中搜索某个int有点困难。我的函数是这样的 int studentId; int searchId; double examGrade1, examGrade2, examGrade3; ifstream readGrades; do { cout << "Enter the student ID: "; cin >> searchId; if (searchId < 0 || searchId > 9999) { cout << "Your student ID must be in between 0 and 9999! Try again...\n"; } } while (searchId < 0 || searchId > 9999); readGrades.open("grades.txt"); if (readGrades) { system("cls"); while (readGrades >> studentId >> examGrade1 >> examGrade2 >> examGrade3) { if (searchId == studentId) { cout << left << "Student ID\t" << "Exam 1\t" << "Exam 2\t" << "Exam 3\t" << endl; cout << "======================================" << endl; cout << left << setw(4) << studentId << "\t\t" << fixed << setprecision(2) << left << setw(5) << examGrade1 << "\t" << left << setw(5) << examGrade2 << "\t" << left << setw(5) << examGrade3 << endl; system("pause"); break; } else cout << "Entered ID not found"; } } else { cout << "Error opening file!\n"; } cout << endl; } int studentId; int-searchId; 双examGrade1、examGrade2、examGrade3; 综合招聘成绩;; 做 { cout>searchId; 如果(搜索ID9999){ cout(9999); readGrades.open(“grades.txt”); 如有需要(阅读成绩) { 系统(“cls”); 同时(readGrades>>studentId>>examGrade1>>examGrade2>>examGrade3) { if(searchId==studentId) { cout
else块位于错误的位置。您需要更新逻辑,以便仅在浏览整个文件后未找到学生ID时打印该消息。这意味着,它已在在输出文件C++; 我现在正在编写一个C++菜单驱动程序,在输出文件中搜索某个int有点困难。我的函数是这样的 int studentId; int searchId; double examGrade1, examGrade2, examGrade3; ifstream readGrades; do { cout << "Enter the student ID: "; cin >> searchId; if (searchId < 0 || searchId > 9999) { cout << "Your student ID must be in between 0 and 9999! Try again...\n"; } } while (searchId < 0 || searchId > 9999); readGrades.open("grades.txt"); if (readGrades) { system("cls"); while (readGrades >> studentId >> examGrade1 >> examGrade2 >> examGrade3) { if (searchId == studentId) { cout << left << "Student ID\t" << "Exam 1\t" << "Exam 2\t" << "Exam 3\t" << endl; cout << "======================================" << endl; cout << left << setw(4) << studentId << "\t\t" << fixed << setprecision(2) << left << setw(5) << examGrade1 << "\t" << left << setw(5) << examGrade2 << "\t" << left << setw(5) << examGrade3 << endl; system("pause"); break; } else cout << "Entered ID not found"; } } else { cout << "Error opening file!\n"; } cout << endl; } int studentId; int-searchId; 双examGrade1、examGrade2、examGrade3; 综合招聘成绩;; 做 { cout>searchId; 如果(搜索ID9999){ cout(9999); readGrades.open(“grades.txt”); 如有需要(阅读成绩) { 系统(“cls”); 同时(readGrades>>studentId>>examGrade1>>examGrade2>>examGrade3) { if(searchId==studentId) { cout,c++,C++,else块位于错误的位置。您需要更新逻辑,以便仅在浏览整个文件后未找到学生ID时打印该消息。这意味着,它已在while循环之外 if (readGrades) { bool found = false; while (readGrades >> studentId >> examGrade1 >> examGrade2 >> examGrade3) { if (searchId == studentId)
while
循环之外
if (readGrades)
{
bool found = false;
while (readGrades >> studentId >> examGrade1 >> examGrade2 >> examGrade3)
{
if (searchId == studentId)
{
cout << left
<< "Student ID\t" << "Exam 1\t" << "Exam 2\t" << "Exam 3\t" << endl;
cout << "======================================" << endl;
cout << left << setw(4) << studentId << "\t\t"
<< fixed << setprecision(2)
<< left << setw(5) << examGrade1 << "\t"
<< left << setw(5) << examGrade2 << "\t"
<< left << setw(5) << examGrade3 << endl;
found = true;
break;
}
}
if ( !found )
{
cout << "Entered ID not found";
}
}
if(readGrades)
{
bool-found=false;
同时(readGrades>>studentId>>examGrade1>>examGrade2>>examGrade3)
{
if(searchId==studentId)
{
cout-Wow,我真的尝试了这个方法,但是忘记了在while语句之外输入if(!found)…我不知道为什么我在过去的一个小时里被这个难住了。非常感谢!@ThatOneGoi,从“我的”判断过去,你可能还会犯类似的错误几次:)哦,不……哦,只要这能让我学会,我一点也不介意