Jump to content
Moopler
Sign in to follow this  
hackbotmaple

Help C++ Converting textbox input to int

Recommended Posts

Hello, I would like to know how to take the number input by user in the textbox and convert it into int.

In this case, I'm coding an auto pot.

Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e)
{
	defaultHP = Convert::ToInt32(textBox1->Text);
}

I tried to convert it in this way but my compiler would underline the textBox1 under

defaultHP = Convert::ToInt32(textBox1->Text);

What's the easiest way to do this? 

 

Thanks :)

Share this post


Link to post
43 minutes ago, hackbotmaple said:

Hello, I would like to know how to take the number input by user in the textbox and convert it into int.

In this case, I'm coding an auto pot.


Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e)
{
	defaultHP = Convert::ToInt32(textBox1->Text);
}

I tried to convert it in this way but my compiler would underline the textBox1 under

What's the easiest way to do this? 

 

Thanks :)

try

defaultHP = int::Parse(textBox1->Text);

 

  • Like 1

Share this post


Link to post
1 hour ago, melonisme said:

try


defaultHP = int::Parse(textBox1->Text);

 

Thanks, 

turns out that I forgot to include my header file.

Solution

Void Form1::textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e)

 

Share this post


Link to post
Guest

@hackbotmaple you should always handle exceptions. Your final product is going to be buggy even if you think your issue is currently resolved. Try something like below or do what @melonisme suggested but wrap it in a try / catch block or write something else handling exceptions which could be thrown.

Quote

Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e)
{
    // if input is not a int / fails to convert
    if (!int::TryParse(textBox1->Text, defaultHP)) 
    {
        MessageBox::Show("display some warning message here.");
    }
    // parsing was a success
}


 

Edited by Guest

Share this post


Link to post
6 hours ago, Ezekiel said:

@hackbotmaple you should always handle exceptions. Your final product is going to be buggy even if you think your issue is currently resolved. Try something like below or do what @melonisme suggested but wrap it in a try / catch block or write something else handling exceptions which could be thrown.


 

Thanks for the tips (Y). Added checks for all my functions xD

Share this post


Link to post

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  

×