我想编写一个程序来查找用户输入的单词,我认为我的解决方案是正确的,但是当我运行它时,该程序在控制台中什么也没显示 有人可以解决吗?
$distance = "st_distance_sphere(Point({$lat}, {$lon}), Point({$lat}, {$lon})) * 0.000621371192";
$query = "SELECT table.*, {$distance} as distance
WHERE {$distance} < 10
AND postcode IN ( 'XX12 3XX', 'XX12 3XX', 'XX12 3XX' )
ORDER BY distance asc
LIMIT 100";
答案 0 :(得分:2)
没有输出的主要原因是
if (maybedel == del) // <<< this will *never* be true
cout << maybedel; // will never run
由于比较数组中的“字符串”需要std::strcmp(maybedel,del) == 0
的帮助会更好。
更新:
另一种攻击方法是避免原始循环,并利用STL对您有利。这是一个更强大的解决方案:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
using namespace std;
int main() {
cout << "enter sentence :\n";
string sen;
if (!getline(cin, sen)) throw std::runtime_error("Unable to read sentence");
cout << "which word do you want to delete ? ";
string del;
if (!(cin >> del)) throw std::runtime_error("Unable to read delete word");
istringstream stream_sen(sen);
vector<string> arrayofkeptwords;
remove_copy_if(istream_iterator<string>(stream_sen), istream_iterator<string>(),
back_inserter(arrayofkeptwords),
[&del](auto const &maybedel) { return maybedel == del; });
copy(begin(arrayofkeptwords), end(arrayofkeptwords),
ostream_iterator<string>(cout, " "));
cout << '\n';
}
答案 1 :(得分:2)
除非您的代码以('')开头,否则if(sen [i] =='')行,即代码的第12行,阻止代码进入代码块! 我稍微修改了代码,现在可以正常工作了。
char sen[200], del[200], maybedel[200];
cout << "enter sentence :" << endl;
cin.getline(sen, 200);
cout << "which word do you want to delete ?" << endl;
cin.getline(del, 200);
int len = strlen(sen);
int t = 0;
for(int i = 0; i <= len; i++) {
if(sen[i] == ' ' || sen[i] == '\0') {
maybedel[t] = '\0';
t = 0;
if(strcmp(del,maybedel)==0) {
cout << maybedel << endl;
}
}
else
{
maybedel[t] = sen[i];
t++;
}
}