2013-10-18 5 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; 

변수의 함수는 자동 저장을 가지므로 아무 것도 없습니다. 기본적으로 값으로 초기화됩니다.

0

초기 값을 정의하지 않았습니다.

double bisPrice = 0; 
double foodPrice = 0; 
0

, 당신의 컴파일러 경고를 수정 변수를 초기화하려면 :에

double bisPrice; 
double foodPrice; 

변경 C 및 C++ 기본 값 유형에

double bisPrice = 0; 
double foodPrice = 0; 

initial value이 없습니다.

일반 로컬 변수를 선언 할 때 해당 값은 기본적으로 결정되지 않습니다. 그러나 변수가 선언 된 바로 그 순간에 구체적인 값을 저장하기를 원할 수 있습니다. 이를 수행하기 위해 변수를 초기화 할 수 있습니다.

개체는 기본 생성자를 사용하여 초기화되므로 string 변수에 대해 동일한 오류가 발생하지 않습니다.

관련 문제