I am new to C++, the issue im having at the moment is that when I ask the user a question I would like a space after that question but can't get one to appear, also my output comes out spaced out due to this or with words missing.
#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;
}
my output comes out like this:
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
I tried to add cin.ignore() after every question and I added spaces to the questions asked to the user cout << "Enter the name of the movie: "; this just seems to make everything worse
std::getline
and std::cin >> number
do not play well togetherUsing std::getline
and std::cin >> number
in the same program can cause problems.
std::getline
extracts and discards the newline character at the end of a line, while std::cin >> number
does not.std::cin >> number
skips over blank lines to find a number, while getline
does not skip over blank lines to find a non-blank line. It just inputs and returns the blank line.If you keep track of these issues, you can mix them. If you want to use std::getline
, but the prior input used std::cin >> number
, you need to get rid of the newline that is still waiting to be input, before calling std::getline
.
You can use std::ignore
for this, but not the way you have. std::ignore()
discards only one character. That will work if the newline is the next character in the stream, but it will fail if newline is preceded by spaces or other characters. You can get around this by ignoring a large number of characters, but it may be easier just to call std::getline
twice, and discard the value returned by the first one.
std::ignore
unless you need toWhen I ran your program, this is the output I got:
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
The first problem I noticed was the missing O
in Oppenheimer
. That occurred because you called std::ignore();
before calling std::getline
. That ignores one character: the O
. Deleting the call to std:ignore
fixes the problem:
cin.ignore(); // Delete this.
getline(cin, movie_name);
In the example above, the sale of 100 adult tickets generated (100 * $20.00) = $2000, while the sale of 100 child tickets generated (100 * $10.00) = $1000. Gross income was $3000. The 60%/40% split should give $1800 to the distributor and $1200 to the theater.
The output only shows $600 for the distributor and $400 for the theater. What gives?
Once again, std::ignore
is the problem. After std::getline
, there is no newline waiting on the input stream to be discarded. std::getline
has already discarded it.
By calling std::ignore
right before std::cin >> adult_ticket_sold;
, the program discards the 1
in the value 100
, leaving 00
as the value for adult_ticket_sold
.
The fix is to delete the call to std::ignore
.
cin.ignore(); // Delete this
cin >> adult_ticket_sold;
Looking a little more, I realized the the other other call to std::ignore
is not needed. It can also be deleted. Although the preceding call to std::cin >> adult_ticket_sold;
has left a newline in the input stream, there is no need to ignore it. The subsequent call to cin >> child_ticket_sold;
will automatically skip over it.
With all calls to std::ignore
removed, the output looks good:
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
using namespace std;
There are drawbacks to using namespace std;
. See Why is "using namespace std;" considered bad practice?. Your textbook probably should not be teaching you to use it.
Instead, pros just type std::
every time they need to reference a name from the Standard Library. That is why I have used std::getline
, std::cin
, and so on, in this answer.
Unless your professor gets mad, you should do the same thing.