C++ 需要帮助传递数组和输入数组吗
这是一个经常被混淆的主题,数组总是通过引用传递 这个计划的目的是让一家公司计算出他们的小猫每周吃多少食物 所以这个程序运行得很好,但是每当我去发送我的食物值,使用输入本身,(这些是每只小猫每周吃的食物量),它返回的值不是我想要传递的,它们只是记忆中的随机数,我假设这是因为我没有返回值,但我知道这些值是通过引用传递的,不需要返回值 请帮忙C++ 需要帮助传递数组和输入数组吗,c++,arrays,pass-by-reference,C++,Arrays,Pass By Reference,这是一个经常被混淆的主题,数组总是通过引用传递 这个计划的目的是让一家公司计算出他们的小猫每周吃多少食物 所以这个程序运行得很好,但是每当我去发送我的食物值,使用输入本身,(这些是每只小猫每周吃的食物量),它返回的值不是我想要传递的,它们只是记忆中的随机数,我假设这是因为我没有返回值,但我知道这些值是通过引用传递的,不需要返回值 请帮忙 #include <iostream> using namespace std; void kittyfood(string kittyNames
#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
}
#包括
使用名称空间std;
void kittyfood(字符串kittyNames[],int-sizeOfArray)//kittyfood函数的原型
无效报告(字符串kittyNames[],整数大小Farray,浮动食品[])//报表功能原型
int main()
{
字符串名称[5]={“Fluffy”、“sleeky”、“Moonie”、“Stuffy”、“Oriana”};//为数组设置猫名
float food[5];//包含5个元素的食物量的float数组
kittyfood(names,5);//调用too kittyfood函数,传递kitty名称和数组大小
report(name,5,food);//使用kitty名称、数组大小和food数量调用report函数
返回0;
}
void kittyfood(字符串kittyNames[],int-sizeOfArray)
{
漂浮食物[5];
对于(int i=0;i而言,问题在于kittyfood函数中的食物是一个局部变量,与您在主函数中创建的数组不同
局部变量在函数返回后被销毁,main上的仍然未初始化,因此包含垃圾值。main
中的food
数组与kittyfood
中的food
数组不同。在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::vector
或std::array
来简化你的生活,但这不是问题。你可以从kittyfood
函数的标准输入中读取食物量(float food[5];
variable),并且永远不要使用它。我想这会对你有所帮助
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];
}
}
}
void kittyfood(字符串kittyNames[],int-sizeOfArray,float food[])
{
对于(int i=0;i在(C)中的数组作为指针传递给函数。更改为std::vector并将它们作为引用传递将使您更容易。将food
array作为kittyfood()的参数传递。我还没有了解向量,但谢谢,是的,它现在可以作为kittyfood()的参数使用,我对此有点困惑,谢谢,它说行float food[5]隐藏了一个参数,所以我删除了它,现在它工作了。现在它工作了,这是一个简单的修复,但你能解释一下为什么吗?我甚至不明白为什么我会将它传递给那个函数,如果它在那个函数之后才发生。