【问题标题】:How do I get my output file to stop printing zeros?如何让我的输出文件停止打印零?
【发布时间】:2021-09-18 13:58:59
【问题描述】:

我的所有数据都正确打印到“nursery_run_results.txt”文件中,除了在我的表格打印之后它后面跟着许多只包含零的行。我认为这是因为我们的 CAPACITY 变量设置为 100(我不允许更改)。当我测试 CAPACITY 设置为 19 时,它打印得很完美,但显然我无法做到这一点。我应该更改或添加什么?


//Include the following
#include <iostream>
#include <string>
#include <fstream>    //you must include this library if you wish to do file i/o
#include <iomanip>
using namespace std;

/*********************************************************
//Following is the declaration of a order record
**********************************************************/

const int CAPACITY = 100;//declaring capacity as a constant
class order_record
{
public:
   string pname;
   string cname;
   double plant_cost;
   double quantity;
   double purchase_tax;
   double net_cost;
   double tax_rate;
   double discount;
   double total_cost;
   double STR[CAPACITY];
};

//Prototypes for your functions: input, output, process, and count_inventory will go here

void input(order_record STR[],int &count); //sample prototype for input
//you add other prototypes
void process(order_record STR[], const int count);
void output(const order_record STR[], const int &count);
double count_inventory (const order_record STR[], const int count);


//Function Implementations will go here

///*************************************************************************************
//Name:  input
//Precondition: The state what is true before the function is called.
//Postcondition: State what is true after the function has executed.
//Description:    Describe what the function does (purpose).

void input(order_record STR[],int &count)
{

    //open the input file stream
    ifstream in;
    in.open("nursery_stock-3.txt");
    
    
    
        while (!in.eof() && count < CAPACITY)
        {
            in >> STR[count].pname >>
                  STR[count].cname >>
                  STR[count].plant_cost >>
                  STR[count].quantity;
            
            count ++;
        }
   
    //close the input file stream
    in.close();
}



//*************************************************************************************


void process(order_record STR[], const int count)
{
    //for loop
    for (int count = 0; count < CAPACITY; count++)
    {
        
        //calculate net cost
        STR[count].net_cost = STR[count].quantity * STR[count].plant_cost;
        
        //calculate tax rates
        //dade = 6.5, broward = 6.0, palm = 7.0
        if (STR[count].cname == "dade")
            STR[count].tax_rate = .065;
        
        else if (STR[count].cname == "broward")
            STR[count].tax_rate = .06;
        
        else if (STR[count].cname == "palm")
            STR[count].tax_rate = .07;
        
        //calculate the quantity to determine discount
        /*quan 0 dis 0
        /quan 1<=5 dis 1
        /quan 6<=11 dis 3
        /quan 12<=20 dis 5
        /quan 21<=50 dis 8
        /quan >50 dis 12
        */
        if (STR[count].quantity <= 0)
            STR[count].discount = 0;
        
        else if (STR[count].quantity >= 1 && STR[count].quantity <= 5)
            STR[count].discount = .01 * STR[count].net_cost;
        
        else if (STR[count].quantity >= 6 && STR[count].quantity <= 11)
            STR[count].discount = .03 * STR[count].net_cost;
        
        else if (STR[count].quantity >= 12 && STR[count].quantity <= 20)
            STR[count].discount = .05 * STR[count].net_cost;
        
        else if (STR[count].quantity >= 21 && STR[count].quantity <= 50)
            STR[count].discount = .08 * STR[count].net_cost;
        
        else if (STR[count].quantity > 50)
            STR[count].discount = .12 * STR[count].net_cost;
        
        
        //calculate tax on purchase
        STR[count].purchase_tax = STR[count].net_cost * STR[count].tax_rate;
        
        
        //calculate total cost (net cost w/ tax and discount)
        STR[count].total_cost = STR[count].net_cost + STR[count].purchase_tax - STR[count].discount;
        
    }
    
}

//*************************************************************************************

///*************************************************************************************
//Name:  output
//Precondition: State what is true before the function is called.
//Postcondition: State what is true after the function has executed.
//Description:    Describe what the function does (purpose).

//declare output file stream (ofstream out)
    // open "..." add for loop;
    // close file

void output(const order_record STR[], const int &count)
{
    //declare output file stream "out"
    ofstream out;
    
    //open file
    out.open("nursery_run_results.txt");
    
    //apply magic formula
    out.setf(ios::showpoint);
    out.precision(2);
    out.setf(ios::fixed);
    
    
    //print to file
    for (int count = 0; count < CAPACITY; count++)
    {
    out << left << setw(15) << STR[count].pname
        << left << setw(10) << STR[count].cname
        << right << setw(8) << STR[count].plant_cost
        << right << setw(8) << STR[count].quantity
        << right << setw(8) << STR[count].net_cost
        << right << setw(8) << setprecision(3) << STR[count].tax_rate
        << right << setw(8) << setprecision(2) << STR[count].purchase_tax
        << right << setw(8) << STR[count].discount
        << right << setw(8) << STR[count].total_cost << endl;
    }
    
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout.setf(ios::fixed);
    
    //print to screen
    for (int count = 0; count < CAPACITY; count++)
    {
    cout << left << setw(15) << STR[count].pname
        << left << setw(10) << STR[count].cname
        << right << setw(8) << STR[count].plant_cost
        << right << setw(8) << STR[count].quantity
        << right << setw(8) << STR[count].net_cost
        << right << setw(8) << setprecision(3) << STR[count].tax_rate
        << right << setw(8) << setprecision(2) << STR[count].purchase_tax
        << right << setw(8) << STR[count].discount
        << right << setw(8) << STR[count].total_cost << endl;
    }
    
    
    //close file
    out.close();
}




//*************************************************************************************


///*************************************************************************************
//Name:  count_inventory
//Precondition: The state what is true before the function is called.
//Postcondition: State what is true after the function has executed.
//Description:    Describe what the function does (purpose).

//add for loop
//return the average

double count_inventory (const order_record STR[], const int count)
{
    double total = 0;
    double average;
    
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout.setf(ios::fixed);
    
    for (int count = 0; count < CAPACITY; count++)
    {
        total += STR[count].total_cost;
    }
    
    average = total / CAPACITY;
    
    return average;
}
//*************************************************************************************


//Here is your driver to test the program
//Here is your driver to test the program
int main()
{
    //declare an input file stream
    ifstream in;
    
    //declare an output file stream
    ofstream out;
    
   order_record STR[CAPACITY];
   int count = 0;
        

   if (in.fail())

   {
       cout << "Input file did not open correctly" << endl;
   }
   else
   {
       input(STR, count);
       process(STR, count);
       output(STR, count);
   }

   cout.setf(ios::showpoint);
   cout.precision(2);
   cout.setf(ios::fixed);

   cout << "Average Total Order Cost = " << count_inventory(STR, count) << endl;

   return 0;
}

【问题讨论】:

  • 欢迎来到 Stack Overflow。用您自己的话来说:您的名为output 的函数使用 来决定要打印的行数的规则是什么(即,为了控制for 循环而检查的值是什么? )?现在,它应该使用的规则是什么——您的程序如何跟踪打印的行数? (提示:output 接受的参数是什么?每个的预期目的是什么?另一个提示:当您尝试使用 19 作为 CAPACITY 的值以获得正确的输出时,您是如何选择该值的?)
  • const 作为函数签名中的参数关键字意味着函数不能更改变量的值。在count_inventory 中,您在 for 循环中使用了一个不同的变量,尽管它具有相同的名称,以控制循环。

标签: c++ arrays file


【解决方案1】:

count 由您的 input 函数确定是有效条目的数量,在您的描述中为 19。您已经将count 作为函数参数传递给output()。这就是你的for 循环应该迭代到的,而不是CAPACITY。在您的 for 循环中,您实际上是在声明一个新的 count 变量,隐藏您传入的变量。

更改您的函数签名以采用不同的参数名称,例如total,然后迭代到它。

void output(const order_record STR[], const int &total)
{
    //...  

    for (int count = 0; count < total; count++)
    {
    // ...
    }

// ...
}

对遭受相同错误的process()count_inventory() 执行相同的操作。

【讨论】:

  • 非常感谢,我盯着它看了好几个小时,你的建议非常有效!!
猜你喜欢
  • 2019-03-20
  • 1970-01-01
  • 2020-10-24
  • 1970-01-01
  • 2019-07-13
  • 2023-04-01
  • 2019-04-11
  • 1970-01-01
  • 2022-06-17
相关资源
最近更新 更多