2017-11-13 2 views
-3

私はレストランのビルプログラムを作っています。私はそれを別の価格に設定していますが、私は20ドルをすべて元に戻しています。あなたが不要なコードと用途を見ても申し訳ありませんが、私は割り当ての要求をしています。アイテムごとに同じ番号を返すのはなぜですか?

私のヘッダーファイルのコードです。

#include "stdafx.h" 
    #include "Food.h" 
    #include <iostream> 
    #include<string> 
    using namespace std; 

私は、食品アイテムの構造を常に名前と価格を持つようにしています。

Food::Food() 
{ 
    FoodItems Steak; 
    Steak.name = "Steak Meal"; 
    Steak.price = 30.00; 
    steak = Steak.name; 
    steakPrice = Steak.price; 

    FoodItems Burger; 
    Burger.name = "Burger Meal"; 
    Burger.price = 20.00; 
    burger = Burger.name; 
    burgerPrice = Burger.price; 

FoodItems Soup; 
Soup.name = "Soup"; 
Soup.price = 10.00; 
soup = Soup.name; 
soupPrice = Soup.price; 

FoodItems Salad; 
Salad.name = "Salad"; 
Salad.price = 8.00; 
salad = Salad.name; 
saladPrice = Salad.price; 

FoodItems ShrimpPlatter; 
ShrimpPlatter.name = "Shrimp Platter"; 
ShrimpPlatter.price = 20.00; 
shrimpPlatter = ShrimpPlatter.name; 
shrimpPlatterPrice = ShrimpPlatter.price; 

FoodItems ChickenMeal; 
ChickenMeal.name = "Chicken Meal"; 
ChickenMeal.price = 20.00; 
chickenMeal = ChickenMeal.name; 
chickenMealPrice = ChickenMeal.price; 

} 

ここでは、私が食品をセットする場所です。ここで

string Food::getSteak() const 
{ 
    return steak; 
} 

double Food::getSteakPrice() const 
{ 
    return steakPrice; 
} 

string Food::getBurger() const 
{ 
    return burger; 
} 

double Food::getBurgerPrice() const 
{ 
    return burgerPrice; 
} 

string Food::getSoup() const 
{ 
    return soup; 
} 

double Food::getSoupPrice() const 
{ 
    return soupPrice; 
} 

string Food::getSalad() const 
{ 
    return salad; 
} 

double Food::getSaladPrice() const 
{ 
    return saladPrice; 
} 

string Food::getShrimpPlatter() const 
{ 
    return shrimpPlatter; 
} 

double Food::getShrimpPlatterPrice() const 
{ 
return shrimpPlatterPrice; 
} 

string Food::getChickenMeal() const 
{ 
    return chickenMeal; 
} 

double Food::getChickenMealPrice() const 
{ 
    return chickenMealPrice; 
} 

は、ここに私のメニュー機能

 void Food::foodMenu() 
{ 
    int amountOfFoodItems; 
    cout << "How many food items do you wish to order?" << endl; 
    cin >> amountOfFoodItems; 
    string food[10]; 
    double price[10]; 

for (int i = 0, b = 0; i < amountOfFoodItems && b < amountOfFoodItems; i++, b++) 
{ 
    int selection; 

    cout << "Selections" << endl; 
    cout << "1 for Steak Meal" << endl; 
    cout << "2 for Burger meal" << endl; 
    cout << "3 for Soup" << endl; 
    cout << "4 for Salad" << endl; 
    cout << "5 for Shrimp Platter" << endl; 
    cout << "6 for Chicken Meal" << endl; 
    cin >> selection; 

    if (selection == 1) 
     food[i] = getSteak(); 
    price[b] = getSteakPrice(); 


    if (selection == 2) 
     food[i] = getBurger(); 
    price[b] = getBurgerPrice(); 

    if (selection == 3) 
     food[i] = getSoup(); 
    price[b] = getSoupPrice(); 

    if (selection == 4) 
     food[i] = getSalad(); 
    price[b] == getSaladPrice(); 

    if (selection == 5) 
     food[i] = getShrimpPlatter(); 
    price[b] = getShrimpPlatterPrice(); 

    if (selection == 6) 
     food[i] = getChickenMeal(); 
    price[b] = getChickenMealPrice(); 
} 

printFoodContents(amountOfFoodItems, food, price); 
} 

ある

void Food::printFoodContents(int amountOfFoodItems, string food[], double 
price[]) 
{ 
    for (int i = 0, b = 0; i < amountOfFoodItems && b < amountOfFoodItems; i++, 
b++) 
    { 
     cout << food[i] << " " << price[b] << endl; 
    } 
} 

が再び私は何のための出力画面に20.00を取得しています選んだのだった項目のすべてを表示する関数です私が選ぶ食べ物。選ばれた食べ物の名前は価格だけではありません。助けて!ありがとうございました!!!

+0

価格を必要とする[B] = ()する必要がありますif文でも同様です。今書かれているように、getChickenMealPrice()は最後に呼び出されるため、常に設定されます。 – NendoTaka

+0

関連:https://stackoverflow.com/questions/359732/why-is-it-considered-a-bad-practice-to-omit-curly-braces?lq=1 – Barmar

+1

http://idownvotedbecau.se/nodebugging /あなたの例は[MCVE]から遠いです。次回にお尋ねになる前に、そのことを念頭においてください。 – user0042

答えて

1

あなたの条件文は右ではありません。

if (selection == 1) 
    food[i] = getSteak(); 
price[b] = getSteakPrice(); 

は次のようになります。以上の2行の

if (selection == 1) { 
    food[i] = getSteak(); 
    price[b] = getSteakPrice(); 
} 

条件文は、かっこ

関連する問題