C++ 输入要在文本文件中查找的ID号
我有一个小型项目,目标是产生以下成果:C++ 输入要在文本文件中查找的ID号,c++,C++,我有一个小型项目,目标是产生以下成果: Enter ID Number: ID NUMBER Name Course 201901 Andy Jones BSN 201901 Eddie Bell BSA 2 result(s) found 但是,我得到了以下输出: Enter ID Number: 201902 ID NUMBER Name Course 201902
Enter ID Number:
ID NUMBER Name Course
201901 Andy Jones BSN
201901 Eddie Bell BSA
2 result(s) found
但是,我得到了以下输出:
Enter ID Number: 201902
ID NUMBER Name Course
201902 Colt Bridges BSCS
ID NUMBER Name Course
201902 Kenny Durant BSCE
ID NUMBER Name Course
201902 Kobe Bean BSIE
3 result(s) found
身份证号码不断循环,
当我在ID号中输入一个数字时,比如20,所有的输出都会显示,我怎样才能让程序只接受准确的ID号呢。
下面是我的代码
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <conio.h>
using namespace std;
int main() {
int offset , count = 0;
string line, IDnum, name, course, search ;
ifstream StudentList;
StudentList.open("StudentFile.txt");
cout << "Enter ID Number: ";
cin >> search;
if (StudentList.is_open()) {
while (!StudentList.eof())
{
getline(StudentList, line);
while ((offset = line.find(search, 0)) != string::npos)
{
cout << "ID NUMBER\t" << " Name\t\t" << "Course\n";
stringstream ss(line);
getline(ss, IDnum, ',');
getline(ss, name, ',');
getline(ss, course);
count++;
cout << IDnum << "\t\t" << name << "\t" << course << endl;
break;
}
}
cout << count << " result(s) found\n";
StudentList.close();
}
else {
cout << "Could not open file";
}
StudentList.close();
_getch;
}
身份证号码不断循环
由于内部while循环,它将继续循环,即它将执行n次输出,其中n是结果数或相同ID的总计数,因此我刚刚添加了if-else语句以防止列名的多个输出。因此,当它第一次被发现时,ID会输出标题,否则只输出这些列的值。
至于用
20作为输入
我想问题在于line.find函数,它似乎检测到了子字符串,因此出错了
这只是我的第一个想法,但正如其他人在评论部分提到的,也许分析这个问题更好,因为ID是唯一的值,不同的用户不能拥有相同的ID
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <conio.h>
using namespace std;
int main() {
int offset , count = 0;
string line, IDnum, name, course, search ;
ifstream StudentList;
StudentList.open("StudentFile.txt");
cout << "Enter ID Number: ";
cin >> search;
if (StudentList.is_open()) {
while (!StudentList.eof())
{
getline(StudentList, line);
while ((offset = line.find(search, 0)) != string::npos)
{
if(count == 0){
cout << "ID NUMBER\t" << " Name\t\t" << "Course\n";
stringstream ss(line);
getline(ss, IDnum, ',');
getline(ss, name, ',');
getline(ss, course);
count++;
cout << IDnum << "\t\t" << name << "\t" << course << endl;
break;
} else {
stringstream ss(line);
getline(ss, IDnum, ',');
getline(ss, name, ',');
getline(ss, course);
count++;
cout << IDnum << "\t\t" << name << "\t" << course << endl;
break;
}
}
}
cout << count << " result(s) found\n";
StudentList.close();
}
else {
cout << "Could not open file";
}
StudentList.close();
_getch;
}
这个答案假设您不关心唯一ID,只处理您提到的问题 首先,您的搜索条件是20,这些ID中的任何一个都满足该条件。 由于您的文件始终是ID、名称、课程,您可以将其附加到您的ID中,如下所示:
cin >> search;
search += ",";
cout << "ID NUMBER\t" << " Name\t\t" << "Course\n";
while (!StudentList.eof())
{
getline(StudentList, line);
while ((offset = line.find(search, 0)) != string::npos)
{
stringstream ss(line);
getline(ss, IDnum, ',');
getline(ss, name, ',');
getline(ss, course);
count++;
cout << IDnum << "\t\t" << name << "\t" << course << endl;
break;
}
}
cout << count << " result(s) found\n";
StudentList.close();
对于20的例子,它只会在以下条目中成功:
20,神户豆,BSIE
另一个原因是打印标题的行:
cout << "ID NUMBER\t" << " Name\t\t" << "Course\n";
在你的循环中。尝试将其放置在循环之前,如下所示:
cin >> search;
search += ",";
cout << "ID NUMBER\t" << " Name\t\t" << "Course\n";
while (!StudentList.eof())
{
getline(StudentList, line);
while ((offset = line.find(search, 0)) != string::npos)
{
stringstream ss(line);
getline(ss, IDnum, ',');
getline(ss, name, ',');
getline(ss, course);
count++;
cout << IDnum << "\t\t" << name << "\t" << course << endl;
break;
}
}
cout << count << " result(s) found\n";
StudentList.close();
为什么多个人有相同的ID号?这只是一个例子,但是多个人不能有相同的ID号。如果你知道ID是唯一的,那么一旦找到它就停止搜索。在本例中,这显然不起作用,但如果实际数据具有唯一ID,则可以在找到第一个ID后停止。您正在打印找到的每条记录的列标题,因为打印标题的代码位于查找匹配记录的循环中。这不是一个有用的答案,因为它没有解释为什么应该使用您发布的代码而不是海报上已经发布的代码。当你不努力提供任何能教他们的东西时,这张海报或将来阅读这个问题的其他人如何从答案中学到任何东西?