【问题标题】:C++ Array function and manipulationC++ 数组函数和操作
【发布时间】:2013-11-21 21:35:22
【问题描述】:

好的,我在课堂上有一个实验作业,要编写一个程序,该程序应该可以做很多事情: 1. 名为 convertWeight 的 void 函数将以磅(int 类型)和盎司为单位的重量转换为以千克(int 类型)和克为单位的等效重量

  1. 名为 showArray 的 void 函数接受两个参数:一个基本类型为 int 的数组参数,以及一个用于数组参数大小的按值调用参数。这个函数简单地打印出数组参数的所有元素,其中两个连续的元素由一个水平制表符分隔。

  2. 在 main() 函数定义中,执行以下操作:

一个。声明一个名为磅的 10 个整数的数组,将其前 3 个元素初始化为以下值:1、5、10,并将其余元素自动初始化为 0。

b.编写一个读取 7 个以磅为单位的重量的 for 循环,将输入的值存储到最后 7 个 数组磅的元素。

c。打印出一个提示行“The entire list of weight:”,然后调用函数 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;
}

我是这个 Array 的新手,我无法理解我的书告诉我的内容。请您指出我的程序有什么问题并告诉我为什么我知道。

这是我的错误列表:

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

任何帮助将不胜感激!

【问题讨论】:

  • pounds = pounds[a]; - 您不能将单个数组元素分配给数组变量本身。只需定义一个名称不冲突的新 int 变量即可。
  • 我是使用 for 循环的变量“a”来改变每个循环的值
  • 为什么 Stack Overflow 上的这么多 C++ 问题都有某种 War Games inspired 1980 年代风格的命令行界面?此外,尽可能使用 std::array 而不是 C 数组。

标签: c++ arrays


【解决方案1】:

c:\users\mackiller\documents\visual studio 2010\projects\lab12\lab12\lab12.cpp(30):错误 C2664:'showArray': 无法将参数 1 从 'int' 转换为 'int []' 1>
从整数类型转换为指针类型需要 reinterpret_cast、C-style cast 或 function-style cast

问题来了

showArray(pounds[10]);

这不会传递一个数组,它将尝试访问数组的第 11 个元素(并调用未定义的行为),并将其传递给 showArray(它将指针作为参数)。你想做的是:

showArray(pounds);

1>c:\users\maciller\documents\visual studio 2010\projects\lab12\lab12\lab12.cpp(34):错误 C2440:'=':不能 从 'int' 转换为 'int [10]' 1> 没有转换 到数组类型,尽管有对引用的转换或 指向数组的指针

这个问题就在这里:

pounds = pounds[a];

您正在尝试将 int 分配给数组。你根本不需要那行:

convertWeight(pounds[a], ounces, kg, grams);

这也修复了这个错误:

1>c:\users\maciller\documents\visual studio 2010\projects\lab12\lab12\lab12.cpp(35): 错误 C2664: 'convertWeight' : 无法将参数 1 从 'int [10]' 转换为 'int' 1>
没有可以进行这种转换的上下文

【讨论】:

  • 非常感谢!我现在知道了。我似乎走错了方向。还有一件事。由于它修复了我的错误,它给了我新的错误:错误 C2109:下标需要数组或指针类型它在函数 showArray 中连续 10 次给我这个错误。我似乎做错了这个程序。我尝试打印数组的方式是否有另一种方法或修复方法?
猜你喜欢
  • 2021-07-02
  • 1970-01-01
  • 2021-03-16
  • 1970-01-01
  • 2014-05-13
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多