【发布时间】:2021-05-26 22:54:05
【问题描述】:
我希望从我的作业的名称部分中删除空格,这允许用户在我的产品列表中放置任何带有或不带空格的名称。例如“TV stand”,我认为 getline 功能会帮助我,但我无法将 getline 功能添加到我的 main.js 中。谁能帮帮我?
#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;
【问题讨论】:
-
我去掉了双倍行距代码以使其更具可读性,但它至少缺少一些
} -
不清楚你的问题是什么。我猜你知道std::getline。那么,当你使用它时会发生什么?
-
也许错误是:https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction 但是带有 getline() 的代码部分没有添加到问题中
-
我不明白你为什么用
char而不是char array或string来写产品名称
标签: c++ function integer main getline