无法将字符串与“y”进行比较
我正在尝试计算我的代码中有多少个 y 。因为你可以看到我的文件中确实有一些 y。但计数器仍然为零。我做错了什么? (ps我知道它们都是“if(word==”y“)”我也想计算n。这里不重要)
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;
}
并且我的输出是
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;
}
I am trying to count how many y's in my code. Since you can see that I do have some y's in my file. but the counters are still zero. what am I doing wrong? (p.s I know they both are "if(word=="y")" I want to count n's too. shouldn't really matter here)
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;
}
and my output is
Date_
9am_
Break_
Lunch_
Walk_
Break_
---------------------------------------------------_
11/09/11_
y_
y_
y_
y_
y_
_
0 0
my input codes:
//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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用 std::count(word.begin(), word.end(), 'y') 来计算单词中 'y' 的出现次数,并将其累加到总计数器中。
Try
std::count(word.begin(), word.end(), 'y')
to count the occurences of 'y' inside a word and accumulate that to a total counter.问题出在程序中生成 DCL 文件的
tofile
函数。编写每一行时,您基本上是用制表符分隔每个字段,然后通过执行<< 来分隔空字符。 “\t”<<结束;
然后读取该行并将其与istringstream
一起使用,但istringstream
用空格字符分隔。因此,您需要将"\t"
更改为" "
并删除<<结束
。The problem is with your
tofile
function in your program to generate the DCL file. When writing each line you're basically separating each field by a tab character, followed by a null character by doing<< "\t" << ends;
and then later reading the line and using it withistringstream
butistringstream
separates by space characters. So, you need change"\t"
to" "
and drop the<< ends
.如果我将适当的包含和“using namespace std”添加到源代码的开头并使用 g++ 进行编译,然后针对输出作为文件“dcl”的内容运行它,它会在末尾打印“5 5” - - 即,您的代码工作正常。你的输入一定是非常奇特的......
If I add the appropriate includes and a "using namespace std" to the beginning of your source and compile with g++, then run it against your output as the contents of the file "dcl", it prints "5 5" at the end -- i.e., your code works fine. Your input must be extremely peculiar somehow...