String to Integer Conversion in MFC [duplicate] - c++

This question already has answers here:
Convert MFC CString to integer
(12 answers)
Closed 7 years ago.
I am just a beginner to MFC platform. I am just trying a simple pgm. Getting 2 numbers in 2 edit boxes and displaying the sum in the third edit box when a button is clicked.
This is my code:
void CMineDlg::OnEnChangeNumber1()
{
CString strNum1,strNum2;
m_Number1.GetWindowText(strNum1,10); //m_NUmber1 is variable to 1st edit box.
m_Number2.GetWindowText(strNum2,10); //m_Number2 is variable to 2nd edit box.
}
void CMineDlg::OnBnClickedSum()
{
m_Result=m_Number1+m_Number2;
}
I know I have to convert the strings to integer. But I have no idea how to do it. Pls Help.

You can use Class Wizard to add variables of integer type and associate them with edit boxes. Then, in OnEnChangeNumber1 event handler (or in OnBnClickedSum), you simply call UpdateData(TRUE); which causes those variables to update their values. After that, you can sum those integer variables.

Use
CString strNum = _T("11"); //CString variable
int num; //Integer Variable
_stscanf(strNum, _T("%d"), &num); //Conversion
Or
num = atoi((char*)(LPCTSTR)strNum);

The correct UNICODE compliant way of doing this:
CString str = _T("10");
int nVal = _ttoi(str);
__int64 = _ttoi64(str);

Related

converting part of string to int and back creates random character [duplicate]

This question already has answers here:
Convert char to int in C and C++
(14 answers)
Closed 2 years ago.
I am having trouble trying to converting time from 12 hour format to 24 hour format.
Note:- This is from an online test site and is filled with boilerplate code to work with the site, so I'm only going to post the part of the program where I am allowed to type.
string timeConversion(string s) {
/*
* Write your code here.
*/
int hours = ((int) s[0])*10+((int) s[1]);
char r[7];
//cout<< sizeof(s)<<"\n";
if(s[8]=='P')
{
hours=hours+12;
r[0]=(char) (hours/10);
r[1]=(char) (hours%10);
for (int i=2;i<8;i++)
{
r[i]=s[i];
}
}else
{
for(int i=0;i<8;i++)
{
r[i]=s[i];
}
}
return r;
}
Here is the input and outputs of test
Input(stdin):-
07:05:45PM
My output(stdout):-
6:05:45
Expected output:-
19:05:45
Now I test line 5 (i.e the line where i convert the hours section into an integer) in another compiler by itself and for some reason instead of properly converting its showing hours=534
Can you guys tell me what went wrong and how to fix it?
I found out what was going wrong with my code earlier. The reason why it didnt work is because c++ doesn't convert the numerical character into the actual numerical value, rather it converts it into the corresponding ascii value for which starts at 48 for "0" and ends at 57 for "9"
Statement to use for converting hours in the string and vice versa would be to
string -> int
int hours = (((int) s[0])%48)*10+(((int) s[1])%48);
or
int hours = (((int) s[0])-48)*10+(((int) s[1])-48);
int -> string
r[0]=(char) ((hours/10)+48);
r[1]=(char) ((hours%10)+48);

How to display concatenated string and int in qlineedit or qtextedit? [duplicate]

This question already has answers here:
How to use std::string in a QLineEdit?
(2 answers)
Closed 2 years ago.
I have here:
int num = 000;
std::string result;
std::string text = "asdasd";
result += text + std::to_string(num);
I want to display the result in qlineedit.
Since you are using qt, you should use QString instead of std::string to avoid many unnecessary conversions.
Indeed, QString is the string class used by all the Qt library (containers, ...).
Your sample will become then:
int num = 0;
QString text = "asdasd";
QString result = text + QString::number(num);
Then, you'll be able to write:
my_widget->setText(result); // QLineEdit or QTextEdit
If you really want to use a std::string anyway, you'll need to convert it to a proper QString. You could use QString::fromStdString() for this purpose.
For example:
my_widget->setText(QString::fromStdString(result));

weird when use atof in Visual Studio [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
When I used VS 2015 to debug the following code,
int main()
{
char thev[8] = "0.12345";
float fv = atof(thev);
printf("%f\n", fv);
return 0;
}
the value of fv in watch window is 0.12345004,
and printf is 0.123450, how to let fv=0.12345 in the run time?
There is similar post, here, but no answer there, can somebody help me?
And I pasted that code to my VS 2015,
int main()
{
const char *val = "73.31";
std::stringstream ss;
ss << val;
double doubleVal = 0.0f;
ss >> doubleVal;
}
the value doubleVal in watch window is 73.310000000000002
Replace the following code:
printf("%f\n", fv);
with:
printf("%.5f\n", fv);
Explaination:
we use a width (%.5f) to say that we want 5 digits (positions) reserved for the output.
The result is that 5 “space characters” are placed before printing the character. And the next character will be not printed.
Reference: https://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output
There is no exact representation in IEEE single precision for 0.12345. The closest two values on either side of it are 0.123450003564357757568359375 and 0.12344999611377716064453125. atof is picking the former, I think because it is closer to 0.12345 than the latter.

c++ How to convert String to int (ascii code to value); [duplicate]

This question already has answers here:
How to convert a single char into an int [duplicate]
(11 answers)
Closed 5 years ago.
i have been trying to make a program that checks if a national identification number is valid how ever i have run into a issue and i can't find an answer anywhere.
I am using a string to store the users input of the identification code and i need to somehow covert the string to int keeping the symbol instead of getting an ascii value.
Example:
(lets say the user inputs the string persKods as 111199-11111
the 5th symbol is 9 so the year value should output as 9 instead of 54 which is its ascii value)
int day,month,year;
year=this->persKods.at(4);
cout << year; // it outputs 54 instead of 9 :/
Can you try to ascci value of '0'.
#include <iostream>
using namespace std;
int main() {
// your code goes here
string str="111199";
int test = str.at(4) - '0';
cout<<test;
return 0;
}
For more information link

Check input is a valid integer [duplicate]

This question already has answers here:
How to determine if a string is a number with C++?
(36 answers)
Closed 10 months ago.
Hi Can anyone help me please. I need to check that my input only contains integers. Im guessing from looking it up that I use the isDigit function but I am not sure how to use this to check the whole number.
I'm using C++ to interact with MSI so i'm getting the integer as follows:
hr = WcaGetProperty(L"LOCKTYPE",&szLockType);
ExitOnFailure(hr, "failed to get the Lock Type");
I think i have to change szLockType to a char and then use isdigit to scan through each character but i am not sure how to implement this. Any help would be greatly appreciated. P.s im a beginner so please excuse if this is a really trivial question..:)
Use std::stoi(). You'll get an exception if the string is not an integer value.
What's the type of szLockType?
Is it a a null-terminated char-string?
Then you can use the array syntax to get individual characters.
for(int i = 0; i < std::strlen(szLockType); i++) {
if(!std::isDigit(szLockType[i])) {
// it contains a non-digit - do what you have to do and then...
break; // ...to exit the for-loop
}
}
Or is it a std::string? Then the syntax is slightly different:
for(int i = 0; i < szLockType.length(); i++) {
if(!std::isDigit(szLockType.at(i)) {
// it contains a non-digit - do what you have to do and then...
break; // ...to exit the for-loop
}
}
Even better, with modern C++ you can do this:
#include <algorithm>
#include <cctype>
auto lambda = [](auto elem)
{
return std::isdigit(elem);
};
return std::all_of(szLockType, szLockType + strlen(szLockType), lambda);
Your choice as to whether you prefer a named lambda or regular, anonymous lambda.
FYI it is std::isdigit rather than isDigit.
https://en.cppreference.com/w/cpp/string/byte/isdigit