c++ - 在 C++ 中读取和打印输入和 C 字符串

标签 c++ arrays function c-strings

我有一个简单的程序来读取和回应用户的输入并计算总金额。我在使用数组时遇到问题。我想知道如何使用数组读取和打印每个产品名称和成本以及结账时的总计。我还应该使用函数吗?

#include <iostream>
#include <cstring>
#include <iomanip>

using namespace std;

const int MAX_CHAR = 100;

void pause();

int main()
{
    char productName[MAX_CHAR]; 
    double price;
    char reply;
    double total = 0;

    cout << fixed << showpoint << setprecision(2);
    cout << "Welcome to your shopping calculator!" << endl;

    do {
        cout << "Enter the product name: ";
        cin.get(productName, MAX_CHAR, '\n');
        cin.ignore(100, '\n');

        cout << "Enter the amount: $";
        cin >> price;
            while (!cin) {
                  cin.clear();
                  cin.ignore(100, '\n');

                  cout << "Invalid amount. Please enter the amount: $";
                  cin >> price;
            }
            cin.ignore(100, '\n');

         cout << "The product name is" << " " << productName << " "
              << " and it costs" << " " << "$" << price << endl;

         total += price;
         cout << "The total amount is " << " " << total << endl; //program needs to keep a running total

         cout << "Would you like to continue shopping? (y/n): ";
         cin >> reply;
         cin.ignore(100, '\n');

     } while ( reply == 'Y' || reply == 'y');   //the program will continue until the user wants to checkout

   pause();
   return 0;
}

 void pause()
 {
  char ch;

   cout << "Press q followed by Enter key to continue....." << endl;
   cin >> ch;
  }  

感谢您的帮助!

最佳答案

您需要使用 std::map 将产品名称映射到成本这样您就可以在之后打印相应的对。至于总计,它存储在变量 total 中。所以打印它很简单。

为此,您需要包含 <map> header ,作为标准库类 std::map在那里定义。此外,我还对您的代码进行了一些应考虑的更改。特别是,使用 std::string并使用 std::numeric_limits<...>::max()返回常量。

#include <iostream>
#include <string>
#include <map>

int main()
{
    std::string productName; 
    double price;
    char reply;
    double total = 0;
    std::map<std::string, double> productToPrice;

    std::cout << std::fixed << std::showpoint << std::setprecision(2);
    std::cout << "Welcome to your shopping calculator!" << std::endl;

    while ((std::cin >> reply) && (reply == 'y' || reply == 'Y'))
    {
        cout << "Enter the product name: ";
        std::cin >> productName;

        cout << "Enter the amount: $";
        while (!(cin >> price))
        {
            cin.clear();
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            cout << "Invalid amount. Please enter the amount: $";

        }
        total += price;
        productToPrice.insert(std::make_pair(productName, price));

        cout << "Would you like to continue shopping? (y/n): ";
    }
    ...
}

请注意我所做的更改。请使用它们。

要打印,您只需执行以下操作:

typedef std::map<std::string, double>::const_iterator iter_type;
for (iter_type beg(productToPrice.begin()),
               end(productToPrice.end()); beg != end; ++beg)
{
    std::cout << beg.first << " -- " << beg.second << std::endl;
}

std::cout << "\nThe total price is: " << total;

关于c++ - 在 C++ 中读取和打印输入和 C 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21054098/

相关文章:

php - 在php中从多维数组创建嵌套的父子数组

python - 如何通过函数注解表明一个函数需要一个函数作为参数,或者返回一个函数?

python - 如何在不中断循环的情况下返回值?

c++ - 为什么用户定义的转换没有在调用对象上隐式发生

c++ - 如何从算法中找到递归关系

c++ - 静态变量与成员

c - 关于函数声明的问题

android - 如何绘制/渲染 Bullet Physics 碰撞体/形状?

c - 循环内的结构数组

java - 将文本文件中的数字读取到 Java 中的 ArrayList