我正在尝试编写一个代码,它接受一个输入文件并组织它并计算一些值。但是,我的 getName 函数有问题,如果读取 -1,该函数应该通过使用 get 成员函数对输入文件中的字符求和来分配名字和姓氏以通过引用参数调用。以下是代码的错误参数:
main.cpp:7:14: error: variable or field 'getName' declared void
main.cpp:7:14: error: 'ifstream' was not declared in this scope
main.cpp:7:24: error: 'shoppingCartInput' was not declared in this scope
main.cpp:7:43: error: 'string' was not declared in this scope
main.cpp:7:51: error: 'firstname' was not declared in this scope
main.cpp:7:62: error: 'string' was not declared in this scope
main.cpp:7:70: error: 'lastname' was not declared in this scope
main.cpp:102:50: error: 'getName' was not declared in this scope
代码如下:
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cctype>
void getName(ifstream& shoppingCartInput, string& firstname, string& lastname);
using namespace std;
int main()
{
// declerations
string infile_name = "";
string shopper = "";
string firstname ="";
string lastname = "";
int quantity;
double price = 0.0, leastExpPrice = 0, mostExpPrice = 0;
string item, leastExpItem, mostExpItem;
double totalCost = 0.0;
//1) Open Your File
cout << "please enter the name of the input file:";
cin >> infile_name;
cout << "\n";
ifstream shoppingCartInput;
ofstream shoppingCartOutput;
shoppingCartInput.open(infile_name.c_str());
if(shoppingCartInput.fail( )) {
cout << "Input file opening failed.\n";
exit(1);
}
shoppingCartOutput.open("output_file.dat");
if(shoppingCartOutput.fail( )) {
cout << "Output file opening failed.\n";
exit(1);
}
//2) Read the first line
shoppingCartInput >> quantity >> price >> item;
//printing the line to the console (properly organized)
shoppingCartOutput.setf(ios::fixed);
shoppingCartOutput.setf(ios::showpoint);
shoppingCartOutput.setf(ios::left);
shoppingCartOutput.width(15);
shoppingCartOutput << item << " ";
shoppingCartOutput.setf(ios::right);
shoppingCartOutput.width(3);
shoppingCartOutput << quantity << " ";
shoppingCartOutput.setf(ios::right);
shoppingCartOutput.width(6);
shoppingCartOutput.precision(2);
shoppingCartOutput << price << " ";
shoppingCartOutput.setf(ios::right);
shoppingCartOutput.width(7);
shoppingCartOutput.precision(2);
shoppingCartOutput << (quantity * price) << endl;
//set it as the most expensive
//set it as the least expensive
leastExpPrice = price;
leastExpItem = item;
mostExpPrice = price;
mostExpItem=item;
//initialize total cost to get the cost of the first line of data
totalCost = (quantity * price);
//3) Read until the end of the file
while(!(shoppingCartInput.eof())) {
while(quantity != -1) {
shoppingCartInput >> quantity >> price >> item;
//printing the line to the console (properly organized)
shoppingCartOutput.setf(ios::fixed);
shoppingCartOutput.unsetf(ios::right);
shoppingCartOutput.setf(ios::showpoint);
shoppingCartOutput.setf(ios::left);
shoppingCartOutput.width(15);
shoppingCartOutput << item << " ";
shoppingCartOutput.setf(ios::right);
shoppingCartOutput.width(3);
shoppingCartOutput << quantity << " ";
shoppingCartOutput.setf(ios::right);
shoppingCartOutput.width(6);
shoppingCartOutput.precision(2);
shoppingCartOutput << price << " ";
shoppingCartOutput.setf(ios::right);
shoppingCartOutput.width(7);
shoppingCartOutput.precision(2);
shoppingCartOutput << (quantity * price) << endl;
if(price < leastExpPrice) {
leastExpPrice = price;
leastExpItem = item;
}
if(price > mostExpPrice) {
mostExpPrice = price;
mostExpItem=item;
}
totalCost += (quantity * price);
}
if (quantity == (-1)) {
string first = "";
string last = "";
getName(shoppingCartInput, firstname, lastname);
shopper = firstname + " " + lastname;
shoppingCartOutput << "\ncheapest item = " << leastExpItem <<
endl;
shoppingCartOutput << "most expensive item = " << mostExpIte <<
endl;
shoppingCartOutput << "total cost = " << totalCost << endl;
shoppingCartOutput.unsetf(ios::right);
shoppingCartOutput.setf(ios::left);
shoppingCartOutput << "shopper = " << shopper;
}
}
shoppingCartInput.close();
shoppingCartOutput.close();
return 0;
}
void getName(ifstream& shoppingCartInput, std::string& firstname, std::string&
lastname)
{
char letter;
do {
shoppingCartInput.get(letter);
firstname += letter - 1;
} while (letter != ',');
do {
shoppingCartInput.get(letter);
lastname += letter;
} while (letter != '\n' || letter != 'r');
}
最佳答案
对你来说,这就是 std::ifstream。或者,在所有 #includes 之后和其余代码之前添加 using namespace std;。
关于带有文件流错误的 C++ Void 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19531269/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信