include <iostream>

using namespace std;

int main () {
    //declaring variables
    double Pi= 3.1416 ;
    double Diameter_ofcircle = 0.0;
    double priceofrailingmaterial = 0.0;
    double Totalpriceofrailingmaterial = 0.0;

    //Getting the user input

    cout<<"Enter the diameter of the circle ";
    cin>>Diameter_ofcircle ;

    cout<<"Enter the price of railing material per foot ";
    cin>> priceofrailingmaterial ;

    Totalpriceofrailingmaterial = ( (Diameter_ofcircle)* Pi ) * ( priceofrailingmaterial) ;

    cout<<"Total price of railing material "<<Totalpriceofrailingmaterial<<endl;

    return 0;
}

This is from codeblocks and I need to transfer this into Visual Studio Express 2010 C++ and this is what I have

#pragma once

namespace Diameter {

    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::Label^  lblPi;
    protected: 

    private: System::Windows::Forms::TextBox^  txtPi;
    private: System::Windows::Forms::Label^  lblDiameter;
    protected: 


    private: System::Windows::Forms::TextBox^  txtDiameter;
    private: System::Windows::Forms::Label^  lblPrice;


    private: System::Windows::Forms::TextBox^  txtPrice;
    private: System::Windows::Forms::Label^  lblTotal;


    private: System::Windows::Forms::TextBox^  txtTotal;

    private: System::Windows::Forms::Button^  btnClose;




    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)
        {
            this->lblPi = (gcnew System::Windows::Forms::Label());
            this->txtPi = (gcnew System::Windows::Forms::TextBox());
            this->lblDiameter = (gcnew System::Windows::Forms::Label());
            this->txtDiameter = (gcnew System::Windows::Forms::TextBox());
            this->lblPrice = (gcnew System::Windows::Forms::Label());
            this->txtPrice = (gcnew System::Windows::Forms::TextBox());
            this->lblTotal = (gcnew System::Windows::Forms::Label());
            this->txtTotal = (gcnew System::Windows::Forms::TextBox());
            this->btnClose = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            // 
            // lblPi
            // 
            this->lblPi->AutoSize = true;
            this->lblPi->Location = System::Drawing::Point(12, 90);
            this->lblPi->Name = L"lblPi";
            this->lblPi->Size = System::Drawing::Size(16, 13);
            this->lblPi->TabIndex = 0;
            this->lblPi->Text = L"Pi";
            // 
            // txtPi
            // 
            this->txtPi->BackColor = System::Drawing::SystemColors::Window;
            this->txtPi->Location = System::Drawing::Point(124, 87);
            this->txtPi->Name = L"txtPi";
            this->txtPi->ReadOnly = true;
            this->txtPi->Size = System::Drawing::Size(75, 20);
            this->txtPi->TabIndex = 1;
            this->txtPi->Text = L"3.1416";
            // 
            // lblDiameter
            // 
            this->lblDiameter->AutoSize = true;
            this->lblDiameter->Location = System::Drawing::Point(12, 32);
            this->lblDiameter->Name = L"lblDiameter";
            this->lblDiameter->Size = System::Drawing::Size(89, 13);
            this->lblDiameter->TabIndex = 2;
            this->lblDiameter->Text = L"Diameter of circle";
            // 
            // txtDiameter
            // 
            this->txtDiameter->Location = System::Drawing::Point(122, 29);
            this->txtDiameter->Name = L"txtDiameter";
            this->txtDiameter->Size = System::Drawing::Size(77, 20);
            this->txtDiameter->TabIndex = 3;
            // 
            // lblPrice
            // 
            this->lblPrice->AutoSize = true;
            this->lblPrice->Location = System::Drawing::Point(12, 160);
            this->lblPrice->Name = L"lblPrice";
            this->lblPrice->Size = System::Drawing::Size(154, 13);
            this->lblPrice->TabIndex = 4;
            this->lblPrice->Text = L"Price of railing material per foot ";
            // 
            // txtPrice
            // 
            this->txtPrice->Location = System::Drawing::Point(194, 215);
            this->txtPrice->Name = L"txtPrice";
            this->txtPrice->Size = System::Drawing::Size(142, 20);
            this->txtPrice->TabIndex = 5;
            // 
            // lblTotal
            // 
            this->lblTotal->AutoSize = true;
            this->lblTotal->Location = System::Drawing::Point(12, 215);
            this->lblTotal->Name = L"lblTotal";
            this->lblTotal->Size = System::Drawing::Size(142, 13);
            this->lblTotal->TabIndex = 6;
            this->lblTotal->Text = L"Total price  of raling material ";
            // 
            // txtTotal
            // 
            this->txtTotal->Location = System::Drawing::Point(194, 160);
            this->txtTotal->Name = L"txtTotal";
            this->txtTotal->Size = System::Drawing::Size(122, 20);
            this->txtTotal->TabIndex = 7;
            // 
            // btnClose
            // 
            this->btnClose->Location = System::Drawing::Point(241, 336);
            this->btnClose->Name = L"btnClose";
            this->btnClose->Size = System::Drawing::Size(75, 23);
            this->btnClose->TabIndex = 8;
            this->btnClose->Text = L"Close";
            this->btnClose->UseVisualStyleBackColor = true;
            this->btnClose->Click += gcnew System::EventHandler(this, &Form1::btnClose_Click);
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(686, 418);
            this->Controls->Add(this->btnClose);
            this->Controls->Add(this->txtTotal);
            this->Controls->Add(this->lblTotal);
            this->Controls->Add(this->txtPrice);
            this->Controls->Add(this->lblPrice);
            this->Controls->Add(this->txtDiameter);
            this->Controls->Add(this->lblDiameter);
            this->Controls->Add(this->txtPi);
            this->Controls->Add(this->lblPi);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion

    private: System::Void btnClose_Click(System::Object^  sender, System::EventArgs^  e) {
                 Application::Exit();
             }
};
}

Diameter of circle is in a label and to enter its number is in a text box.
Pi is in a label and to enter its number is in a text box I have it to read only because its suppose to stay 3.1416.
Price of railing material per foot is in a label and to enter its number is in a text box.
Total price of railing material is in a label and to enter its number is in a text box.

Dani AI

Generated

A Calculate button and a small Click handler that parses the TextBox strings to numbers, does the multiplication, and writes the result back to the total TextBox fixes the gap between the console example and the WinForms form. The formula (diameter pi price) is correct; the usual problem in WinForms is converting Text (System::String^) into numeric types before multiplying.

correctly called out that string-to-number conversion is the likely issue; std::stringstream works for native std::string but is awkward with TextBox->Text in C++/CLI. was right to suggest a Calculate button. In C++/CLI it is common to use System::Double::TryParse for safe parsing or System::Convert::ToDouble inside a try/catch.

Example Click handler (C++/CLI) that validates input and displays the formatted total:

private: System::Void btnCalculate_Click(System::Object^ sender, System::EventArgs^ e)
{
    double pi, diameter, price, total;

    if (!System::Double::TryParse(txtPi->Text->Trim(), pi) ||
        !System::Double::TryParse(txtDiameter->Text->Trim(), diameter) ||
        !System::Double::TryParse(txtPrice->Text->Trim(), price))
    {
        System::Windows::Forms::MessageBox::Show(
            "Enter valid numeric values for Pi, diameter and price.",
            "Input error",
            System::Windows::Forms::MessageBoxButtons::OK,
            System::Windows::Forms::MessageBoxIcon::Error);
        return;
    }

    total = diameter * pi * price;
    txtTotal->Text = total.ToString("F2"); // formats to 2 decimal places
}

Notes and small tips: make txtTotal ReadOnly so output is not accidentally edited; trim input before parsing; TryParse respects the current culture (decimal comma vs dot) — use invariant parsing only if inputs are guaranteed to use '.'; for more precision prefer a code constant (e.g. const double PI = 3.141592653589793;) instead of a user-editable Pi textbox.

Recommended Answers

All 5 Replies

I need to mulitply but I don't know how

This,(Diameter_ofcircle)* Pi * ( priceofrailingmaterial), seems to be ok. what problem are you having with multiplying?

Add a button "Calculate" to your form.
In the clickhandler Get the text of PI, Price and Diameter out of their respective textboxes.
Convert to float or double, perform a multiplication. Convert this oucome to text and put that text in your total textbox.
Btw. you can use the constant M_PI if you put this in front:

#define _USE_MATH_DEFINES 
#include <cmath>

That way you can delete your PI label and textbox.

thats from code blocks, I was showing the formula for Total Price.

Actually that's the only formula you were showing. All your code does is set up the form and its controls.

It appears that converting your string to numbers is the problem not multiplying. One fairly simple way is to use sstream, it the standard extraction operator and can do simple conversions like this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.