简体   繁体   中英

problem with ceiling function in C++

I am taking an Intro to Programming class. We do not have to write actual code, but we get extra credit if we do. We use Raptor for our flowcharts, and you can generate C++ code from there, and with some modification get working code. I use Visual Studio 2008 for the code mod and build. For the midterm, I have a parking problem. The comments explain what the program does, and it builds fine except for one error: I get a message saying that 'ceiling' identifier not found on line 70. Here is the code:

// midterm part 2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

void getTime (float &time);
void getAge (int &age);
void calcFee (float time, double &fee);

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
   double discount;
   double fee;
   float time;
   int age;

   cout << "This program gets the amount of time parked and calculates the total fee for parking." <<endl;
   cout << "The rate is $1.00 for the first hour or part thereof, and $0.75 for each additional" <<endl;
   cout << "hour or part thereof.  The minimum fee is $1.00.  The maximum fee is $10.00." <<endl;
   cout << "Those over 55 years of age receive a 10% discount, except on the minimum." << endl;
   getTime(time);
   getAge(age);
   calcFee(time,fee);
   if (fee>10)
   {
      fee = 10;
   }
   else
   {
      fee = fee;
   }
   if (age>=55 && fee>1)
   {
      discount =fee*0.1;
   }
   else
   {
      discount =0;
   }
   fee =fee-discount;
   cout << "Your total fee for parking is $"<<fee<<"." << endl;
   return 0;
}

void getTime (float &time)
{
   string raptor_prompt_variable_zzyz;

   raptor_prompt_variable_zzyz ="Enter the number of hours parked.";
   cout << raptor_prompt_variable_zzyz << endl;
   cin >> time;
}

void getAge (int &age)
{
   string raptor_prompt_variable_zzyz;

   raptor_prompt_variable_zzyz ="What is the age of the driver?";
   cout << raptor_prompt_variable_zzyz << endl;
   cin >> age;
}

void calcFee (float time, double &fee)
{
   float HOURLY_RATE = 0.75;

   time = ceiling(time);
   if (time==0)
   {
      fee =0;
   }
   else
   {
      fee =1+((time-1)*HOURLY_RATE);
   }
}

I am no code expert, so if this code is not perfect, I would not know it. I can take the line "time = ceiling(time);" out of the code, and it builds fine. It just will not calculate the way it should. The instructor prefers the use of the ceiling function in this situation, but if there is another method, I would look at that. Thanks in advance for any help that can be offered.

Ceiling function is called ceil in C++. #include <cmath> to use it.

C++ has the ceil function which is exactly the same as "ceiling". To use it, you just need to import it's library, like this :

/* ceil example */
#include <stdio.h>
#include <math.h>

int main ()
{
  printf ("ceil of 2.3 is %.1lf\n", ceil (2.3) );
  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM