【发布时间】:2009-05-30 22:57:44
【问题描述】:
#include <iostream>
#include <string>
#include <fstream>
using namespace std ;
string strWord( int index , string line)
{
int count = 0;
string word;
for ( int i = 0 ; i < line.length(); i++)
{
if ( line[i] == ' ' )
{
if ( line [i+1] != ' ')
{
count ++;
if ( count == index)
{
return word;
}
word ="";
}
}
else
{
word += line[i];
}
}
}
int main ( )
{
ifstream inFile ;
inFile.open("new.txt");
string line , id;
cout <<"Enter id : ";
cin >>id;
while(!inFile.eof() )
{
getline ( inFile , line );
if ( strWord ( 1, line ) == id )
{
cout <<strWord ( 2 , line ) <<endl;
break;
}
}
system("pause");
}
问题是:有人可以向我解释一下吗?我不明白它在做什么,我的意思是我明白了这个概念,但每一行都在做什么?
【问题讨论】:
-
请修正您的代码以在每行前使用 4 个空格。这是作业吗?
-
修复了格式。 @H4cKL0rD,请记住在将代码粘贴到 Stack Overflow 时通过缩进四个空格来格式化代码。
标签: c++