-2

I am looking to remove the space from the name part of my assignment which allows users to put any name with or without space in my product list. for example "TV stand", I assume getline function will help me with it but I couldn't add the getline function into my main. can anyone help me with it?

#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
struct product {
    char product_name;
    int no_of_purchase;
    int no_of_sales;
    double purchase_cost;
    double selling_price;
    double profit_loss;
    double percent_profit_loss;
    string product_sales;
};
bool Compare(product P1, product P2)
{
    return P1.percent_profit_loss > P2.percent_profit_loss;
}
int main()
{
    int n;
    cout << "Enter the number of the product:";
    cin >> n;
    cout << "\n";
    product Products[n];
    for (int i = 0; i < n; ++i) {
        cout << "Enter the name of the product: ";
        cin >> Products[i].product_name;
        cout << "Enter the number of " << Products[i].product_name << " purchased: ";
        cin >> Products[i].no_of_purchase;
        cout << "Enter the number of " << Products[i].product_name << " sold: ";
        cin >> Products[i].no_of_sales;
drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 1
    Sidenote: you may want to read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Brian61354270 Feb 24 '21 at 15:34
  • `product_name` is one character long. Those are not very descriptive product names. – Eljay Feb 24 '21 at 15:37
  • 2
    I got rid of the double spaced code to try to make it more readable however it is missing at least a few `}` – drescherjm Feb 24 '21 at 15:39
  • 1
    It is not clear what your problem is. I guess you know [std::getline](https://en.cppreference.com/w/cpp/string/basic_string/getline). So, what happens when you use it? – Damien Feb 24 '21 at 15:39
  • 1
    Maybe the bug is: [https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) however the part of the code with getline() was not added to the question – drescherjm Feb 24 '21 at 15:41
  • 1
    I don't understand that why are you using `char` not `char array` or `string` to write product name – Linear Data Structure Feb 24 '21 at 19:17
  • @AhmarAamir that is a good point. A `char` is a single character which won't make an interesting product name. – drescherjm Feb 24 '21 at 19:19

1 Answers1

1

To be honest I don't understand your question but this might be helpful for you

I changed the product_name data type from char to string because the char data type is not making any sense

#include <iostream>
#include <string>
using namespace std;
struct product {
    string product_name;
    int no_of_purchase;
    int no_of_sales;
    double purchase_cost;
    double selling_price;
    double profit_loss;
    double percent_profit_loss;
    string product_sales;
};
bool Compare(product P1, product P2)
{
    return P1.percent_profit_loss > P2.percent_profit_loss;
}
int main()
{
    int n;
    cout << "Enter the number of the product:";
    cin >> n;
    cin.ignore();
    cout << "\n";
    product Products[n];
    for (int i = 0; i < n; ++i) {
        cout << "Enter the name of the product: ";
        getline(cin, Products[i].product_name);
        cout << "Enter the number of " << Products[i].product_name << " purchased: ";
        cin >> Products[i].no_of_purchase;
        cout << "Enter the number of " << Products[i].product_name << " sold: ";
        getline(cin, Products[i].no_of_sales);
        cin.ignore();
    }
    return 0;
}