打印包含指定子字符串的所有字符串
我是C++的初学者。我正在尝试实现一个小型的C++代码,以获得乐趣。打印包含指定子字符串的所有字符串
该程序从包含我所有朋友的名字的文本文件读取其输入。
该计划的目的是返回/打印与指定的信/昵称开头的所有名称
例如
Nick
Joseph
Jack
Robert
Paul
David
如果我输入“J”的结果应该是约瑟和杰克
,如果我输入“P”或“啪”的结果应该是保罗
任何一个可以请指导我获得逻辑正确的。提前谢谢了。
问候, 帕
THS 逻辑是这样的:
read the desired prefix from user
repeat
read one line from file
if the line starts with the desired prefix print it
until there are no more lines
#include
Hi @Michael,非常感谢Logic。如何使用for循环扫描文本文件中的每一行以获取用户所需的输入?请帮忙!你可以指导我的代码请!谢谢 – user3366438
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream input;
input.open("words.txt");
char word[80];
char output;
if (input.fail())
{
cout << " the file doesnt exist" << endl;
cout << " exit program" << endl;
return 0 ;
}
while (!input.eof())
{
input >> output;
cout << output;
}
input.close();
return 0;
}
那你试试? –
你已经有了基本的逻辑集。您只需要a)提示输入b)接受输入c)搜索输入的字符串d)报告结果。 –
#include #include using namespace std; int main() { ifstream input; input.open(“words.txt”); char word [80]; 字符输出; (input.fail()) cout > output; cout
user3366438