2017-02-13 118 views
0

所以这是一个经常混淆的主题,数组总是通过引用传递。需要帮助传递数组,并输入数组

该计划的目的是让公司弄清楚他们的小猫每周吃多少食物。因此,无论何时我发送我的食物价值,,,使用输入自己,(这些是每个小猫每周吃的食物的数量),它发回的价值,而不是我的' 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 
} 
+2

Buitlt-in(C)数组作为指针传递给函数。更改为std :: vector 并通过它们作为参考将使它更容易。 – user2672165

+0

通过'食物'数组作为kittyfood()的参数。 –

+0

我还没有学习矢量,但谢谢,是的,它现在与它作为kittyfood()的参数,即时通讯 – CaliBreeze

回答

0

问题是,kittyfood函数里面的食物是一个局部变量,而不是你在主函数中创建的数组。

该函数返回后,局部变量被破坏,并且main中的变量仍然未初始化,因此包含垃圾值。

+0

感谢您的困惑!它现在运作良好! – CaliBreeze

0

food array mainfood array 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,使您的生活更轻松,但这是与本题无关。

+0

它现在可以工作,这是一个简单的修复,但你能解释为什么吗?为什么我甚至会把它传递给那个函数是没有意义的,如果它在那个函数之后不是一件事。 – CaliBreeze

0

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

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]; 
     } 
    } 
} 
+0

谢谢,它说浮线食物[5]影响了一个参数,所以我删除了它,现在它可以工作。 – CaliBreeze