我有一個結構中有兩個數組。我試圖打印出每個數組的內容,但是當我完成所有工作時,無論我打印哪個元素,都無法獲得字符串和零值。這些數組結構爲什麼不打印任何東西?
#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()'函數嗎? –