468,504 Members | 1,986 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

Need Help with Box Office Program

1
am new to c++ and having problems running this program properly. Am suppose to create a data file using Notepad that stores the inputs for the program. Read the data from your input file. Also, instead of writing the output to the screen, write it to a file named BoxOfficeReport.txt.

I created a file with notepad and named it BoxOfficeReport.txt. in it I typed
"A Movie title"
382
127

It compiles but when i run it i get something like this
Name of the movie: y(arrow type sybol)B
adult tickets sold: 4270931
children's tickets sold: 0

can anyone help.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8.     {
  9.  
  10.         ifstream inFile;
  11.         const int SIZE = 25;
  12.         char movie[SIZE];    
  13.  
  14.         int adult = 6;
  15.         int children = 3;
  16.         int gross_profit, net_profit, distributor, aticket, cticket;
  17.  
  18.                 inFile.open("BoxOfficeReport.txt");
  19.  
  20.         inFile >> movie;
  21.         cout   << "Name Of The Movie: " << movie << endl;
  22.  
  23.         inFile >> aticket;
  24.         cout   << "Adult Tickets Sold: " << aticket << endl;
  25.  
  26.         inFile >> cticket;
  27.         cout   << "Children's Tickets Sold: " << cticket << endl;
  28.  
  29.         gross_profit = (aticket*adult)+(cticket*children);
  30.         net_profit = gross_profit * .20;
  31.         distributor = gross_profit * .80;
  32.  
  33.         cout   << "Gross Box Office Profit: " << gross_profit << endl;
  34.         cout   << "Net Box Office Profit: " << net_profit << endl;
  35.         cout   << "Amount Paid to Distributor: " << distributor << endl;
  36.  
  37.         inFile.close();
  38.         return 0;
  39.     }
Feb 1 '08 #1
1 3472
weaknessforcats
9,207 Expert Mod 8TB
You can't do this:
inFile >> movie;
movie is a char array. The >> operator will stop on the first whitespace character so all you get is A.

You wil; need to use the getline() method to fetch the entire string.

Then it shoud work OK.

You could test this with a movie title that has only one word and see of your code suddenly starts working.
Feb 1 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Benjamin Walling | last post: by
2 posts views Thread by somersbar | last post: by
5 posts views Thread by Michael Russell | last post: by
1 post views Thread by larpup | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.