Well, I'm a newbie, this is my year as a senior researcher. I am trying to do an exercise from my tutorial in which I use a structure called MovieData that has a constructor that allows me to initialize member variables when a MovieData struct is created. This is 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 result i got:
Title
Director
2009
90
1,000,000.00
1,000,000.00
Terminator
James cameron
1984
120
-92559631349317830000000000000000000000000000000000000000000000.00.00
-92559631349317830000000000000000000000000000000000000000000000.00.00
Press any key to continue. . .
Compiled in Microsoft Visual C ++ 2008 Express Edition.
My question is: is this due to a double data type overflow? I even tried it with a long double, and the same thing happens. although I used 5mil as production_cost and 2mil as first_year_revenue both the output numbers are the same. Using my default constructor correctly prints 1,000,000. Am I using the correct data type in this case? I want it to be double, because it is a monetary number, dollars and a cent.
Thanks for any help that comes to my mind. Sorry for my long question. This is my first post on SO, so any feedback on the correct format of posting questions will be great, thanks!
user173424
source share