Friday, July 31, 2009

What am i doing wrong here? c++ ( im getting errors)?

cout %26lt;%26lt; "This project was written by Jacob A. DePratti" %26lt;%26lt; endl;





char moviename[50], adult[50], child[50];


int Six,Three,Total,AdultSales,ChildSales,Gr...





AdultSales = adult*6;


ChildSales = child*3;


GrossSales = AdultSales + ChildSales;








cout %26lt;%26lt; "" %26lt;%26lt; endl;





cout %26lt;%26lt; "Enter the movie's Name: ";


cin.get(moviename,50);


cout %26lt;%26lt; "" %26lt;%26lt; endl;


cout %26lt;%26lt; "Enter the number of adult tickets sold: ";


cin %26gt;%26gt; adult;


cout %26lt;%26lt; "" %26lt;%26lt; endl;


cout %26lt;%26lt; "Enter the number of child tickets sold: ";


cin %26gt;%26gt; child;


cout %26lt;%26lt; "" %26lt;%26lt; endl;











cout %26lt;%26lt; "***************************************... %26lt;%26lt; endl;


cout %26lt;%26lt; "" %26lt;%26lt; endl;


cout %26lt;%26lt; "INCOME STATEMENT" %26lt;%26lt; endl;


cout %26lt;%26lt; "******************" %26lt;%26lt; endl;





cout %26lt;%26lt; "" %26lt;%26lt; endl;


cout %26lt;%26lt; "Movie Name: "%26lt;%26lt;moviename%26lt;%26lt; endl;


cout %26lt;%26lt; "Adult Tickets Sold: "%26lt;%26lt;adult %26lt;%26lt; endl;


cout %26lt;%26lt; "Child Tickets Sold: "%26lt;%26lt;child%26lt;%26lt; endl;


cout %26lt;%26lt; "Gross Box Office Profits: " %26lt;%26lt; GrossSales %26lt;%26lt;endl;


cout %26lt;%26lt; "Net Box Office Profit: " %26lt;%26lt; endl;


cout %26lt;%26lt; "Amount Paid To Distributers" %26lt;%26lt; endl;


cout %26lt;%26lt; " " %26lt;%26lt; endl;

What am i doing wrong here? c++ ( im getting errors)?
instead of


char moviename[50], adult[50], child[50];


do


char moviename[50];


int adult[50], child[50];
Reply:I dunno, looks okay at first glance, but seeing the error messages would be very helpful.
Reply:What's the first error
Reply:At first glance it looks ok, make sure there is a ';' at the end of your int line. If I knew the error I may be able to narrow it down a little more for you.
Reply:At a glance...





You declare two arrays of characters:


char ... adult[50], child[50];





When you use the variable without the brace [] qualifier, you're actually using it as a pointer to the first member of the array.





So looking at your code, I see the following problems:





1. Calculation of variables AdultSales, ChildSales is performing pointer calculations instead of calculations using integer values.





2. You treat moviename as a string value on your cin statement (which is correct), but you don't do the same thing for adult and child. If you want adult and child to be strings, you need to enter them as strings and convert them to numeric equivalents. If you don't, you're just doing calculations on each characters ASCII code.





3. AdultSales, and ChildSales calculations are performed before you have values for adult and child sales. Remember that the equals (=) symbol is a one time assignment statement.





For example, the code:


x = 1;


y = x;


x = 2;


cout %26lt;%26lt; "The value of x: " %26lt;%26lt; x %26lt;%26lt; endl;


cout %26lt;%26lt; "The value of y: " %26lt;%26lt; y %26lt;%26lt; endl;





will have the following output:


The value of x: 2


The value of y: 1





You want to call your calculations after you have values for the variables used in them.





Hope that helps!


No comments:

Post a Comment