【发布时间】:2014-03-10 03:17:19
【问题描述】:
我能够在我的列表中找到该单词,但我想显示找到该单词后的任何数字。我的名单上有名字,后面跟着他们的 GPA。
示例...
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,以便完整显示?
标签: c++ file while-loop