【发布时间】:2016-05-25 18:17:47
【问题描述】:
给定一个包含此数据的 .txt 文件(它可以包含任意数量的相似行):
hammer#9.95
shovel#12.35
在 C++ 中使用标志控制的 while 循环,当在导入的 .txt 文件中搜索项目名称时,应返回项目的价格(由哈希分隔)。
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
inFile.open(invoice1.txt);
char name;
char search;
int price;
ifstream inFile;
ofstream outFile;
bool found = false;
if (!inFile)
{
cout<<"File not found"<<endl;
}
outFile.open(invoice1.txt)
inFile>>name;
inFile>>price;
cout<<"Enter the name of an item to find its price: "<<endl;
cin>>search;
while (!found)
{
if (found)
found = true;
}
cout<<"The item "<<search<<" costs "<<price<<"."<<endl;
return 0;
}
【问题讨论】:
-
您可能希望首先发布您的代码,以便我们在向您描述解决方案时有一个参考框架。
-
描述很简单,所以如果你有一些具体的问题,请询问。在一个循环中,读取每一行,如果该行以您要搜索的内容开头,则输出该行 # 之后的所有内容。
-
感谢您发布家庭作业的副本,然后附上您的答案。不幸的是,您似乎已将答案提交给 stackoverflow.com 而不是您的讲师。 stackoverflow.com 不是一个对教师课程进行评分的网站,而是用于提问和获取答案的网站。如果你有一个与你的家庭作业有关的问题,你应该明确地写出它是什么,而不是仅仅发布你的代码,让每个人都猜测你的问题是什么。
标签: c++ if-statement while-loop flags