visual c++ - How do I remove the extra decimals when I divide change by 25 I get 3.48 and I need a whole number any ideas? -
#pragma once namespace vendingmachine { using namespace system; using namespace system::componentmodel; using namespace system::collections; using namespace system::windows::forms; using namespace system::data; using namespace system::drawing; /// <summary> /// summary form1 /// </summary> public ref class form1 : public system::windows::forms::form { public: form1(void) { initializecomponent(); // //todo: add constructor code here // } protected: /// <summary> /// clean resources being used. /// </summary> ~form1() { if (components) { delete components; } } private: system::windows::forms::label^ label1; private: system::windows::forms::textbox^ textbox1; protected: private: system::windows::forms::button^ button1; private: system::windows::forms::button^ button2; private: system::windows::forms::label^ label2; private: system::windows::forms::label^ label3; private: system::windows::forms::label^ label4; private: system::windows::forms::label^ label5; private: system::windows::forms::label^ label6; private: system::windows::forms::label^ label7; private: system::windows::forms::textbox^ textbox2; private: system::windows::forms::textbox^ textbox3; private: system::windows::forms::textbox^ textbox4; private: system::windows::forms::textbox^ textbox5; private: system::windows::forms::textbox^ textbox6; private: /// <summary> /// required designer variable. /// </summary> system::componentmodel::container ^components; #pragma region windows form designer generated code /// <summary> /// required method designer support - not modify /// contents of method code editor. /// </summary> void initializecomponent(void) { this->label1 = (gcnew system::windows::forms::label()); this->textbox1 = (gcnew system::windows::forms::textbox()); this->button1 = (gcnew system::windows::forms::button()); this->button2 = (gcnew system::windows::forms::button()); this->label2 = (gcnew system::windows::forms::label()); this->label3 = (gcnew system::windows::forms::label()); this->label4 = (gcnew system::windows::forms::label()); this->label5 = (gcnew system::windows::forms::label()); this->label6 = (gcnew system::windows::forms::label()); this->label7 = (gcnew system::windows::forms::label()); this->textbox2 = (gcnew system::windows::forms::textbox()); this->textbox3 = (gcnew system::windows::forms::textbox()); this->textbox4 = (gcnew system::windows::forms::textbox()); this->textbox5 = (gcnew system::windows::forms::textbox()); this->textbox6 = (gcnew system::windows::forms::textbox()); this->suspendlayout(); // // label1 // this->label1->autosize = true; this->label1->location = system::drawing::point(14, 14); this->label1->name = l"label1"; this->label1->size = system::drawing::size(153, 13); this->label1->tabindex = 0; this->label1->text = l"amount of change (0-99 cents)"; // // textbox1 // this->textbox1->location = system::drawing::point(173, 11); this->textbox1->name = l"textbox1"; this->textbox1->size = system::drawing::size(95, 20); this->textbox1->tabindex = 1; // // button1 // this->button1->location = system::drawing::point(20, 59); this->button1->name = l"button1"; this->button1->size = system::drawing::size(60, 28); this->button1->tabindex = 2; this->button1->text = l"calculate"; this->button1->usevisualstylebackcolor = true; this->button1->click += gcnew system::eventhandler(this, &form1::button1_click); // // button2 // this->button2->location = system::drawing::point(190, 59); this->button2->name = l"button2"; this->button2->size = system::drawing::size(63, 28); this->button2->tabindex = 3; this->button2->text = l"clear"; this->button2->usevisualstylebackcolor = true; // // label2 // this->label2->autosize = true; this->label2->location = system::drawing::point(14, 132); this->label2->name = l"label2"; this->label2->size = system::drawing::size(99, 13); this->label2->tabindex = 4; this->label2->text = l"number of quarters"; // // label3 // this->label3->autosize = true; this->label3->location = system::drawing::point(14, 178); this->label3->name = l"label3"; this->label3->size = system::drawing::size(88, 13); this->label3->tabindex = 5; this->label3->text = l"number of dimes"; // // label4 // this->label4->autosize = true; this->label4->location = system::drawing::point(14, 218); this->label4->name = l"label4"; this->label4->size = system::drawing::size(0, 13); this->label4->tabindex = 6; // // label5 // this->label5->autosize = true; this->label5->location = system::drawing::point(14, 215); this->label5->name = l"label5"; this->label5->size = system::drawing::size(94, 13); this->label5->tabindex = 7; this->label5->text = l"number of nickels"; // // label6 // this->label6->autosize = true; this->label6->location = system::drawing::point(14, 256); this->label6->name = l"label6"; this->label6->size = system::drawing::size(96, 13); this->label6->tabindex = 8; this->label6->text = l"number of pennies"; // // label7 // this->label7->autosize = true; this->label7->location = system::drawing::point(14, 301); this->label7->name = l"label7"; this->label7->size = system::drawing::size(59, 13); this->label7->tabindex = 9; this->label7->text = l"total coins"; // // textbox2 // this->textbox2->location = system::drawing::point(173, 129); this->textbox2->name = l"textbox2"; this->textbox2->size = system::drawing::size(95, 20); this->textbox2->tabindex = 10; // // textbox3 // this->textbox3->location = system::drawing::point(173, 178); this->textbox3->name = l"textbox3"; this->textbox3->size = system::drawing::size(95, 20); this->textbox3->tabindex = 11; // // textbox4 // this->textbox4->location = system::drawing::point(173, 215); this->textbox4->name = l"textbox4"; this->textbox4->size = system::drawing::size(95, 20); this->textbox4->tabindex = 12; // // textbox5 // this->textbox5->location = system::drawing::point(173, 253); this->textbox5->name = l"textbox5"; this->textbox5->size = system::drawing::size(95, 20); this->textbox5->tabindex = 13; // // textbox6 // this->textbox6->location = system::drawing::point(173, 294); this->textbox6->name = l"textbox6"; this->textbox6->size = system::drawing::size(95, 20); this->textbox6->tabindex = 14; // // form1 // this->autoscaledimensions = system::drawing::sizef(6, 13); this->autoscalemode = system::windows::forms::autoscalemode::font; this->clientsize = system::drawing::size(284, 338); this->controls->add(this->textbox6); this->controls->add(this->textbox5); this->controls->add(this->textbox4); this->controls->add(this->textbox3); this->controls->add(this->textbox2); this->controls->add(this->label7); this->controls->add(this->label6); this->controls->add(this->label5); this->controls->add(this->label4); this->controls->add(this->label3); this->controls->add(this->label2); this->controls->add(this->button2); this->controls->add(this->button1); this->controls->add(this->textbox1); this->controls->add(this->label1); this->name = l"form1"; this->text = l"form1"; this->resumelayout(false); this->performlayout(); } #pragma endregion private: system::void button1_click(system::object^ sender, system::eventargs^ e) { // declare variables double change, quarters, dimes, nickels, pennies, total_coins; // read in data double::tryparse(textbox1->text, change); double::tryparse(textbox2->text, quarters); double::tryparse(textbox3->text, dimes); double::tryparse(textbox4->text, nickels); double::tryparse(textbox5->text, pennies); the issue in process data section of code
// process data change = change; 0 - 99; textbox1->text = change.tostring(); messagebox::show(change.tostring()); /*error-i-need-a-whole-number ->*/quarters += change / 25; messagebox::show(quarters.tostring()); // current code above gives me answer of 3.48 when change equals 87 // display results textbox6->text = total_coins.tostring(); textbox2->text = quarters.tostring(); } }; }
looking past awkward lines accomplish nothing change = change; 0 - 99;, want use int type quantities.
why should use double quantities anyway? have ever heard of fraction of quarter or dime? make them of type int. (change representing cents, not dollars, making int makes sense.)
if use int, 87 / 25 truncated 3 quarters, result you're expecting.
Comments
Post a Comment