使用并行阵列的初学者编程学生
beginner programming student with parallel arrays
初学者程序员,对所有程序员都非常尊重。我的头发不见了,有时我会因为试图解决这些问题而感到筋疲力尽。Anyhoot 当前作业让我从我完成.txt文件中读取数据。执行计算并输出到屏幕。读入数据的变量比我应该写入输出文件的变量多。所以我读入了数据,现在我必须将tripNumber和FinalCost读入两个不同的数组,然后将反向数据写入文件。我已经掌握了大部分内容,但卡在了几个地方,这些地方在我的代码中应该很清楚。意识到每个人都有自己的问题,这不是一个悲伤的故事。我每周工作60 +小时,并试图获得学位。感谢您的任何帮助或建议,使这项复杂的技能更容易理解。
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
//create two arrays
const int ARRAY_SIZE = 100; //array size of 100 elements
ifstream fileIn; //create file object
ofstream fileSave; //create new output file
fileIn.open("TripInput.txt"); //read in file
//Variables to hold data from the file
int tripNbr = 0;
double fuelCost = 0;
double fuelTotal = 0;
double wasteDisp = 0;
double misCost = 0;
int counter = 0;
int nbrOfTrip[ARRAY_SIZE];
double totalCost[ARRAY_SIZE];
for(counter = 0; counter < ARRAY_SIZE; counter++)
{
nbrOfTrip[counter] = 0;
totalCost[counter] = 0;
}
cout<<"Welcome to My Space Travel Company"<<endl;
cout<<endl;
cout<<"Trip No"<<setw(10)<<"Fuel"<<setw(10)<<"Waste"<<setw(10)<<"Misc"<<setw(15)
<<"Discount Fuel"<<setw(15)<<"Final Cost"<<endl;
if(fileIn.fail())//test to see if file opened
{
cout<<"File did not open."<<endl;
}
while(fileIn>>tripNbr>>fuelCost>>wasteDisp>>misCost) //while loop to read in data from file
{
fuelTotal = fuelCost - (fuelCost * .10);
double finalCost = fuelTotal + wasteDisp + misCost;
cout<<tripNbr<<setprecision(2)<<fixed<<setw(14)<<fuelCost<<setw(10)<<wasteDisp
<<setw(10)<<misCost<<setw(15)<<fuelTotal<<setw(15)<<finalCost<<endl;
//Write trip number and final cost to the 2 parallel arrays...not sure how to
//to do this.
//open output file
fileSave.open("TripCost.txt");
//for loops to output data to file
for(counter = 0; counter < ARRAY_SIZE; counter++)
{
fileSave<< nbrOfTrip[counter]<<endl;
fileSave<< totalCost[counter]<<endl;
}
}
system("Pause");
return 0;
}
我想到了几件事。首先,您的"while循环以从文件中读取数据"在错误的位置结束。它应该是
while(fileIn>>tripNbr>>fuelCost>>wasteDisp>>misCost) //while loop to read in data from file
{
fuelTotal = fuelCost - (fuelCost * .10);
double finalCost = fuelTotal + wasteDisp + misCost;
cout<<tripNbr<<setprecision(2)<<fixed<<setw(14)<<fuelCost<<setw(10)<<wasteDisp
<<setw(10)<<misCost<<setw(15)<<fuelTotal<<setw(15)<<finalCost<<endl;
//Write trip number and final cost to the 2 parallel arrays...not sure how to
//to do this.
}
输出到文件后,此循环结束。这意味着您将多次输出文件,这是不正确的。
其次,将行程号和最终成本写入数组非常简单。您只需要一个额外的变量来记录您添加的行程数。我称之为"旅行次数"。喜欢这个
int numberOfTrips = 0;
while(fileIn>>tripNbr>>fuelCost>>wasteDisp>>misCost) //while loop to read in data from file
{
fuelTotal = fuelCost - (fuelCost * .10);
double finalCost = fuelTotal + wasteDisp + misCost;
cout<<tripNbr<<setprecision(2)<<fixed<<setw(14)<<fuelCost<<setw(10)<<wasteDisp
<<setw(10)<<misCost<<setw(15)<<fuelTotal<<setw(15)<<finalCost<<endl;
//Write trip number and final cost to the 2 parallel arrays
nbrOfTrip[numberOfTrips] = tripNbr;
totalCost[numberOfTrips] = finalCost;
++numberOfTrips;
}
最后,当您将数据写入文件时,您应该只写入添加到并行数组的条目数,而不是整个数组。因此,最后一个循环应该使用前一个循环中的"trips数量"变量。喜欢这个
//for loops to output data to file
for(counter = 0; counter < numberOfTrips; counter++)
{
fileSave<< nbrOfTrip[counter]<<endl;
fileSave<< totalCost[counter]<<endl;
}
相关文章:
- 有一个打印语句的函数是一种糟糕的编程实践吗
- C++错误消息*成员参考.**初学者*
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 嵌套for循环C++的问题(初学者)
- 模板元编程:如何将参数包组合成新的参数包
- Qt Q串行端口未编程设备未关闭
- 我是 C++ 的初学者,我想知道如何在 2D 矢量中获取重复值
- 模板元编程 - 尝试实现维度分析
- 我是编程新手
- 初学者如何使用标准::any_of?
- C++编程从外部文本文件定义数组大小
- 了解算法的性能差异(如果以不同的编程语言实现)
- 使用 Gtkmm 以编程方式选择 Gtk::TextView 中的文本
- 模板编程初学者问题
- 代码块上的编译器标志出现问题.编程初学者
- 使用并行阵列的初学者编程学生
- C++练习车辆程序问题 - 初学者编程
- 你会如何解释一个外行人或编程初学者,面向对象方法的大胆点 - 安全
- 面向初学者的c++非阻塞编程
- 初学者尝试通过游戏编程学习继承