ええと、私は初心者です。コンピューター サイエンス専攻の年です。MovieData
構造体の作成時にメンバー変数を初期化できるコンストラクターを持つ構造体を使用する、教科書からの演習を試みていますMovieData
。私のコードは次のようになります。
#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;
}
ここに私が受け取った出力があります:
題名 監督 2009年 90 1000000.00 1000000.00 ターミネーター ジェームズ・キャメロン 1984年 120 -92559631349317830000000000000000000000000000000000000000000.00 -92559631349317830000000000000000000000000000000000000000000.00 何かキーを押すと続行します 。. .
Microsoft Visual C++ 2008 Express Edition でコンパイル。
私の質問は、これは double データ型のオーバーフローが原因ですか? long double を使用して試してみましたが、同じことが起こります。両方の数値出力が同じであるためproduction_cost
、5mil と 2mil を使用しましたが。first_year_revenue
デフォルトのコンストラクターを正しく使用すると、1000000 が出力されます。この場合、正しいデータ型を使用していますか? 通貨の数字、ドルとセントなので、2 倍にしたいのです。
助けてくれてありがとう。私の長い質問で申し訳ありません。これは SO に関する私の最初の投稿なので、質問を投稿する正しい形式に関するフィードバックは素晴らしいものになるでしょう。ありがとう!