题
好了,我是一个初学者,这是我一年的计算机科学专业。我试图做
从我的课本练习有我使用结构称为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;
}
这里是我接收到的输出:
Title Director 2009 90 1000000.00 1000000.00 Terminator James Cameron 1984 120 -92559631349317830000000000000000000000000000000000000000000000.00 -92559631349317830000000000000000000000000000000000000000000000.00 Press any key to continue . . .
编译上的Microsoft Visual C ++ 2008速成版。
我的问题是,会出现这种情况,由于双数据类型的溢出?我甚至尝试了使用长双,并会发生同样的事情。即使我用5密耳的production_cost
和2密耳为first_year_revenue
两个数字输出是相同的。用我的默认构造函数正确打印出100万。现在用正确的数据类型在这种情况下我?我想这是双重因为它是一个货币数,美元和美分。
感谢您力所能及的帮助来我的方式。对不起我长的问题。这是我对SO的第一篇文章,所以在发布问题的正确格式的任何反馈将是巨大的,谢谢!
解决方案
感谢发布您的完整代码,这个问题现在很明显。下面的函数的问题是:
MovieData(string t, string d, unsigned y, unsigned r, double p, double f)
{
title = t;
director = d;
year = y;
running_time = r;
}
您已省略了以下语句:
production_cost = p;
first_year_revenue = f;
没有这些陈述,production_cost
和first_year_revenue
不使用上述构造时初始化。
本练习强调需要发布的确切您发布的堆栈溢出的问题时使用的代码。您发布的代码的第一个版本是不同的,并没有包含这个bug。
其他提示
固定,从编译(缺少分号,大写字母M代替小写字母m)我得到以下停止代码中的错误:
#include <iostream>
#include <iomanip>
using namespace std;
struct MovieData
{
string title;
string director;
unsigned year;
unsigned running_time;
double production_cost;
double first_year_revenue;
MovieData() // My default constructor
{
title = "Title";
director = "Director";
year = 2009;
running_time = 90;
production_cost = 1000000.00; // this one comes out ok.
first_year_revenue = 1000000.00; // this one comes out ok as well.
}
// This is my 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;
production_cost = p;
first_year_revenue = f;
}
};
void displayMovieData(MovieData m)
{
cout << m.title << endl;
cout << m.director << endl;
cout << m.year << endl;
cout << m.running_time;
cout << fixed << showpoint << setprecision(2);
cout << m.production_cost << endl;
cout << m.first_year_revenue << endl << endl;
}
int main()
{
MovieData terminator(
"Terminator", "James Cameron", 1984, 120, 5000000, 2000000);
displayMovieData(terminator);
return 0;
}
编译并运行它的不可以重现您的问题...
$ g++ -Wall --pedantic z.cc
$ ./a.out
Terminator
James Cameron
1984
1205000000.00
2000000.00
$
请复制的代码完全粘贴为我把它在这里,让我们知道会发生什么(以怎样的编译器,平台,等等等等 - 我使用的是MacOSX上10.5 GCC 4.0.1)。
没有,你不会溢出任何电影的收入或生产成本的双重范围,直到永远。
我的猜测是,问题出在你的displayMovieData功能。您可以将代码发布到了吗?点击
IIRC你可以得到印像,如果你调用像printf和它得到单打和双打之间的混淆奇怪的值。或者,如果你通过它%d
代替%f
的......
您输出不会告诉你的代码相匹配 - 你忽略你的输出声明包含的是运行时间后“<< ENDL”。这总是让我们的调试更难。
下面是您的代码在MacOS X 10.5.8(豹)与G ++ 4.0.1正常工作。
#include <string>
using namespace std;
struct MovieData
{
string title;
string director;
unsigned year;
unsigned running_time;
double production_cost;
double first_year_revenue;
MovieData() // My default constructor
{
title = "Title";
director = "Director";
year = 2009;
running_time = 90;
production_cost = 1000000.00; // this one comes out ok.
first_year_revenue = 1000000.00; // this one comes out ok as well.
}
// This is my 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;
production_cost = p;
first_year_revenue = f;
}
};
#include <iostream>
#include <iomanip>
using namespace std;
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;
}
int main()
{
MovieData def;
MovieData terminator("Terminator", "James Cameron", 1984, 120, 5000000, 2000000);
MovieData terminator2("Terminator 2", "James Cameron", 1984, 120, 5000000.0, 2000000.0);
displayMovieData(def);
displayMovieData(terminator);
displayMovieData(terminator2);
}
我得到的输出是:
Title
Director
2009
90
1000000.00
1000000.00
Terminator
James Cameron
1984
120
5000000.00
2000000.00
Terminator 2
James Cameron
1984
120
5000000.00
2000000.00
我的最好的论文(由上面的数据不支持)是不知为何,你还没有得到“诠释”到“双”在调用构造正在发生转换,但我承认我不明白怎么会发生。
像这里其他的答案,我不能确定的问题可能是什么,因为代码似乎是好的。然而,尝试更换此声明:
void displayMovieData(MovieData m)
与
void displayMovieData(const MovieData &m)
,看看你的情况是否有所改善。看到最近的问题为什么最好编写FUNC(常量类&值)?以在这两个声明之间的区别的详细信息。
我要强调,这应该的不的改变你的程序的行为,但如果一些关于你的编译器和/或运行时环境中有一个错误,上面的代码改变可能有助于隔离问题。
我会尝试添加1.0至数,使他们加倍。它可能试图从整数翻一番做翻译时感到困惑。
这将反映你在这工作的默认construtor。