需要帮助传递数组和输入数组

时间:2017-02-13 05:05:28

标签: c++ arrays pass-by-reference

因此,这是一个经常混淆的主题,数组总是通过引用传递。

这个计划的目的是让一家公司弄清楚他们的小猫每周吃多少食物。

因此,每当我发送食物价值时,该程序运作良好,即使用输入本身,(这些是每只小猫每周食用的食物量),它发回的值不是我的#39 ; m试图传递,它们只是记忆中的随机数字,我认为它是因为我没有返回值,但我读到这些值是通过引用传递的,并且您不需要返回值,

请帮忙!

#include <iostream>

using namespace std;
void kittyfood(string kittyNames[], int sizeOfArray);                 //prototype for kittyfood function
void report(string kittyNames[], int sizeOfArray, float food[]);        //prototype for report function

int main()
{
    string names[5]={"Fluffy","Sneaky","Moonie","Stuffy","Oriana"};   //set cat names to the array
    float food[5];                                                  //float array for food amounts with 5 elements
    kittyfood(names,5);                            //call too kittyfood function passing the kitty names and the size of array
    report(names,5,food);                //call to report function with kitty names, size of array, and ammount of foods
    return 0;
}

void kittyfood(string kittyNames[], int sizeOfArray)
{   
    float food[5];
    for (int i=0;i<sizeOfArray; i++)                //loop to go through cat names and get the amounts of food they eat
    {
        cout << "Please enter the amount of food in pounds "<< kittyNames[i] << " eats weekly\n";  //prompt user food eaten
        cin >> food[i];           //user input food eaten
        while (food[i]<0)
        {
            cout << "This cannot be a negative ammount \n";         //input validation
            cin >> food[i];
        }
    }
}

void report(string kittyNames[], int sizeOfArray, float food[])
{
    float smallest, largest;                    //declaration for the smallest and largest amount
    string smallestName, largestName;           //declaration for the cat that eats the most or least
    smallest=largest=food[0];                   //initialize the smallest and largest at the first array food value
    smallestName=largestName=kittyNames[0];     //initialize for the smallest and largest eaten for the first cat name in array
    float totalFood;                    //declaration
    totalFood=0;                        //initialization
    for (int i=0;i<sizeOfArray; i++)        //loop to go through cats and  display their name and amount of food they ate
    {
        cout << kittyNames[i] << " eats "<< food[i]<< " pounds of food weekly \n";
        if (smallest > food[i])
        {
            smallest = food[i];                         //if the food amount is less than the original value then replace it
            smallestName=kittyNames[i];                 //change the name of the cat to the new cats name
        }

        if (largest < food[i])
        {
            smallest = food[i];                     //if the food amount is more than the original then replace it
            largestName = kittyNames[i];            //change the name of the cat to thew new cats name
        }
        totalFood+=food[i];     //keep adding the amounts of food to the total each time the loop goes through
    }
    cout << endl<<smallestName << " ate the least amount of food at " << smallest << " pounds \n";  //display the lowest cats name + ammount
    cout << largestName << " ate the most amount of food at " << largest << " pounds \n";           //display the largest cats name + ammount
    cout << "The total amount of food eaten weekly is "<<totalFood<<endl;      //display total food eaten
}

3 个答案:

答案 0 :(得分:0)

问题是kittyfood函数中的食物是局部变量,与您在主函数中创建的数组不同。

在函数返回后,局部变量被销毁,而main上的那个仍然未初始化,因此包含垃圾值。

答案 1 :(得分:0)

food中的main数组与food中的kittyfood数组不同。在kittyfood中,您使用某些值填充函数数组的本地。因此,main中的数组具有未定义的内容。

您也可以将food数组传递给kittyfood,如下所示:

int main()
{
    string names[5]={"Fluffy","Sneaky","Moonie","Stuffy","Oriana"};   
    float food[5];  
    kittyfood(names, food, 5);       
}

void kittyfood(string kittyNames[], float food[], int sizeOfArray)
{   
   // populate the food array
}

或者您可以使用std::vectorstd::array让您的生活更轻松,但这不是问题所在。

答案 2 :(得分:0)

您从float food[5];函数中的标准输入读取了食物量(kittyfood变量),从不使用它。我想这会对你有所帮助。

void kittyfood(string kittyNames[], int sizeOfArray, float food[])
{   
    for (int i=0;i<sizeOfArray; i++)                //loop to go through cat names and get the amounts of food they eat
    {
        cout << "Please enter the amount of food in pounds "<< kittyNames[i] << " eats weekly\n";  //prompt user food eaten
        cin >> food[i];           //user input food eaten
        while (food[i]<0)
        {
            cout << "This cannot be a negative ammount \n";         //input validation
            cin >> food[i];
        }
    }
}