我是 C++ 新手,目前遇到的问题是,当我问用户一个问题时,我想要在该问题后面有一个空格,但无法出现一个空格,而且我的输出也因此而出现间隔或者缺少单词。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string movie_name;
string lines = "-------------------------------";
int adult_ticket_sold, child_ticket_sold;
double gross_profit, theater_gross_profit, distributor_gross_profit;
const float adult_ticket_price = 20;
const float child_ticket_price = 10;
const double theater_income_rate = .4;
const double distributor_income_rate = .6;
cout << "Project 1\n";
cout << "Written by: billy bill\n";
cout << lines << endl;
cout << "Box Office Entry\n";
cout << lines << endl;
cout << "Enter the name of the movie: ";
cin.ignore();
getline(cin, movie_name);
cout << "Enter the amount of adult tickets sold: ";
cin.ignore();
cin >> adult_ticket_sold;
cout << "Enter the amount of children tickets sold: ";
cin.ignore();
cin >> child_ticket_sold;
gross_profit = (adult_ticket_sold * adult_ticket_price) + (child_ticket_sold * child_ticket_price);
theater_gross_profit = theater_income_rate * ((adult_ticket_sold * adult_ticket_price) + (child_ticket_sold * child_ticket_price));
distributor_gross_profit = distributor_income_rate * gross_profit;
cout << lines << endl;
cout << "Summary: Movie Income Distribution\n";
cout << lines << endl;
cout << "The movie:" << movie_name << endl;
cout << "This is how much the theater made: " << theater_gross_profit << endl;
cout << "The distributor gross profit is: " << distributor_gross_profit << endl;
cout << lines << endl;
cout << "End of Project 1";
return 0;
}
我的输出是这样的:
Project 1
Written by: billy bill
-------------------------------
Box Office Entry
-------------------------------
Enter the name of the movie:the boy that lived
Enter the amount of adult tickets sold:10
Enter the amount of children tickets sold:10
--------
-----------------------
Summary: Movie Income Distribution
-------------------------------
The movie:he boy that lived
This is how much the theater made: 40
The distributor gross profit is: 60
-------------------------------
End of Project 1
Process finished with exit code 0
我尝试在每个问题后添加 cin.ignore() ,并在向用户 cout 提出的问题中添加空格 << "Enter the name of the movie: "; this just seems to make everything worse
std::getline
和 std::cin >> number
在一起玩得不好在同一程序中使用
std::getline
和 std::cin >> number
可能会导致问题。
std::getline
提取并丢弃行尾的换行符,而 std::cin >> number
则不会。std::cin >> number
会跳过空行来查找数字,而getline
不会跳过空行来查找非空行。它只是输入并返回空行。如果您跟踪这些问题,您可以将它们混合在一起。如果你想使用
std::getline
,但之前的输入使用了std::cin >> number
,则需要在调用std::getline
之前删除仍在等待输入的换行符。
您可以使用
std::ignore
来实现此目的,但不能像您那样使用。 std::ignore()
仅丢弃一个字符。如果换行符是流中的下一个字符,那么这将起作用,但如果换行符前面有空格或其他字符,则它将失败。您可以通过忽略大量字符来解决此问题,但只需调用 std::getline
两次并丢弃第一个字符可能会更容易。
std::ignore
当我运行你的程序时,这是我得到的输出:
Project 1
Written by: billy bill
-------------------------------
Box Office Entry
-------------------------------
Enter the name of the movie: Oppenheimer
Enter the amount of adult tickets sold: 100
Enter the amount of children tickets sold: 100
-------------------------------
Summary: Movie Income Distribution
-------------------------------
The movie:ppenheimer
This is how much the theater made: 400
The distributor gross profit is: 600
-------------------------------
End of Project 1
我注意到的第一个问题是
O
中缺少 Oppenheimer
。发生这种情况是因为您在拨打 std::ignore
之前拨打了 std::getline
。删除对 std:ignore
的调用可以解决问题:
cin.ignore(); // Delete this.
getline(cin, movie_name);
在上面的示例中,销售 100 张成人门票生成 (100 * $20.00) = $2000,而销售 100 张儿童门票生成 (100 * $10.00) = $1000。总收入为3000美元。 60%/40% 的分成应该给发行商 1800 美元,给剧院 1200 美元。
输出仅显示发行商为 600 美元,剧院为 400 美元。给什么?
再次,
std::ignore
是问题所在。在 std::getline
之后,输入流上没有换行符等待被丢弃。 std::getline
已经丢弃了。
通过在
std::ignore
之前调用 cin >> adult_ticket_sold;
,程序会丢弃值 1
中的 100
,将 0
保留为 adult_ticket_sold
的值。
修复方法是删除对
std::ignore
的调用。
cin.ignore(); // Delete this
cin >> adult_ticket_sold;
再看一点,我意识到不需要另一个对 std::ignore 的调用。也可以删除。
删除所有对
std::ignore
的调用后,输出看起来不错:
Project 1
Written by: billy bill
-------------------------------
Box Office Entry
-------------------------------
Enter the name of the movie: Oppenheimer
Enter the amount of adult tickets sold: 100
Enter the amount of children tickets sold: 100
-------------------------------
Summary: Movie Income Distribution
-------------------------------
The movie:Oppenheimer
This is how much the theater made: 1200
The distributor gross profit is: 1800
-------------------------------
End of Project 1