So I've decided to make my first windowed application (only previously made command prompt applications). Using C++. I'm making a simple calculator, but I'm having trouble converting a string to a double. The error occurs every time I use the "atof" function on label->Text. I read in my textbook this function should convert a C-string to a double. label1 is the input box for my calculator. Buttons are self explanatory.
Here's what my calculator should look like when finished:
Here is the whole code:
Here's what my calculator should look like when finished:
Code:
private: System::Void buttonPlus_Click(System::Object^ sender, System::EventArgs^ e) {
firstNumber = atof(label1->Text);
label1->Text = "0";
operation = '+';
}
// Button Minus
private: System::Void buttonMinus_Click(System::Object^ sender, System::EventArgs^ e) {
firstNumber = atof(label1->Text);
label1->Text = "0";
operation = '-';
}
// Button Divide
private: System::Void buttonDivide_Click(System::Object^ sender, System::EventArgs^ e) {
firstNumber = atof(label1->Text);
label1->Text = "0";
operation = '/';
}
// Button Times
private: System::Void buttonTimes_Click(System::Object^ sender, System::EventArgs^ e) {
firstNumber = atof(label1->Text);
label1->Text = "0";
operation = '*';
}
Here is the whole code:
Code:
#pragma once
#include <cstdlib> // Needed for string conversion functions
using namespace std;
namespace Calculator {
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^ buttonPlus;
private: System::Windows::Forms::Button^ buttonMinus;
private: System::Windows::Forms::Button^ buttonTimes;
private: System::Windows::Forms::Button^ buttonDivide;
private: System::Windows::Forms::Button^ buttonEquals;
private: System::Windows::Forms::Button^ buttonClear;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Button^ button5;
private: System::Windows::Forms::Button^ button8;
private: System::Windows::Forms::Button^ button4;
private: System::Windows::Forms::Button^ button7;
private: System::Windows::Forms::Button^ button0;
private: System::Windows::Forms::Button^ button6;
private: System::Windows::Forms::Button^ button9;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Button^ buttonDecimal;
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->buttonPlus = (gcnew System::Windows::Forms::Button());
this->buttonMinus = (gcnew System::Windows::Forms::Button());
this->buttonTimes = (gcnew System::Windows::Forms::Button());
this->buttonDivide = (gcnew System::Windows::Forms::Button());
this->buttonEquals = (gcnew System::Windows::Forms::Button());
this->buttonClear = (gcnew System::Windows::Forms::Button());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->button5 = (gcnew System::Windows::Forms::Button());
this->button8 = (gcnew System::Windows::Forms::Button());
this->button4 = (gcnew System::Windows::Forms::Button());
this->button7 = (gcnew System::Windows::Forms::Button());
this->button0 = (gcnew System::Windows::Forms::Button());
this->button6 = (gcnew System::Windows::Forms::Button());
this->button9 = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->buttonDecimal = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// buttonPlus
//
this->buttonPlus->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->buttonPlus->Location = System::Drawing::Point(197, 63);
this->buttonPlus->Name = L"buttonPlus";
this->buttonPlus->Size = System::Drawing::Size(75, 32);
this->buttonPlus->TabIndex = 1;
this->buttonPlus->Text = L"+";
this->buttonPlus->UseVisualStyleBackColor = true;
this->buttonPlus->Click += gcnew System::EventHandler(this, &Form1::buttonPlus_Click);
//
// buttonMinus
//
this->buttonMinus->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->buttonMinus->Location = System::Drawing::Point(197, 96);
this->buttonMinus->Name = L"buttonMinus";
this->buttonMinus->Size = System::Drawing::Size(75, 32);
this->buttonMinus->TabIndex = 2;
this->buttonMinus->Text = L"-";
this->buttonMinus->UseVisualStyleBackColor = true;
this->buttonMinus->Click += gcnew System::EventHandler(this, &Form1::buttonMinus_Click);
//
// buttonTimes
//
this->buttonTimes->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->buttonTimes->Location = System::Drawing::Point(197, 166);
this->buttonTimes->Name = L"buttonTimes";
this->buttonTimes->Size = System::Drawing::Size(75, 32);
this->buttonTimes->TabIndex = 3;
this->buttonTimes->Text = L"*";
this->buttonTimes->UseVisualStyleBackColor = true;
this->buttonTimes->Click += gcnew System::EventHandler(this, &Form1::buttonTimes_Click);
//
// buttonDivide
//
this->buttonDivide->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->buttonDivide->Location = System::Drawing::Point(197, 130);
this->buttonDivide->Name = L"buttonDivide";
this->buttonDivide->Size = System::Drawing::Size(75, 32);
this->buttonDivide->TabIndex = 4;
this->buttonDivide->Text = L"/";
this->buttonDivide->UseVisualStyleBackColor = true;
this->buttonDivide->Click += gcnew System::EventHandler(this, &Form1::buttonDivide_Click);
//
// buttonEquals
//
this->buttonEquals->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->buttonEquals->Location = System::Drawing::Point(197, 202);
this->buttonEquals->Name = L"buttonEquals";
this->buttonEquals->Size = System::Drawing::Size(75, 45);
this->buttonEquals->TabIndex = 10;
this->buttonEquals->Text = L"=";
this->buttonEquals->UseVisualStyleBackColor = true;
this->buttonEquals->Click += gcnew System::EventHandler(this, &Form1::buttonEquals_Click);
//
// buttonClear
//
this->buttonClear->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 11.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->buttonClear->Location = System::Drawing::Point(134, 201);
this->buttonClear->Name = L"buttonClear";
this->buttonClear->Size = System::Drawing::Size(60, 46);
this->buttonClear->TabIndex = 7;
this->buttonClear->Text = L"C";
this->buttonClear->UseVisualStyleBackColor = true;
this->buttonClear->Click += gcnew System::EventHandler(this, &Form1::buttonClear_Click);
//
// button1
//
this->button1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button1->Location = System::Drawing::Point(12, 63);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(60, 46);
this->button1->TabIndex = 11;
this->button1->Text = L"1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button2->Location = System::Drawing::Point(74, 63);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(60, 46);
this->button2->TabIndex = 12;
this->button2->Text = L"2";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// button3
//
this->button3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button3->Location = System::Drawing::Point(134, 63);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(60, 46);
this->button3->TabIndex = 13;
this->button3->Text = L"3";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// button5
//
this->button5->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button5->Location = System::Drawing::Point(74, 109);
this->button5->Name = L"button5";
this->button5->Size = System::Drawing::Size(60, 46);
this->button5->TabIndex = 14;
this->button5->Text = L"5";
this->button5->UseVisualStyleBackColor = true;
this->button5->Click += gcnew System::EventHandler(this, &Form1::button5_Click);
//
// button8
//
this->button8->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button8->Location = System::Drawing::Point(74, 155);
this->button8->Name = L"button8";
this->button8->Size = System::Drawing::Size(60, 46);
this->button8->TabIndex = 15;
this->button8->Text = L"8";
this->button8->UseVisualStyleBackColor = true;
this->button8->Click += gcnew System::EventHandler(this, &Form1::button8_Click);
//
// button4
//
this->button4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button4->Location = System::Drawing::Point(12, 109);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(60, 46);
this->button4->TabIndex = 16;
this->button4->Text = L"4";
this->button4->UseVisualStyleBackColor = true;
this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
//
// button7
//
this->button7->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button7->Location = System::Drawing::Point(12, 155);
this->button7->Name = L"button7";
this->button7->Size = System::Drawing::Size(60, 46);
this->button7->TabIndex = 17;
this->button7->Text = L"7";
this->button7->UseVisualStyleBackColor = true;
this->button7->Click += gcnew System::EventHandler(this, &Form1::button7_Click);
//
// button0
//
this->button0->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button0->Location = System::Drawing::Point(12, 201);
this->button0->Name = L"button0";
this->button0->Size = System::Drawing::Size(60, 46);
this->button0->TabIndex = 18;
this->button0->Text = L"0";
this->button0->UseVisualStyleBackColor = true;
this->button0->Click += gcnew System::EventHandler(this, &Form1::button0_Click);
//
// button6
//
this->button6->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button6->Location = System::Drawing::Point(134, 109);
this->button6->Name = L"button6";
this->button6->Size = System::Drawing::Size(60, 46);
this->button6->TabIndex = 19;
this->button6->Text = L"6";
this->button6->UseVisualStyleBackColor = true;
this->button6->Click += gcnew System::EventHandler(this, &Form1::button6_Click);
//
// button9
//
this->button9->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button9->Location = System::Drawing::Point(134, 155);
this->button9->Name = L"button9";
this->button9->Size = System::Drawing::Size(60, 46);
this->button9->TabIndex = 20;
this->button9->Text = L"9";
this->button9->UseVisualStyleBackColor = true;
this->button9->Click += gcnew System::EventHandler(this, &Form1::button9_Click);
//
// label1
//
this->label1->BackColor = System::Drawing::SystemColors::ControlLightLight;
this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label1->Location = System::Drawing::Point(12, 9);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(260, 45);
this->label1->TabIndex = 21;
this->label1->Text = L"0";
this->label1->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
//
// buttonDecimal
//
this->buttonDecimal->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->buttonDecimal->Location = System::Drawing::Point(74, 201);
this->buttonDecimal->Name = L"buttonDecimal";
this->buttonDecimal->Size = System::Drawing::Size(60, 46);
this->buttonDecimal->TabIndex = 22;
this->buttonDecimal->Text = L".";
this->buttonDecimal->UseVisualStyleBackColor = true;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Controls->Add(this->buttonDecimal);
this->Controls->Add(this->label1);
this->Controls->Add(this->button9);
this->Controls->Add(this->button6);
this->Controls->Add(this->button0);
this->Controls->Add(this->button7);
this->Controls->Add(this->button4);
this->Controls->Add(this->button8);
this->Controls->Add(this->button5);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->buttonClear);
this->Controls->Add(this->buttonEquals);
this->Controls->Add(this->buttonDivide);
this->Controls->Add(this->buttonTimes);
this->Controls->Add(this->buttonMinus);
this->Controls->Add(this->buttonPlus);
this->Location = System::Drawing::Point(12, 13);
this->Name = L"Form1";
this->Text = L"Calculator";
this->ResumeLayout(false);
}
// Setup variables
double firstNumber;
double secondNumber;
double result;
char operation;
#pragma endregion
// Button 1
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
if (label1->Text == "0")
{
label1->Text = "1";
}
else
{
label1->Text = label1->Text + "1";
}
}
// Button 2
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
if (label1->Text == "0")
{
label1->Text = "2";
}
else
{
label1->Text = label1->Text + "2";
}
}
// Button 3
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
if (label1->Text == "0")
{
label1->Text = "3";
}
else
{
label1->Text = label1->Text + "3";
}
}
// Button 4
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
if (label1->Text == "0")
{
label1->Text = "4";
}
else
{
label1->Text = label1->Text + "4";
}
}
// Button 5
private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) {
if (label1->Text == "0")
{
label1->Text = "5";
}
else
{
label1->Text = label1->Text + "5";
}
}
// Button 6
private: System::Void button6_Click(System::Object^ sender, System::EventArgs^ e) {
if (label1->Text == "0")
{
label1->Text = "6";
}
else
{
label1->Text = label1->Text + "6";
}
}
// Button 7
private: System::Void button7_Click(System::Object^ sender, System::EventArgs^ e) {
if (label1->Text == "0")
{
label1->Text = "7";
}
else
{
label1->Text = label1->Text + "7";
}
}
// Button 8
private: System::Void button8_Click(System::Object^ sender, System::EventArgs^ e) {
if (label1->Text == "0")
{
label1->Text = "8";
}
else
{
label1->Text = label1->Text + "8";
}
}
// Button 9
private: System::Void button9_Click(System::Object^ sender, System::EventArgs^ e) {
if (label1->Text == "0")
{
label1->Text = "9";
}
else
{
label1->Text = label1->Text + "9";
}
}
// Button 0
private: System::Void button0_Click(System::Object^ sender, System::EventArgs^ e) {
if (label1->Text != "0")
{
label1->Text = label1->Text + "0";
}
}
// Button Clear
private: System::Void buttonClear_Click(System::Object^ sender, System::EventArgs^ e) {
label1->Text = "0";
}
/////////////////////////////////////////////////////
// atof function converts a C-string into a double.//
// See page 561 for ohter C-string conversions. //
/////////////////////////////////////////////////////
// Button Plus
private: System::Void buttonPlus_Click(System::Object^ sender, System::EventArgs^ e) {
firstNumber = atof(label1->Text);
label1->Text = "0";
operation = '+';
}
// Button Minus
private: System::Void buttonMinus_Click(System::Object^ sender, System::EventArgs^ e) {
firstNumber = atof(label1->Text);
label1->Text = "0";
operation = '-';
}
// Button Divide
private: System::Void buttonDivide_Click(System::Object^ sender, System::EventArgs^ e) {
firstNumber = atof(label1->Text);
label1->Text = "0";
operation = '/';
}
// Button Times
private: System::Void buttonTimes_Click(System::Object^ sender, System::EventArgs^ e) {
firstNumber = atof(label1->Text);
label1->Text = "0";
operation = '*';
}
// Button Equals
private: System::Void buttonEquals_Click(System::Object^ sender, System::EventArgs^ e) {
secondNumber = atof(label1->Text);
switch (operation)
{
case '+':
result = firstNumber + secondNumber;
break;
case '-':
result = firstNumber - secondNumber;
break;
case '/':
result = firstNumber / secondNumber;
break;
case '*':
result = firstNumber * secondNumber;
break;
}
label1->Text = result;
}
};
}