简体   繁体   English

C ++不会添加传入的值

[英]C++ won't add values passed in

When I pass in a value and assign it to another variable, it never seems to add them together. 当我传入一个值并将其分配给另一个变量时,似乎永远不会将它们加在一起。 It only outputs both totals in the file, but not together. 它仅在文件中输出两个总数,但不一起输出。 Can anyone point out my mistake? 谁能指出我的错误?

void financialReport(int price)
{
    ofstream financial_log("financial.txt", ios::app);

    int total = 0;
    total += price;
    int test = total++;
    financial_log << "Total: " << test;


   financial_log.close(); 
}


    cout << "Destination:  ";
cin >> destination;
cout << "Price agreed: ";
cin >> price;

financialReport(price);

This is the output I get in my text file: 这是我在文本文件中获得的输出:

Total4Total5

Also, for some reason, there is no space between the total and the price. 同样,由于某种原因,总数和价格之间没有空格。

It's a little hard to understand your question but I think one issue conuld be confusion about how the ++ operator works. 很难理解您的问题,但是我认为一个问题可能是有关++运算符的工作方式的混淆。 variable_name++ will increment the variable after its evaluation in the current statement and ++variable_name will increment the variable before its evaluation in the current statement. variable_name ++将在当前语句中对其求值后对变量进行递增,而++ variable_name将在当前语句中对其求值对变量进行递增。

In your code above, ++ has no effect. 在上面的代码中,++没有作用。 If you want your variable test to have a value of one greater than total. 如果您希望变量测试的值大于合计的值一。 Then you need to do this: 然后,您需要执行以下操作:

int test = ++total;

However, honestly in your case, that doesn't even make sense since total isn't used anywhere in the function after that. 但是,老实说,这完全没有意义,因为在此之后函数中的任何地方都没有使用total。 You would be better off just simply doing: 只需执行以下操作,您就会更好:

int test = total + 1;

Try opening a separate and specific questions about any ios or in/out formatting concerns. 尝试打开有关任何ios或输入/输出格式问题的单独且特定的问题。

It also looks like you're somehow trying to come up with a total across two function calls using a local variable which will never work because total will get reset to 0 on every function call. 看起来您似乎也在某种程度上尝试使用局部变量来计算两个函数调用的合计,该变量永远不会起作用,因为在每个函数调用中合计将重置为0。

I would be willing to bet that if you do as A. Yurchenko suggests you will probably find your own bug. 我愿意打赌,如果您像A. Yurchenko那样做,您可能会发现自己的错误。

If you want to store total value in a file, you may use this: 如果要将total价值存储在文件中,可以使用以下方法:

void financialReport(int price) {

    ifstream financial_log_in("financial.txt");
    int total = 0;
    string dummy;
    while (financial_log_in >> dummy && dummy != "Total:") {
    }
    if (dummy == "Total:") {
        financial_log_in >> total;
    }

    ofstream financial_log_out("financial.txt");
    total += price;
    financial_log_out << "Total: " << total << endl;
    financial_log_out.close();
}

Here we read the current value from financial.txt , update it and write it back. 在这里,我们从financial.txt读取当前值,对其进行更新并写回。

But in case you just call the function a few times during one runtime this will be more efficient: 但是,如果您仅在一个运行时中多次调用该函数,则效率会更高:

void financialReport(int price) {
    static int total = 0;
    ofstream financial_log("financial.txt");
    total += price;
    financial_log << "Total: " total << endl;
}

This way financialReport(5); financialReport(6); 这样, financialReport(5); financialReport(6); financialReport(5); financialReport(6); will result in Total: 11 , but when you restart the program total will be 0 again. 将得出Total: 11 ,但是当您重新启动程序时, total将再次为0

Please, notice that both methods will overwrite financial.txt . 请注意,这两种方法都将覆盖financial.txt If you don't want this behavior add ios::app flag to the constructor of the ofstream objects. 如果您不希望出现这种情况,请将ios::app标志添加到ofstream对象的构造函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM