Converting textbox string to float? - c++

I'm basically trying to write a basic converter in visual studio 2008, and I have 2 text boxes, one which gets input from the user, and one which gives output with the result. When I press the button I want the input from the first textbox to multiply by 4.35 then display in the 2nd textbox. This is my code in the button code so far:
String^ i1 = textBox1->Text;
float rez = (i1*4.35)ToString;
textBox2->Text = rez;
However I'm getting these errors:
f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(148) : error C2676: binary '*' : 'System::String ^' does not define this operator or a conversion to a type acceptable to the predefined operator
f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(148) : error C2227: left of '->ToString' must point to class/struct/union/generic type
f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(149) : error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'float' to 'System::String ^'
Please help I'm going insane on how ridiculously difficult it is to get some input from a textbox in C++. I've googled every error I had and nothing useful came up, I've been searching answers for an hour already, please help.

Fixing it for you,
String^ i1 = textBox1->Text;
float rez = (float)(Convert::ToDouble(i1)*4.35);
textBox2->Text = rez.ToString();
Basically, you want to convert your string to an actual number, do the math, and then make it back into a string for displaying purposes.

You're trying to multiply a string by a double and there is no operator that defines how to do that. You need to convert your string to a double first, and then use that in the calculation.
Then, you're trying to assign a string to a float, which again is nonsense.. You need to calculate the float, then convert it to a string when assigning it to the textbox text field.
Something like:
String^ i1 = textBox1->Text;
float rez = (Convert::ToDouble(i1)*4.35);
textBox2->Text = rez.ToString();

Related

How to convert FText to float in UE4

I'm getting user input from a box in the unreal engine landscape import pane and would like to be able to convert that input to a float. Currently, the text comes in as FText
I've tried casting the resulting FText to float and using the built-in FText::toNumber.
I would like to be able to do something like:
FText mapDeltaX = GetPropertyValueText(PropertyHandle_Scale_X);
float deltaX = (float)mapDeltaX;
But unfortunately I get the error no suitable conversion from "FText" to "float" exists.
You can convert TCHAR* to float using
FCString::Atof(*String);
So in your case you'd convert your FText to FString, then to float:
FCString::Atof(*mapDeltaX.ToString());

Converting time_t to an int

I want to convert the unix timestamp returned by time() as time_t to an integer. I've been searching for a solution for 20 minutes, and decided to ask here.
Every solution I have found has not worked. When trying to cast from time_t to int, I get errors:
long int t = static_cast<long int> time(NULL);
error C2061: syntax error : identifier 'time'
error C2146: syntax error : missing '(' before identifier 'time'
I am very very new to C++. Thanks in advance.
time_t is already an integer, though it's deliberately chosen to be one that stores the system's full range of UNIX time, so I would recommend against this cast.
However, if you insist, you're on the right lines but just got the cast syntax wrong.
In general, statically casting e to T looks like this:
static_cast<T>(e) // <-- parentheses!
Just as the error message told you, you are "missing '(' before identifier 'time'".
So, your expression will be:
long int t = static_cast<long int>(time(NULL));
Just read the errors and insert the 'missing ( before identifier time':
long int t = static_cast<long int>(time(NULL));
static_cast requires the value to be encapsulated in parentheses.
simply add parenthesis around time(NULL):
long int t = static_cast<long int>(time(NULL));

initialization with ' ... ' expected for aggregate object

I'm trying to write a code in C++ that allows you to enter some text and it will open a website with the variable s_input appended to it. However, I get this error:
'system' : cannot convert parameter 1 from 'std::string' to 'const
char *'
I get that error for the last line you see.
cin >> s_input;
transform(s_input.begin(), s_input.end(), s_input.begin(), tolower);
s_input = "start http://website.com/" + s_input + "/0/7/0";
system(s_input);
I am new to C++ and this is more of a learning program.. So please show as many examples as possible! Thanks!
If s_input is a std::string (I'm betting it is):
system(s_input.c_str());
The function system takes a const char* as parameter, as the error message clearly states.

Replace input with " * " C++

i want the user to input a passwort. of course it's a secret passwort so nobody should see it.
so i tried to replace the letters and numbers the user inputs, with ' * '. here is my try.
while ((pw=getch())!='x'){
cout << "*";
strcpy(pwstring,pw);
}
input_pw=atoi(pwstring.c_str());
later i want the 'x' to be a 'enter'. but at the moment it's not important. with this, i get some compiler errors under Visual Studio.
Fehler 3
error C2664: 'strcpy': Konvertierung des Parameters 1 von 'char' in 'char *' nicht möglich c:\users\tim\desktop\kalssnne\methoden.h zeile: 70
i will try to translate this.
error 3
error C2664: 'strcpy': converting of parameter 1 from 'char' to 'char*' is not possible.
official english error code
"'function' : cannot convert parameter number from 'type1' to 'type2'"
thank u: R. Martinho Fernandes
but what does this mean, and how can i fix it?
hope u can help me
greetings.
Your question isn't as much about C++ as it is about how to interact with your terminal. The language is (deliberately) entirely agnostic of how input and output are handled, and everything that you're worried about is how the terminal behaves. As such, any answer will depend heavily on your platform and your terminal.
In Linux, you will probably want to look into termios.h or ncurses.h. There's an old Posix function getpass() which does something similar to what you want, but it's deprecated.
Unfortunately I have no idea how to approach terminal programming in Windows.
On a posix system use getpass (3).
It won't give you asterix echos, instead it echos nothing, but it is the way to do it.
Or if you are on a BSD system you could use readpassphrase (3) which is more flexible than the older call.
as R. Martinho Fernandes says: strcpy doesn't do what you think it does.
strcpy takes a char* buffer, and a char* source, and copies all of the data from the second (up to the first zero character) to the first. The easiest solution is to keep track of the length of pwstring and add characters one at a time:
char pwstring[100];
int length = 0;
while ((pw=getch())!='x' && length < 99){
cout << "*";
pwstring[length] = pw;
length = length + 1;
}
pwstring[length] = '\0';
int pwint = atoi(pwstring);
[EDIT] If pwstring is a std::string, then this becomes REALLY easy, since it already keeps track of it's own length.
std::string pwstring;
while ((pw=getch())!='x'){
cout << "*";
pwstring += pw;
}
int pwint = atoi(pwstring.c_str());
strcpy(pwstring,pw);
I'm guessing that pwstring is a std::string? strcpy is a c function, it acts on 'c' null terminated strings. You are providing it with a c++ string and an int.

C++ Convert SQLVARCHAR to string

I need to convert from a SQLVARCHAR to a string data type
Variable definitions as follows:
string strFirstName;
SQLVARCHAR rtnFirstName[50];
Want to be able to accomplish the following:
if (strFirstName.empty()) strFirstName = rtnFirstName;
Gives an error that the binary '=': no operator found which takes a right-hand operand of type 'SQLVARCHAR[50]' (or there is no acceptable conversion)
What database API are you using? All the Google hits I can find for SQLVARCHAR say it's an unsigned char, so you can do something like this:
strFirstName = reinterpret_cast<char*>(rtnFirstName);