0

Okay I have a lab assignment for class to make a program that is suppose to do a number of things: 1. A void function named convertWeight converts a weight in pounds (type int) and ounces to the equivalent weight in kilograms (type int) and grams

  1. A void function named showArray that takes two parameters: one array parameter of base type int, and one call-by-value parameter for the size of the array parameter. This function simply prints out all elements of the array parameter, where two consecutive elements are separated by a horizontal tab.

  2. In the main() function definition, do the followings:

a. declare an array named pounds of 10 integers, initializing its first 3 elements to the following values: 1, 5, 10, and auto-initializing the remaining elements to 0.

b. write a for-loop that reads in 7 weights in pounds, storing the entered values to the last 7 elements of the array pounds.

c. print out one prompt line “The entire list of weight:” and then call the function showArray to display the entire array pounds.

d. write another for-loop that calls the function convertWeight to convert each weight in pounds given in the array pounds to the equivalent weight in kilograms and grams.

This what I came up with:

#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;
}

I am new to this Array thing and I cant get what my book is telling me. Could you please point out what it is that is wrong with my program and tell me why so I know.

here is my error list:

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

Any help givin will be appreciated!

3
  • 1
    pounds = pounds[a]; - You can't assign a single array element into the array variable itself. Just define a new int variable with a non conflicting name. Commented Nov 21, 2013 at 21:42
  • I was to use the for loop's variable "a" that changes value every single loop Commented Nov 21, 2013 at 21:43
  • Why do so many of the C++ questions on Stack Overflow have some kind of War Games inspired 1980s-style command-line interface? Also, use std::array and not C arrays whenever possible. Commented Nov 21, 2013 at 21:49

1 Answer 1

2

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

The problem is here

showArray(pounds[10]);

That will not pass an array, it will attempt to access the 11th element of the array (and invoke undefined behavior), and pass that to showArray (which takes a pointer as an argument). What you want to do there is:

showArray(pounds);

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

This problem is here:

pounds = pounds[a];

You are attempting to assign an int to an array. You don't need that line at all:

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

Which also fixes this error:

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I understand now. I seem to be heading in the wrong direction. Also one more thing. Since it fixed my errors it gave me new ones: error C2109: subscript requires array or pointer type it gave me this error 10 times in a row in the function showArray. I seem to have done this program wrong. Is there another way or a fix to the way im trying to print out the arrays?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.