Well, I'm a beginner, it's my year as a computer science major. I'm trying to do
an exercise from my textbook that has me use a struct called MovieData
that has
a constructor that allows me to initialize the member variables when the MovieData
struct is created. Here's what my code looks like:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// struct called MovieData
struct MovieData
{
string title;
string director;
unsigned year;
unsigned running_time;
double production_cost;
double first_year_revenue;
MovieData() // default constructor
{
title = "Title";
director = "Director";
year = 2009;
running_time = 90;
production_cost = 1000000.00;
first_year_revenue = 1000000.00;
}
// Constructor with arguments:
MovieData(string t, string d, unsigned y, unsigned r, double p, double f)
{
title = t;
director = d;
year = y;
running_time = r;
}
};
// function prototype:
void displayMovieData(MovieData);
// main:
int main()
{
// declare variables:
MovieData movie, terminator("Terminator", "James Cameron", 1984, 120, 5000000, 2000000);
// calling displayMovieData function for movie and terminator
// so it will display information about the movie:
displayMovieData(movie);
displayMovieData(terminator);
return 0;
}
// displayMovieData function:
// It receives struct MovieData variable as
// an argument and displays that argument's
// movie information to the user.
void displayMovieData(MovieData m)
{
cout << m.title << endl;
cout << m.director << endl;
cout << m.year << endl;
cout << m.running_time << endl;
cout << fixed << showpoint << setprecision(2);
cout << m.production_cost << endl;
cout << m.first_year_revenue << endl << endl;
}
here is the output I received:
Title Director 2009 90 1000000.00 1000000.00 Terminator James Cameron 1984 120 -92559631349317830000000000000000000000000000000000000000000000.00 -92559631349317830000000000000000000000000000000000000000000000.00 Press any key to continue . . .
Compiled on Microsoft Visual C++ 2008 Express Edition.
My question is, is this happening due to an overflow of the double data-type? I even tried it using long double and the same thing occurs. even though i used 5mil as production_cost
and 2mil as first_year_revenue
both numbers output are the same. Using my default constructor properly prints out 1000000. Am I using the correct data-type in this case? I want it to be double because it's a monetary number, dollars and cents.
Thanks for whatever help comes my way. Sorry about my long question. It's my first post on SO, so any feedback on correct format of posting questions will be great, Thanks!