读取和打印输入和 C 字符串C++

reading and printing inputs and C-string in C++

本文关键字:字符串 C++ 输入 打印 读取      更新时间:2023-10-16

我有一个简单的程序来读取和回显用户的输入并计算总量。我在使用数组时遇到问题。我想知道如何使用数组来读取和打印每个产品名称以及成本,并在结帐时查找总计。我是否也应该使用函数?

#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> 标头,因为标准库类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++ 中的 I/O 约定,这导致了很多问题。如果您能详细说明您的问题到底是什么,那将非常有帮助。

Carpetfizz 是正确的,因为您在编译时不知道项目的数量,因此您需要使用带有 std::vector 的动态数组。您可以在此处了解向量。

此外,C++有一个非常有用的字符串数据类型,您可以将其包含在 #include <string> 中。使用它以及垃圾字符串(例如 string junk; ,您可以避免使用cin.ignore(...)并通过使用 getline(cin, junk) 获得更干净的 I/O。

我强烈建议这样做,因为创建 C 字符串或 C 样式字符串的向量很痛苦,因为 C 样式字符串实际上是字符数组,因此您必须使用std::vector<std::vector<char> >产品。