我可以在我的列表中找到单词,但是我希望在单词找到后显示任何数字。我的名单上有名字,然后是他们的GPA。在C++中查找单词
例...
michael 2.3 Rachel 2.5 Carlos 3.0
我想补充显示位于名称后的数字的特征,一旦它的发现,我声明为INT GPA,但我不知道怎么样纳入我的计划。
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string name;
int offset;
string line;
int gpa;
ifstream read_file;
read_file.open("alpha.dat");
cout << "Please enter your name: \n";
cin >> name;
if (read_file.is_open())
{
while (!read_file.eof())
{
getline(read_file, line);
if ((offset = line.find(name)) != string::npos)
{
cout << "the word has been found: \n";
// cout << name << gpa; example to display
}
}
read_file.close();
return 0;
}
如果每一行都包含名称和GPA,为什么不能简单地打印'line'以便将其全部显示? – 0x499602D2