Having trouble with c++ integers and math
Having trouble with c++ integers and math
So its a Friday, Ive been working this app and my head is about to explode. I can't find the problem anywhere!!! I am a beginner coder so I am hoping the gods of stackoverflow can guide me in the right direction or provide some feedback!! :]
This console app is just a simple parking time ticket. The code complies fine with no errors. But my math is all messed up!
Here is a sample result: With time entered as 3:50 and exit 5:29 with vehicle as T.
TIME-IN -858993460:858993460 TIME-OUT -858933460:-858993460 PARKING TIME 0:-858993460 TOTAL CHARGE -214748352.00
And here is my code
#include //used for cout/cin #include //used to manipulate data void getData(int* ehour, int* emin, int* exhour, int* exmin); void rate(int exhour, int exmin, int ehour, int emin, int* thour, int* tmin, int* round); void charge(char* vehic, float* rate1, float* rate2, int ehour); void result(int exhour, int exmin, int ehour, int emin, int thour, float rate1, float rate2, int round, float total); int main(void) { char vehic; int ehour; int emin; int exhour; int exmin; int thour; int tmin; int round; float rate1; float rate2; float total; getData(&ehour, &emin, &exhour, &exmin); rate(exhour, exmin, ehour, emin, &thour, &tmin, &round); charge(&vehic, &rate1, &rate2, ehour); total= rate1 + rate2; result( exhour, exmin, ehour, emin, thour, rate1, rate2, round, total); return 0; } void getData(int* ehour, int* emin, int* exhour, int* exmin) { char v; printf("Enter C for car, B for bus, T for truck: "); scanf("%c", &v); printf("\nHour vehicle entered 0-24: "); scanf("%d", &ehour); printf("\nMinute vehicle entered 0-60: "); scanf("%d", &emin); printf("\nHour vehicle exited 0-24: "); scanf("%d", &exhour); printf("\nMinute vehicle exited 0-60: "); scanf("%d", &exmin); return; } void rate(int exhour, int exmin, int ehour, int emin, int* thour, int* tmin, int* round) { if(emin < exmin) { emin= emin + 60; exhour= exhour - 1; } *thour = ehour - exhour; *tmin = emin - exmin; if ((*tmin > 0 && *tmin <= 60)) { *thour = *thour + 1; *round = *tmin * 0; } return; } void charge(char* vehic, float* rate1, float* rate2, int ehour) { switch (*vehic) { case 'c': if (ehour <= 3) { *rate1 = 0.00; if (ehour > 3) *rate2 = 1.25 * (ehour - 3); } break; case 'b': if (ehour <= 2) { *rate1 = 2.00 * ehour; if (ehour > 2) *rate2 = 2.50 * (ehour - 2); } break; case 't': if (ehour <= 1) { *rate1 = 3.75 * ehour; if (ehour > 1) *rate2 = 4.50 * (ehour - 1); } break; } return; } void result(int exhour, int exmin, int ehour, int emin, int thour, float rate1, float rate2, int round, float total) { printf("\n\t\t PARKING LOT CHARGE \t\t\n"); printf("\nType of vehicle: Car or Bus or Truck"); printf("\nTIME-IN\t\t %d:%d", ehour, emin); printf("\nTIME-OUT\t\t %d:%d", exhour, exmin); printf("\n\t\t\t --------"); printf("\nPARKING TIME\t\t %d:%d", thour, round); printf("\n\t\t\t --------"); total= rate1 + rate2; printf("\nTOTAL CHARGE\t\t %4.2f\n\n", total); return; }
I am sorry this is alot of code! I am just so puzzled!!! Are my ints not formatted correctly? Is the math wrong?
Answer by Michael Krelin - hacker for Having trouble with c++ integers and math
One thing I noticed ? You pass &ehour
and friends, which is a pointer to pointer to your scanf
call (you can start by removing &
from scanf
calls when ehour
is int*
).
Answer by sehe for Having trouble with c++ integers and math
char v; printf("Enter C for car, B for bus, T for truck: "); scanf("%c", &v); printf("\nHour vehicle entered 0-24: "); scanf("%d", ehour); printf("\nMinute vehicle entered 0-60: "); scanf("%d", emin); printf("\nHour vehicle exited 0-24: "); scanf("%d", exhour); printf("\nMinute vehicle exited 0-60: "); scanf("%d", exmin);
You took the address of the parameters, which were already pointers.
As a general finger exercise, here's what you could do in more typical C++ style:
///////////////////////////////////// // timepoint classes (booking.hpp) struct timepoint { int hour, minute; timepoint normalized() const; int totalMinutes () const; int roundedHours () const; timepoint operator- (timepoint const& rhs) const; }; struct booking_t { char vehicle; timepoint enter, exit; timepoint parked() const { return exit - enter; } }; ///////////////////////////////////// // main program (main.cpp) booking_t inputData(); void displayBill(booking_t const& booking); int main(void) { auto booking = inputData(); displayBill(booking); } ///////////////////////////////////// // timepoint methods (booking.cpp) timepoint timepoint::normalized() const { timepoint tmp { (hour + minute/60) % 24, minute % 60 }; while (tmp.minute < 0) tmp.hour--, tmp.minute+=60; while (tmp.hour < 0) tmp.hour+=24; return tmp; } int timepoint::roundedHours() const { return (totalMinutes()-1) / 60 + 1; // TODO check rounding logic } int timepoint::totalMinutes() const { return hour*60 + minute; } timepoint timepoint::operator-(timepoint const& rhs) const { return timepoint { 0, totalMinutes() - rhs.totalMinutes() } .normalized(); } #include //used for cout/cin timepoint getTime(std::string label) { int hour, minute; std::cout << "\nHour " << label << " 0-24: "; std::cin >> hour; std::cout << "\nMinute " << label << " 0-60: "; std::cin >> minute; return { hour, minute }; } ///////////////////////////////////// // global functions - input booking_t inputData() { std::cout << "Enter C for car, B for bus, T for truck: "; char v; std::cin >> v; auto entered = getTime("vehicle entered"); auto exited = getTime("vehicle exited"); return { v, entered.normalized(), exited.normalized() }; } ///////////////////////////////////// // calculation + billing #include #include //used to manipulate data #include
Answer by F?lix Galindo Allué for Having trouble with c++ integers and math
Take out the & in scanf! They are already pointers!
Answer by john for Having trouble with c++ integers and math
The other error in your code is that the variable vehic
never gets a value. It looks like you meant to give it a value in getData
but messed that up somehow.
Answer by Kerrek SB for Having trouble with c++ integers and math
I can't find much redeeming about your original code, so this isn't an answer per se, but perhaps you might find it interesting how I would write code for your problem in "pure C++":
#include #include #include bool parse_time(std::string const & s, int & t) { int h, m; char c; std::istringstream iss(s); if (iss >> h >> c >> m >> std::ws && c == ':' && iss.get() == EOF) { t = 60 * h + m; return true; } return false; } int main() { int t_in, t_out; std::string line; if (!(std::cout << "Enter arrival time: " && std::getline(std::cin, line) && parse_time(line, t_in) && std::cout << "Enter departure time: " && std::getline(std::cin, line) && parse_time(line, t_out))) { std::cerr << "Input error! Aborting.\n"; return 0; } std::cout << "You spent " << t_out - t_in << " minutes.\n"; }
Here's a typical session:
Enter arrival time: 5:14 Enter departure time: 8:41 You spent 207 minutes.
Answer by Yakk for Having trouble with c++ integers and math
Rule 1: Declare variables near to where you first use them. And initialize all variables when you create them:
int main(void) { int ehour = 0; int emin = 0; int exhour = 0; int exmin = 0; getData(&ehour, &emin, &exhour, &exmin); int thour = 0; int tmin = 0; int round = 0; rate(exhour, exmin, ehour, emin, &thour, &tmin, &round); char vehic = 0; float rate1 = 0; float rate2 = 0; charge(&vehic, &rate1, &rate2, ehour); float total = 0; total= rate1 + rate2; result( exhour, exmin, ehour, emin, thour, rate1, rate2, round, total); return 0; }
Now see that float total
? Move the declaration and initialization of the variable to the same line:
float total = rate1 + rate2;
Rule 2: Don't use pointers if you don't need to. If you have an integer you want to pass both into and out of a function, make the parameter a reference parameter like this:
void getData(int& ehour, int& emin, int& exhour, int& exmin)
In main:
getData(ehour, emin, exhour, exmin);
In getData:
void getData(int& ehour, int& emin, int& exhour, int& exmin) { char v; printf("Enter C for car, B for bus, T for truck: "); scanf("%c", &v); printf("\nHour vehicle entered 0-24: "); scanf("%d", &ehour); printf("\nMinute vehicle entered 0-60: "); scanf("%d", &emin); printf("\nHour vehicle exited 0-24: "); scanf("%d", &exhour); printf("\nMinute vehicle exited 0-60: "); scanf("%d", &exmin); return; }
Now, I spotted your first screwup. You where reading not to the integers, but to the pointers to the integers. Now that I have made them references (think "aliases" or for the variables passed in), the &
operator gets a pointer to the integer, not a pointer to the pointer to the integer!
The next thing you want to do is not to use scanf. Scanf is hard to use. Why use something hard to use?
Step 3: Your program should help you debug it.
When you read something from input, repeat it back out to make sure you read it right. Ie, after you call getData, the very next thing you should do is repeat out what you read. Eventually when your code is solid you can remove that "repeat it back", but when developing a program this kind of feedback is essential.
See Kerrek SB for better ways to read instead of using scanf
.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment