Sign in to follow this  
xaviarrob

(C++ Help) Win32 form

Recommended Posts

xaviarrob    103
So I got My Mill calculator(console to work) and I tried to put everything into a Win32 form and Surprisingly all of my functions seem to be correct,
but I have ran into one problem,The actual function is evaluating to 0 before it makes it to the answer window. I have commented where this occurs and ignore the reset button not doing anything.

[code]#pragma once

namespace AtheabsolutefinalMillCalculator {

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 for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::TextBox^ AnsLabel;
private: System::Windows::Forms::TextBox^ AnsLabelPM;



private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::Label^ label5;

private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->AnsLabel = (gcnew System::Windows::Forms::TextBox());
this->AnsLabelPM = (gcnew System::Windows::Forms::TextBox());
this->label4 = (gcnew System::Windows::Forms::Label());
this->label5 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(12, 269);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(215, 54);
this->button1->TabIndex = 0;
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(233, 270);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(217, 53);
this->button2->TabIndex = 1;
this->button2->Text = L"Reset";
this->button2->UseVisualStyleBackColor = true;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Lucida Sans", 14.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label1->Location = System::Drawing::Point(52, 19);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(134, 22);
this->label1->TabIndex = 2;
this->label1->Text = L"House Value";
this->label1->Click += gcnew System::EventHandler(this, &Form1::label1_Click);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(17, 44);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(199, 20);
this->textBox1->TabIndex = 3;
this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(17, 109);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(199, 20);
this->textBox2->TabIndex = 4;
this->textBox2->TextChanged += gcnew System::EventHandler(this, &Form1::textBox2_TextChanged);
//
// label2
//
this->label2->AutoSize = true;
this->label2->Font = (gcnew System::Drawing::Font(L"Lucida Sans", 14.25F, System::Drawing::FontStyle::Bold));
this->label2->Location = System::Drawing::Point(29, 84);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(173, 22);
this->label2->TabIndex = 5;
this->label2->Text = L"Number of Mills";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Font = (gcnew System::Drawing::Font(L"Lucida Sans", 14.25F, System::Drawing::FontStyle::Bold));
this->label3->Location = System::Drawing::Point(29, 145);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(91, 22);
this->label3->TabIndex = 6;
this->label3->Text = L"You Pay";
this->label3->Click += gcnew System::EventHandler(this, &Form1::label3_Click);
//
// AnsLabel
//
this->AnsLabel->Location = System::Drawing::Point(17, 170);
this->AnsLabel->Name = L"AnsLabel";
this->AnsLabel->Size = System::Drawing::Size(199, 20);
this->AnsLabel->TabIndex = 7;
//
// AnsLabelPM
//
this->AnsLabelPM->Location = System::Drawing::Point(17, 211);
this->AnsLabelPM->Name = L"AnsLabelPM";
this->AnsLabelPM->Size = System::Drawing::Size(199, 20);
this->AnsLabelPM->TabIndex = 8;
//
// label4
//
this->label4->AutoSize = true;
this->label4->Font = (gcnew System::Drawing::Font(L"Lucida Sans", 14.25F, System::Drawing::FontStyle::Bold));
this->label4->Location = System::Drawing::Point(222, 170);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(94, 22);
this->label4->TabIndex = 9;
this->label4->Text = L"Per Year";
//
// label5
//
this->label5->AutoSize = true;
this->label5->Font = (gcnew System::Drawing::Font(L"Lucida Sans", 14.25F, System::Drawing::FontStyle::Bold));
this->label5->Location = System::Drawing::Point(222, 211);
this->label5->Name = L"label5";
this->label5->Size = System::Drawing::Size(114, 22);
this->label5->TabIndex = 10;
this->label5->Text = L"Per Month";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::SystemColors::AppWorkspace;
this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^ >(resources->GetObject(L"$this.BackgroundImage")));
this->BackgroundImageLayout = System::Windows::Forms::ImageLayout::Stretch;
this->ClientSize = System::Drawing::Size(534, 353);
this->Controls->Add(this->label5);
this->Controls->Add(this->label4);
this->Controls->Add(this->AnsLabelPM);
this->Controls->Add(this->AnsLabel);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->label1);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Mill Calculator";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion //this is where the calculation occurs
int intnum1, intnum2, intnum3, intnum4, intans, intanspm;
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
intnum1=int::Parse(this->textBox1->Text); //This is the calculation, If you need a screenshot I will gladly post one.
intnum2=int::Parse(this->textBox2->Text);
intnum4= intnum1 * .35;
intnum3= intnum2 * .001;
intans= intnum4 * intnum3;
this->AnsLabel->Text=Convert::ToString(intans);
intanspm= intans / 12;
this->AnsLabelPM->Text=Convert::ToString(intanspm);

}
private: System::Void label1_Click(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void textBox2_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void label3_Click(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
};
}

[/code]

Share this post


Link to post
Share on other sites
Nevadaes    137
You're getting a zero as an answer because you're multiplying ints with doubles. You can't multiply an integer with a float/double, because, as the definition suggests, an integer number isn't a real one, therefore it doesn't allow real values such as decimal. When you multiply an integer with a double, it will floors the number:

[code]
int myInt = 1;
myInt *= 0.5; // myInt now equals 0, because it has been floored up.
myInt = 1;
myInt *= 1.5; // myInt now equals 1.
[/code]

If you want to solve your problem, you either have to explicitly cast your ints into floats/doubles when you multiply them, or simply change your variables type into float/double, because you're multiplying your ints by "0.35" and "0.001".

Share this post


Link to post
Share on other sites
xaviarrob    103
[quote name='Nevadaes' timestamp='1306468743' post='4816289']
You're getting a zero as an answer because you're multiplying ints with doubles. You can't multiply an integer with a float/double, because, as the definition suggests, an integer number isn't a real one, therefore it doesn't allow real values such as decimal. When you multiply an integer with a double, it will floors the number:

[code]
int myInt = 1;
myInt *= 0.5; // myInt now equals 0, because it has been floored up.
myInt = 1;
myInt *= 1.5; // myInt now equals 1.
[/code]

If you want to solve your problem, you either have to explicitly cast your ints into floats/doubles when you multiply them, or simply change your variables type into float/double, because you're multiplying your ints by "0.35" and "0.001".
[/quote]
Exactly what I needed, thanks so much! Minus the reset button the program is fully functional how I want it to be. Would you happen to know how I would, say one of the ansewers comes out as like 53.333333, cut it off at the 2nd decimal place?

Share this post


Link to post
Share on other sites
Nevadaes    137
Well, one way of doing would be to play around with strings functionality. For example, you could put your number in a string, such as 53.333333 for this example, find the dot "." and substring the rest of the string after counting more than two numbers. Like that, you only remove something when there is more than 2 decimal numbers. Check what functionality the strings can offer you and play around with those ;)

Share this post


Link to post
Share on other sites
xaviarrob    103
For math purposes though couldn't that cause basicly a + or - 1cent error? Since it would just be cutting off 2 places after the decimal.

Share this post


Link to post
Share on other sites
rip-off    10976
You must decide whether you want accuracy or not. When you choose to round you are choosing to throw away some level of accuracy as being irrelevant. One option is to display the rounded version by default but have some UI control or application setting to disable this.

Aside: you are declaring class members for temporary data. Don't do this, instead declare the variables at first use. Also, it is redundant to encode the type of the variable into its name. Disregard the fact that automatically generated code does this. In your earlier thread you had meaninful names, you should do that here too.

Share this post


Link to post
Share on other sites
xaviarrob    103
[quote name='rip-off' timestamp='1306490245' post='4816362']
You must decide whether you want accuracy or not. When you choose to round you are choosing to throw away some level of accuracy as being irrelevant. One option is to display the rounded version by default but have some UI control or application setting to disable this.

Aside: you are declaring class members for temporary data. Don't do this, instead declare the variables at first use. Also, it is redundant to encode the type of the variable into its name. Disregard the fact that automatically generated code does this. In your earlier thread you had meaninful names, you should do that here too.
[/quote]


Well, in the case of money, if I always round up above .5 and round down below .4 and always do it to the 2nd decimal place wouldn't it technicly be accurate? I understand that the actual "literal" numbers would be incorrect but when you pay for something they typicly round in the case of percents, correct me if I am wrong. As to that what exactly am I making temp data? I know I used bad names for the variables (I was using a tutorial and trying to keep as close to it to ensure I didn't do anything wrong).I am changing them as we speak.

Share this post


Link to post
Share on other sites
rip-off    10976
If you want that kind of rounding you can do it just before turning the data into a string. The technique is to add 0.5f to the data, and truncate it as an integer. You can see how this works: if you have 0.51, adding 0.5 gives 1.01 and truncating gives you 1. If you have 0.49, adding .5 gives 0.99 and truncating gives 0.

In your case, because you want two decimal places of accuracy, you can multiply the number by 100, apply the above function and then divide the resulting integer by 100.0f to give you the result to exactly two places of decimal: no string manipulation required.

Something like the following:
[code]
const float Accuracy = 100.0f;

float calculation = /* ... */;
int truncated = static_cast<int>((Accuracy * calculation) + 0.5f);
float display = truncated / Accuracy;

std::cout << "Result: " << display << '\n';
[/code]

Share this post


Link to post
Share on other sites
xaviarrob    103
Here is what I have and I get these errors. This is probably due to me not knowing a whole lot about how c++ works.

[font="Consolas"][size="1"][font="Consolas"][size="1"]

[size="4"]1>c:\users\rob nicholas\documents\visual studio 2010\projects\a the absolute final mill calculator\a the absolute final mill calculator\Form1.h(214): error C3845: 'AtheabsolutefinalMillCalculator::Form1::Accuracy': only static data members can be initialized inside a ref class or value type
1>c:\users\rob nicholas\documents\visual studio 2010\projects\a the absolute final mill calculator\a the absolute final mill calculator\Form1.h(21): error C2758: 'AtheabsolutefinalMillCalculator::Form1::Accuracy' : must be initialized in constructor base/member initializer list
1> c:\users\rob nicholas\documents\visual studio 2010\projects\a the absolute final mill calculator\a the absolute final mill calculator\Form1.h(214) : see declaration of 'AtheabsolutefinalMillCalculator::Form1::Accuracy'
1>c:\users\rob nicholas\documents\visual studio 2010\projects\a the absolute final mill calculator\a the absolute final mill calculator\Form1.h(223): error C2143: syntax error : missing ';' before 'static_cast'
1>c:\users\rob nicholas\documents\visual studio 2010\projects\a the absolute final mill calculator\a the absolute final mill calculator\Form1.h(223): error C2059: syntax error : '('



========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========[/size]

[/size][/font][/size][/font]



[code] double (fHouseValue, fNumberOfMills, fMillCalculation, fAssesedValue, fTaxPayedPerYear, fTaxPayedPerMonth);
const float Accuracy = 100.0f;
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
fHouseValue=double::Parse(this->textBox1->Text);
fNumberOfMills=double::Parse(this->textBox2->Text);
fAssesedValue= fHouseValue * .35;
fMillCalculation= fNumberOfMills * .001;
fTaxPayedPerYear= fAssesedValue * fMillCalculation * .875;
int truncated static_cast<int>((Accuracy * fTaxPayedPerYear) + 0.5f);
float fFinalResult = truncated / Accuracy;
this->AnsLabel->Text=Convert::ToString(fFinalResult);
fTaxPayedPerMonth= fTaxPayedPerYear / 12;
this->AnsLabelPM->Text=Convert::ToString(fTaxPayedPerMonth);
[/code]

Share this post


Link to post
Share on other sites
rip-off    10976
This is what the compiler is trying to say:
[quote]
[size="1"]error C3845: 'AtheabsolutefinalMillCalculator::Form1::Accuracy': [size="5"]only static data members can be initialized inside a[/size] ref [size="5"]class[/size] or value type[/size]
[/quote]
Note that Googling the error code ([url="http://msdn.microsoft.com/en-us/library/z8zd05hd(v=vs.80).aspx"]C3845 here[/url]) is a great way to understand these errors.

Take the errors one at a time: sometimes an early error will confuse the compiler so much that it will generate spurious errors later on.

Share this post


Link to post
Share on other sites
xaviarrob    103
[quote name='rip-off' timestamp='1306505141' post='4816429']
This is what the compiler is trying to say:
[quote]
[size="1"]error C3845: 'AtheabsolutefinalMillCalculator::Form1::Accuracy': [size="5"]only static data members can be initialized inside a[/size] ref [size="5"]class[/size] or value type[/size]
[/quote]
Note that Googling the error code ([url="http://msdn.microsoft.com/en-us/library/z8zd05hd(v=vs.80).aspx"]C3845 here[/url]) is a great way to understand these errors.

Take the errors one at a time: sometimes an early error will confuse the compiler so much that it will generate spurious errors later on.
[/quote]

Had one of those *facepalm* moments XD thanks for the help

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this