我有一个结构中有两个数组。我试图打印出每个数组的内容,但是当我完成所有工作时,无论我打印哪个元素,都无法获得字符串和零值。这些数组结构为什么不打印任何东西?
#include <iostream>
using namespace std;
struct menuItem
{
string breakfastItem[7];
float itemPrice[7];
}dish;
void setMenu()
{
dish.breakfastItem[0]= "Plain Eggs";
dish.itemPrice[0]= 1.45;
dish.breakfastItem[1]= "Bacon and Eggs";
dish.itemPrice[1]=2.45;
dish.breakfastItem[2]="Muffin";
dish.itemPrice[2]=0.99;
dish.breakfastItem[3]="French Toast";
dish.itemPrice[3]=1.99;
dish.breakfastItem[4]="Fruit Basket";
dish.itemPrice [4]=2.46;
dish.breakfastItem[5]="Cereal";
dish.itemPrice[5]=0.69;
dish.breakfastItem[6]= "Coffee";
dish.itemPrice[6]=.50;
dish.breakfastItem[7]="Tea";
dish.itemPrice[7]=0.75;
}
int main()
{
cout << dish.breakfastItem[0];
cout << dish.itemPrice[0];
}
您应该刷新(假设您正在调试并在main的最后一个右大括号处有一个断点):'cout << dish.breakfastItem [ 0] << endl;' –
你曾经调用过'setMenu()'函数吗? –