gpt4 book ai didi

c++ - 使用并行数组的初学者编程学生

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:54:18 28 4
gpt4 key购买 nike

对所有程序员都非常尊重的初学者程序员。我的头发不见了,有时我因为试图解决这些问题而感到筋疲力尽。 Anyhoot 当前任务让我从我已经完成的 .txt 文件中读取数据。执行计算并输出到屏幕。读入的数据比我应该写入输出文件的变量更多。所以我已经读入了数据,现在我必须将 tripNumber 和 FinalCost 读入两个不同的数组,然后将 Reverse 中的数据写入文件。我已经掌握了其中的大部分内容,但仍停留在一些应该在我的代码中清楚的地方。意识到每个人都有自己的问题,这不是一个悲伤的故事。我每周工作 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.

}

输出到文件后,循环结束。这意味着您将多次输出该文件,这是不对的。

其次,将行程编号和最终成本写入数组非常简单。您只需要一个额外的变量来记录您添加了多少次行程。我将其称为“numberOfTrips”。像这样

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;
}

最后,当你将数据写入文件时,你应该只写入添加到并行数组的条目数,而不是整个数组。所以最后一个循环应该使用前一个循环中的“numberOfTrips”变量。像这样

//for loops to output data to file
for(counter = 0; counter < numberOfTrips; counter++)
{
fileSave<< nbrOfTrip[counter]<<endl;
fileSave<< totalCost[counter]<<endl;
}

关于c++ - 使用并行数组的初学者编程学生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19917267/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com