2011-11-10 11 views
0

私のコードで何文字を数えようとしています。あなたが私のファイルにいくつかのyを持っているのを見ることができるので。カウンターはまだゼロです。私は何を間違えているのですか?文字列を "y"と比較できません

inline ifstream& read(ifstream& is, string f_name) 
{ 
is.close(); 
is.clear(); 
is.open(f_name.c_str()); 
return is; 
} 

//main function 
int main() 
{ 
string f_name=("dcl"); 
ifstream readfile; 
read(readfile, f_name); 
string temp, word; 
istringstream istring; 
int counter=0, total=0; 
while(getline(readfile,temp)) 
{ 
    istring.str(temp); 
    while(istring>>word) 
    { 
     cout<<word<<"_"<<endl; 
     if(word=="y") 
      ++counter; 
     if(word=="y") 
      ++total; 
    } 
    istring.clear(); 
} 
cout<<counter<<" "<<total<<" "<<endl; 
return 0; 
} 

(psの私は本当にここで問題。 "(単語==場合は、" Y ")は、" 私はカウントするn個すぎだ、彼らの両方があるべきではありません知っている)と私の出力が

Date_ 
9am_ 
Break_ 
Lunch_ 
Walk_ 
Break_ 
---------------------------------------------------_ 
11/09/11_ 
y_ 
y_ 
y_ 
y_ 
y_ 
_ 
0 0 

です私の入力コード:

//check list for daily activities 
#include <iostream> 
#include <string> 
#include <fstream> 
#include <stdexcept> 
using namespace std; 
//inline function 
inline ofstream& write(ofstream& input, string& f_name); //write to a file function 
inline void tofile(ofstream& w2file, const string& s); //output format 
//main function 
int main() 
{ 
string f_name="dcl"; 
cout<<"date: "<<ends; 
string temp; 
cin>>temp; 
ofstream checklist; 
write(checklist,f_name); 
tofile(checklist,temp); 
cout<<"start the day at 9am? [y/n]"<<ends; 
cin>>temp; 
tofile(checklist,temp); 
cout<<"a break every 1 hr? [y/n]: "<<ends; 
cin>>temp; 
tofile(checklist,temp); 
cout<<"lunch at noon? [y/n]: "<<ends; 
cin>>temp; 
tofile(checklist,temp); 
cout<<"20 min walk? [y/n]: "<<ends; 
cin>>temp; 
tofile(checklist,temp); 
cout<<"a break every 1 hr in the afternoon? [y/n]: "<<ends; 
cin>>temp; 
tofile(checklist,temp); 
checklist<<endl; 
cout<<endl; 
return 0; 
} 

inline ofstream& write(ofstream& input, string& f_name) 
{ 
input.close(); 
input.clear(); 
input.open(f_name.c_str(),ofstream::app); 
return input; 
} 

inline void tofile(ofstream& w2file, const string& s) 
{ 
w2file<<s<<"\t"<<ends; 
} 
+0

あなたのコードは私のために働きます(該当するヘッダを追加した後)。テキストファイルを作成し、プログラムをコンパイルして実行し、結果を「5 5」にするために、あなたの出力から_を取り除いた。 – wollw

+0

私はプログラムをコンパイル可能にし、ファイルに "y"行を入力すると、最終出力として "5 5"が期待通りに得られます。 – aschepler

+0

多分それは私の文字エンコーディングですか?私がgeditでdclを開いたとき。それに数字が入った奇妙な箱がいくつかあります。 – ihm

答えて

1

問題は、プログラム内のtofile関数がDCLファイルを生成することにあります。各行を書くときは、基本的には各項目をタブ文字で区切り、その後にヌル文字を入力して<< "\t" << ends;とし、後で行を読んでistringstreamだが、istringstreamを空白で区切って使います。したがって、"\t"から" "に変更し、<< endsを削除する必要があります。

inline void tofile(ofstream& w2file, const string& s) 
{ 
    w2file << s << " "; 
} 
+0

あなたが必要とするのは次の場合だけです:if(0 == word.compare( "y"))++ counter、++ total; –

+1

'=='演算子は 'string'と' char * 'に対してうまく動作します。 –

+0

は依然として0 0を得る。それは私がLinuxでコンパイルしたからですか?それをtxtファイルとして保存する必要はありますか?私はちょうどtouch'ed dcl(ファイル名) – ihm

2

単語内部の「Y」の出現箇所をカウントし、合計カウンタにそれを蓄積するstd::count(word.begin(), word.end(), 'y')を試してみてください。

0

私が追加した場合、適切なが含まれており、ソースの先頭に「名前空間stdを使用して」およびg ++でコンパイルし、その後、「DCL」ファイルの内容として、あなたの出力に対してそれを実行し、それが "印刷します5 5 "最後に - つまり、あなたのコードは正常に動作します。あなたの入力は何らかの形で非常に特殊なものでなければなりません。

+0

私の入力コードが更新されました。 – ihm