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

时间:2014-01-10 20:46:00

标签: 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;
  }  

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您需要使用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;

答案 1 :(得分:0)

你肯定是在正确的轨道上。我认为你在C和C ++中混合了I / O约定,这导致了很多问题。如果你能详细说明你的问题究竟是什么,那将是非常有帮助的。

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

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

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