2012-12-17 2 views
0

이 상황에 맞는 프로그램을 작성하고 있습니다 : 두 명의 자녀가 있습니다. 그들은 아이스크림이나 캔디에 돈을 쓸지 여부를 결정하기 위해 돈을 모으는 것입니다. $ 20 이상이면 아이스크림 ($ 1.50)에 모두 사용하십시오. 그렇지 않으면 사탕 (50 달러)에 모든 것을 쓰십시오. 구매할 아이스크림이나 사탕의 양을 표시하십시오.내 프로그램을 if/else 문으로 계속 시도하려고합니다. C++

I've written my code here: 

#include<iostream> 
#include <iomanip> 
using namespace std; 

//function prototypes 
void getFirstSec(double &, double &); 
double calcTotal(double, double); 

int main() 
{ 

//declare constants and variables 
double firstAmount = 0.0; 
double secondAmount = 0.0; 
double totalAmount = 0.0; 
const double iceCream = 1.5; 
const double candy = 0.5; 
double iceCreamCash; 
double candyCash; 
int iceCreamCount = 0; 
int candyCount = 0; 


//decides whether to buy ice cream or candy 
getFirstSec(firstAmount, secondAmount); 
totalAmount = calcTotal(firstAmount, secondAmount); 

if (totalAmount > 20) 
{ 
     iceCreamCash = totalAmount; 
     while (iceCreamCash >= 0) 
     { 
       iceCreamCash = totalAmount - iceCream; 
       iceCreamCount += 1; 
     } 
     cout << "Amount of ice cream purchased : " << iceCreamCount; 
} 
else 
{ 
     candyCash = totalAmount; 
     while (candyCash >= 0) 
     { 
       candyCash = totalAmount - candy; 
       candyCount += 1; 
     } 
     cout << "Amount of candy purchased : " << candyCount; 
} 
} 
// void function that asks for first and second amount 
void getFirstSec(double & firstAmount, double & secondAmount) 

{ 
cout << "First amount of Cash: $"; 
cin >> firstAmount; 
cout << "Second amount of Cash: $"; 
cin >> secondAmount; 
return; 
} 
// calculates and returns the total amount 
double calcTotal(double firstAmount , double secondAmount) 
{ 
    return firstAmount + secondAmount; 
} 

I 입력 제 1 및 제 2 양,하지만 경우/다른 부분에 계속되지 않습니다. 누군가가 여기에 문제가 무엇인지 나를 깨우칠 수 있습니까? 감사!

답변

6
while (iceCreamCash >= 0) 
    { 
      iceCreamCash = totalAmount - iceCream; 
      iceCreamCount += 1; 
    } 

이 루프는 절대 종료되지 않습니다. 루프의 아무 것도 루프의 반복마다 iceCreamCash을 감소시키지 않습니다. 아마 당신은 다음을 의미했습니다 :

while (iceCreamCash >= 0) 
    { 
      iceCreamCash = totalAmount - iceCream * iceCreamCount; 
      iceCreamCount += 1; 
    } 
+0

... 그리고 * loop *도없이 계산 될 수 있습니다. – Nawaz

+1

중요한 교훈 : 코드 조각을 남기지 않는다는 것은 결코 시작하지 않았다는 것을 결코 의미하지 마십시오. 실제로 "if/else"부분에 도달했습니다. –

+0

또는'iceCreamCash - = iceCream; '을 사용할 수 있습니다. – DanS

관련 문제