我是编程新手,最近我分配了一个非常难的代码。我一直试图完成它,但我很难过。
我的函数原型是
int getNumAccidents(int);
我的主要电话是:
int north;
int south;
int east;
int west;
int central;
cout << "Please enter the value for North\n";
getNumAccidents(north);
cout << "the number of accidents in the north are:\n" << north;
break;
我的功能是:
int getNumAccidents(int value)
{
cin >> value;
return value;
}
它编译得很好,但是当我输入数据时我得到了意想不到的回报。我输入32,我的功能返回4591493 .....为什么?
答案 0 :(得分:3)
试试这个
int north;
int south;
int east;
int west;
int central;
cout << "Please enter the value for North\n";
north=getNumAccidents(north);
cout << "the number of accidents in the north are:\n" << north;
break;
答案 1 :(得分:2)
使用
void getNumAccidents(int &value)
{
cin >> value;
}
因为您需要将输入返回到main。
答案 2 :(得分:0)
还有另一种方式。
void getNumAccidents(int *value)
{
if (value)
{
cin >> *value;
}
}
像这样使用
int value = 0;
getNumberAccidents(&value);