好的,我有一个实验室作业,可以创建一个程序,假设它做了很多事情: 1.名为convertWeight的void函数将以磅为单位的权重(类型为int)和盎司转换为以千克为单位的等效权重(类型为int)和克
一个名为showArray的void函数,它接受两个参数:一个基本类型为int的数组参数,以及一个用于数组参数大小的call-by-value参数。此函数只打印出数组参数的所有元素,其中两个连续元素由水平制表符分隔。
在main()函数定义中,执行以下操作:
一个。声明一个名为磅为10的整数数组,将其前3个元素初始化为以下值:1,5,10,并将其余元素自动初始化为0.
湾写一个for循环,以磅为单位读取7个权重,将输入的值存储到最后7个 阵列的元素磅。
℃。打印出一个提示行“整个权重列表:”然后调用showArray函数显示整个数组磅数。
d。写另一个for循环,它调用函数convertWeight将数组磅中给出的每个重量换算为千克和克的等效重量。
这就是我提出的:
#include <iostream>
using namespace std;
void convertWeight(int pounds, double ounces, int& kg, double& grams);
//Preconditions: parameters pounds and ounces are nonnegative numbers, representing a weight in pounds and ounces
//Postcondition: parameters kg and grams will be set to values of the equivalent weight in kilograms and grams
void showArray(int pounds[10]);
int main()
{
int pounds[10]={1, 5, 10}, i, a, kg;
double ounces, grams;
cout << "Enter 7 additional weights in pounds: \n";
cin >> pounds[3];
for(i = 4; i < 10; i++)
{
cin >> pounds[i];
}
cout << "The entire list of weights: \n";
showArray(pounds[10]);
for(a = 0; a < 10; a++)
{
pounds = pounds[a];
convertWeight(pounds, ounces, kg, grams);
cout << pounds[a] << " pounds = " << kg << " kgs and " << grams << " grams";
}
return 0;
}
void showArray(int pounds[10])
{
cout << pounds[0] << " " << pounds[1] << " " << pounds[2] << " " << pounds[3] << " " << pounds[4] << " " << pounds[5] << " "
<< pounds[6] << " " << pounds[7] << " " << pounds[8] << " " << pounds[9] << " " << pounds[10] << " " ;
}
//Do NOT modify this function definition
void convertWeight(int pounds, double ounces, int& kg, double& grams)
{
const double KGS_PER_POUND = 0.45359237;
const double OUNCES_PER_POUND = 16.0;
const double GRAMS_PER_KG = 1000.0;
double totalKgs;
totalKgs = (pounds + ounces/OUNCES_PER_POUND)*KGS_PER_POUND;
kg = static_cast<int>(totalKgs);
grams = (totalKgs - kg)*GRAMS_PER_KG;
}
我是这个阵列的新手,我无法得到我的书告诉我的内容。能不能指出我的计划有什么问题,并告诉我为什么这么认识。
这是我的错误列表:
1>c:\users\mackiller\documents\visual studio 2010\projects\lab12\lab12\lab12.cpp(30): error C2664: 'showArray' : cannot convert parameter 1 from 'int' to 'int []'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\mackiller\documents\visual studio 2010\projects\lab12\lab12\lab12.cpp(34): error C2440: '=' : cannot convert from 'int' to 'int [10]'
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
1>c:\users\mackiller\documents\visual studio 2010\projects\lab12\lab12\lab12.cpp(35): error C2664: 'convertWeight' : cannot convert parameter 1 from 'int [10]' to 'int'
1> There is no context in which this conversion is possible
任何帮助都会受到赞赏!
答案 0 :(得分:2)
c:\ users \ mackiller \ documents \ visual studio 2010 \ projects \ lab12 \ lab12 \ lab12.cpp(30):错误C2664:'showArray': 不能将参数1从'int'转换为'int []'1&gt;
从整数类型转换为指针类型需要 reinterpret_cast,C风格的演员表或函数式演员
问题出在这里
showArray(pounds[10]);
这将不会传递数组,它将尝试访问数组的第11个元素(并调用未定义的行为),并将其传递给showArray
(它将指针作为参数)。你想做的是:
showArray(pounds);
1&gt; c:\ users \ mackiller \ documents \ visual studio 2010 \ projects \ lab12 \ lab12 \ lab12.cpp(34):错误C2440:'=':不能 从'int'转换为'int [10]'1&gt;没有转换 到数组类型,虽然有转换引用或 指向数组的指针
这个问题在这里:
pounds = pounds[a];
您正在尝试将int
分配给数组。你根本不需要那条线:
convertWeight(pounds[a], ounces, kg, grams);
这也解决了这个错误:
1&gt; c:\ users \ mackiller \ documents \ visual studio 2010 \ projects \ lab12 \ lab12 \ lab12.cpp(35):错误C2664:'convertWeight' :无法将参数1从'int [10]'转换为'int'1&gt;
没有可以进行此转换的上下文