-2

My program does not take input.

It is not the case of String, I am facing problem in cin operator, so I can't understand the output of the program. Please give me reason and its accurate solution.

struct Product
{
        int code;
        string description;
        char packaging;
        float price;
        float discount;
};

int main()
{
        Product p;

        cout << "\nEnter a code of a product:\t";
        cin >> p.code;

        cout << "\nEnter the description of a product:\t";
        getline(cin, p.description);
        cin.ignore();

        cout << "\nPress:";
        cout << "\n\t'L' or 'l' for large";
        cout << "\n\t'M' or 'm' for medium";
        cout << "\n\t'S' or 's' for small";

Error


        cout << "\n\nChoose your package from the list above:\t";
        cin >> p.packaging;

Error

   switch (p.packaging)
       {
       case 'L':
       case 'l':
                p.price = 50000;
                cout << "\nThe price of a product:\t$" << p.price;
                break;

       case 'M':
       case 'm':
                p.price = 25000;
                cout << "\nThe price of a product:\t$" << p.price;
                break;

       case 'S':
       case 's':
                p.price = 12500;
                cout << "\nThe price of a product:\t$" << p.price;
                break;

      default:
                cout << "\nYou entered wrong";
      }

      p.discount = 0.02;

      cout << "\nDISCOUNT:\t" << p.discount;

      double deductedPrice;

      deductedPrice = p.price * p.discount;

      p.price = p.price - deductedPrice;

      cout << "\nDiscounted price:\t" << p.price;



      cout << "\n\n\n\n\n";

      system("pause");

      return 0;
}

I have searched from the internet but I can't get any solution which satisfies me.

Snapshot of my output screen

Output screen

Ghufran Hasan
  • 105
  • 2
  • 9

1 Answers1

1

The problem is that after std::cin >> p.code there is an additional newline in the stream, which is "eaten" by getline. Solution: put the following line immediately after std::cin >> p.code;

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

to ignore it (make sure that you #include <limits> at the beginning).

vsoftco
  • 52,188
  • 7
  • 109
  • 221
  • Error in p.packaging not p.code – Ghufran Hasan Nov 11 '15 at 19:44
  • I am not satisfied with your answer. – Ghufran Hasan Nov 11 '15 at 20:09
  • @GhufranHasan That's why I marked it duplicate and closed the question. Please read the duplicate carefully and you will find the answer there. And btw, "Error" means in general compile-time error. You are probably getting logic errors. – vsoftco Nov 11 '15 at 20:10
  • Getline function is use for the string. but I have used only cin operator and type of p.packaging is char.. – Ghufran Hasan Nov 11 '15 at 20:16
  • If you make the effort and read that duplicate, you'll see what is going on. Both `cin` and `getline` operate on the same input stream, and one newline is left in the stream after the last read before `cin >> packaging;`. – vsoftco Nov 11 '15 at 20:18
  • Ok.. You maybe true, but your solution is very complex.. – Ghufran Hasan Nov 11 '15 at 20:22
  • @GhufranHasan That's how C++ streams work. You have to ignore the newline after `cin` and before `getline`, otherwise the first `getline` eats up the newline, then you ignore a valid character from what was supposed to be read from the `getline`, then `cin` reads the second character from what the initial `getline` was supposed to read. – vsoftco Nov 11 '15 at 20:49