【问题标题】:How to call the function to pass the value in this code? C++如何调用函数来传递这段代码中的值? C++
【发布时间】:2015-09-28 10:51:02
【问题描述】:

我正在尝试做作业,但我不知道如何从这里继续。任务要求设计一个计算器来控制预算。以下是要求:

您是否曾经在购物时现金不足,无法超过一定的美元限额?你有点需要一个计算器。如果设备实际上是购物车的一部分,并且当您将物品添加到购物车中时,它会增加您的总计数器,这将是很酷的。为了解决这个问题,我们将编写一个程序来记录我们到访商店时所花费的金额。

你的程序应该做什么:

  1. 允许购物者(用户)输入产品名称和成本。这应该得到回应和确认。请务必检查是否存在不良数据。
  2. 应允许用户继续此操作,直到他们想要结帐为止。
  3. 您的程序需要保持运行总计。
  4. 结帐时,应显示总计。

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    int item_info(float, char);
    
    int main()
    {
    
        //Declare all varibles.
        char itemName[100];
        float itemPrice;
        char option;
        float total;
        int count;
    
        count = 1;
    
        // Setting decimal point for output.
        cout.setf(ios::fixed, ios::floatfield);
        cout.setf(ios::showpoint);
        cout.precision(2);
    
        item_info(itemPrice, itemName[100]);
        total = total + itemPrice;
    
        //Ask user if they want to continued to add more items.
        cout << "Do you want to add more items? (Y/N)";
        cin >> option;
    
        if (option == 'Y' || option == 'y')
        {
            item_info(itemPrice, itemName[100]);
    
        }
        else
        {
            cout << "The total price of all items: $" << total << ".\n";
        }
    
        }
    
    int item_info(float itemPrice, char itemName[100])
        {
        //Ask for input.
        cout << "Please enter the item name: ";
        cin.get(itemName, '\n');
    
        //Ask for the price of the item.
        cout << "Please enter the price of the item: ";
        cin >> itemPrice;
    
        //Check if it's a valid data.
        while (!cin)
        {
            cin.clear();
            cin.ignore(100, '\n');
            cout << "Invalid input! Please enter the item price:";
            cin >> itemPrice;
        }
        //Only take necessary data.
        cin.ignore(100, '\n');
    
        //Confirm the user input.
        cout << "Item information:" << itemName <<": " << itemPrice <<"\n";
        return itemPrice;
        }
    

谁能告诉我怎么解决?

【问题讨论】:

  • 不要使用float 来表示货币价值:精度不够。一旦你使用了两个有效数字来表示便士/美分,你就只剩下大约 5 英镑/美元了。
  • 使用std::string,会解决你所有的问题
  • 您通常不应该使用浮点来赚钱,因为数据类型不够精确。只需以美分计算,仅在打印值时转换为浮点数(如果需要)。
  • @SingerOfTheFall 我想看看你是如何仅使用std::string计算总数的
  • @Simon,由于他的char[] 用法,该操作的代码甚至无法编译,而您正在谈论浮点精度......

标签: c++ function loops callback


【解决方案1】:

您有两个明显的问题:首先是item_info 的声明和定义不匹配。一个接受一个字符,另一个接受一个字符数组。

第二个问题是您使用单个字符调用函数,但问题是您的索引超出了itemName 数组的范围。

我的猜测是该函数应该接受一个字符串(即一个字符数组),这意味着声明和调用是错误的。调用函数时不应使用索引,只需像任何其他参数一样传递itemName(参见例如itemPrice)。

【讨论】:

    【解决方案2】:

    首先,您的item_info 参数在定义和声明方面有所不同:

    int item_info(float, char);//should be char[100]
    int item_info(float itemPrice, char itemName[100])
    {
    <...>
    }
    

    其次,这里只传递数组的第 100 个元素,而不是数组本身:

    item_info(itemPrice, itemName[100]); //should be  item_info(itemPrice, itemName);
    

    最后,你真的应该使用std::string,它会帮助你避免这样的问题。

    【讨论】:

      【解决方案3】:

      您的item_info 函数返回一个int,并且您不会在任何地方使用此返回值。

      如果您想使用itemPriceitemName 作为输入/输出参数,您需要使用引用&amp;。 另外,为什么不使用std::string 而不是char 数组呢?

      正如其他答案所提到的。 item_info 的声明和定义不匹配。

      【讨论】:

        猜你喜欢
        • 2021-12-02
        • 2019-08-10
        • 2018-09-16
        • 1970-01-01
        • 2016-10-10
        • 2017-05-19
        • 1970-01-01
        • 1970-01-01
        • 2017-07-25
        相关资源
        最近更新 更多