繁体   English   中英

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

[英]reading and printing inputs and C-string in C++

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

#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 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;类的垃圾字符串string junk; ,您可以避免使用cin.ignore(...)并通过使用getline(cin, junk)获得更干净的I / O。

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

暂无
暂无

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

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