2013-10-18 61 views
0

這是一個基本的C++編程類的程序。變量foodPrice和bisPrice給我使用時沒有被初始化的錯誤,並且它們不能通過引用傳遞。我不知道如何初始化它們。請幫忙。由於該變量正在被使用且未被初始化

using namespace std; 

void getBiscuits(string &, double &); 
void getDryFood(string & , double &); 
void processData(double, double , double & , double & , double & , double , double); 
void displayOrder(string , string , string , double , double , double , double , double , double , double , double , double , double); 

const double foodCost = 30; 
const double bisCost = 10; 
const double salesTax = .07; 

int main() 
{ 
    //declare variables 
    string name; 
    double numBis; 
    string bis; 
    string dryFood; 
    double foodNum; 
    double bisPrice; 
    double foodPrice; 
    double tax = 0; 
    double total = 0; 
    double subTotal = 0; 

    //get data from user 
    cout << "Welcome to The Big Dog's Food Panty" << endl; 
    cout << "Please tell us your first and last name?" << endl; 
    getline(cin, name); 
    cout << "Thanks " << name << endl; 
    system ("cls"); 
    getBiscuits(bis , numBis) ; 
    getDryFood(dryFood , foodNum) ; 
    processData(numBis , foodNum , subTotal , tax , total , bisPrice , foodPrice) ; 
    displayOrder(name , bis , dryFood , bisPrice , numBis , bisCost , foodPrice , foodNum , foodCost , subTotal , tax , salesTax , total) ; 

    // stop to let the user read the far 
    system ("pause"); 
} 

void getBiscuits(string &bis , double &numBis) 
{ 
    string dummyVariable; 

    cout << "Would you like the Spicy Chicken or BBQ Beef biscuits?" << endl; 
    getline(cin, bis); 
    system ("cls"); 
    cout << "How many five lb bags of " << bis << " would you like?" << endl; 
    cin >> numBis; 
    getline(cin, dummyVariable); 
    system ("cls"); 
} 

void getDryFood(string &dryFood , double &foodNum) 
{ 
    cout << "Would you like Salmon and Peas or Chicken and Rice dry food?" << endl; 
    getline(cin, dryFood); 
    system ("cls"); 
    cout << "How many 28 lbs bags of " << dryFood << " would you like?" << endl; 
    cin >> foodNum; 
    system ("cls"); 
} 

void processData(double numBis , double foodNum , double &subTotal ,double &tax , double &total , double bisPrice , double foodPrice) 
{ 
    //calculate 
    bisPrice = numBis * bisCost; 
    foodPrice = foodNum * foodCost; 
    subTotal = bisPrice + foodPrice; 
    tax = subTotal * salesTax; 
    total = subTotal + tax; 
} 

void displayOrder(string name , string bis , string dryFood , double bisPrice , double numBis , double bisCost , double foodPrice , double foodNum , double foodCost , double subTotal ,double tax , double salesTax , double total) 
{ 
    // display cost 
    cout << "Thanks " << name << " for your order!" << endl; 
    cout << numBis << setprecision(2) << fixed << " five lbs bags of " << bis << " at $10.00 each is $" << bisPrice << endl; 
    cout << setprecision(0) << fixed <<foodNum << setprecision(2) << fixed << " 28 lbs bags of " << dryFood << " at $30.00 each is $" << foodPrice << endl; 
    cout << setprecision(2) << fixed << "Your subtotal is $" << subTotal << endl; 
    cout << setprecision(2) << fixed << "Your sales tax is $" << tax << endl; 
    cout << setprecision(2) << fixed << "Your total is $" << total << endl; 
} 
+1

在整個程序中甚至沒有*單行錯誤檢查。 – WhozCraig

回答

0

只是做到這一點,錯誤會:

double bisPrice = 0; 
double foodPrice = 0; 

我認爲這應該是很明顯你,你沒有在聲明:)

而且提供任何價值是不要忘了這些,以及:

string name = ""; 
double numBis = 0; 
string bis = ""; 
string dryFood = ""; 
double foodNum = 0; 

變量函數具有自動存儲,因此他們沒有t默認初始化爲一個值。

0

您沒有定義它們的初始值。

double bisPrice; 
double foodPrice; 

更改爲:

double bisPrice = 0; 
double foodPrice = 0; 
0

要解決編譯器警告,初始化變量:

double bisPrice = 0; 
double foodPrice = 0; 

在C和C++的基本值類型不具有initial value

當聲明常規局部變量時,其值默認爲不確定。但是你可能想要一個變量在聲明它的同時存儲一個具體值。爲了做到這一點,你可以初始化變量。

對象將使用其默認構造函數進行初始化,這就是爲什麼對於string變量沒有相同錯誤的原因。

相關問題