2017-02-13 60 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