간단한거 질문이요..
해가빛
데이터 수를 입력받고 시간을 입력해서 요금을 계산하려고 하는데요.
3시간이내면 2달러, 24시간이면 10달러 3시간 이상이면 2달러에 시간당 0.5달러가 추가되는 식으로 계산하려고 하는데요..
제가 짠건데 요금부분이 이상하게 출력이 되는데 왜그런지 잘 모르겠네요..
그냥 C 로는 할수 있을거 같은데 C++ 로 짜려니까 잘 모르겠네요.. C랑 많이 다른거 같기도 하고..ㅠ.ㅠ
도와주세요~~
#include iostream
using std::cout;
using std::cin;
using std::endl;
using std::fixed;
#include iomanip
using std::setw;
using std::setprecision;
#define MAX 30
float calculateCharges(float []);
int main(void)
{
float hour[MAX];
int carCount = 0;
int i, data;
float charge[MAX] = {0};
float hourSum = 0;
float chargeSum = 0;
cout Enter a number of data : ;
cin data;
cout endl;
for(i = 0; i data; i++) {
cout Enter a parking time : ;
cin hour[i];
charge[i] = calculateCharges(hour);
}
cout \nCar\tHours\tCharge endl;
cout ------------------------ endl;
for(i = 0; i data; i++) {
carCount++;
cout setw(2) carCount setw(10) fixed setprecision(1)
hour[i] setw(9) fixed setprecision(2) charge[i] endl;
hourSum += hour[i];
chargeSum += charge[i];
}
cout ------------------------ endl;
cout TOTAL setw(7) fixed setprecision(1) hourSum
setw(9) fixed setprecision(2) chargeSum endl;
return 0;
}
float calculateCharges(float hour[MAX])
{
if(hour[MAX] = 3.00)
return 2.00;
else if(hour[MAX] == 24.00)
return 10.00;
else
return 2.00 + ((hour[MAX] - 3.00) * 0.50);
}
-
큰깃
아.. 알겠습니다... 답변 감사합니다~~
-
지존
hour 배열을 초기화 해 주시는 것이 낫구요.
그리고 배열에서 님이 하신 것처럼 하시려면 포인터로 접근을 하시고, 증감연산자를 이용하셔야 님께서 원하시는 값을 얻으실 수 있습니다.
이와는 다르게 미밍님께서 말씀하신 방법으로 하셔도 원하시는 값을 얻으실 수 있습니다. -
유진깡
제가 한거처럼 (hour)하면 그냥 하나만 받는거라서 결과가 2.00 만 계속 출력이 되는건가요??
-
유희
calculateCharges(hour) = calculateCharges(hour[i]) 수정
float calculateCharges(float hour[MAX]) = float calculateCharges(float a)