if QString contains a number - c++

I want to know if a string contains a '1' or '0' so I wrote this code:
if ((input.contains('0'))||(input.contains('1'))) {
ui->answerbox->setText(QString::number(BinToDec(number)));
}
else {
ui->answerbox->setText("Error");
}
If I put in "014", it runs the if block but if I put "4" it runs the else block. What's wrong?

Man this function BinToDec is a total disaster.
bool success;
auto value = input.toInt(&success, 2);
if (success) {
ui->answerbox->setText(QString::number(value));
}
else {
ui->answerbox->setText("Error");
}

Related

Calling next multiple times in loop causes stack overflow in ue4 Lua machine

I've been working on lua machine json object system. But I've hit a snag with the json object merger(You can see the functions code used below). When I try to iterate through a table using a while loop and IterateTable function, and then ether call Next or a function containing Next it causes an infinite loop that leads to a stack overflow crash.
Loops through the map elements of a user data object
void UJsonMergetFunctionLibrary::MergeLuaJsonObject(UMyDynamicObject* Origional, UMyDynamicObject* ToMerge, bool Override)
{
//Get all user data object keys
for (TPair<FString, FLuaValue>& P: ToMerge->GetTableContent())
{
//Holds the value of current key
FLuaValue Hold;
if (Origional->hasField(P.Key))
{
if ((P.Value.Type == ELuaValueType::Table) || (P.Value.Type == ELuaValueType::UObject))
{
Hold = Origional->LuaGetField(P.Key);
MergeLuaJsonObject(Hold, P.Value, Override);
}
else if (Override)
{
Origional->LuaSetField(P.Key, P.Value);
}
}
else
{
Origional->LuaSetField(P.Key, P.Value);
}
}
}
Loops through the elements of ether a lua table or user data object
void UJsonMergetFunctionLibrary::MergeLuaJsonObject(FLuaValue& Origional, FLuaValue& ToMerge, bool Override)
{
if (Origional.Type != ToMerge.Type) {
if (Override)
{
Origional = ToMerge;
return;
}
else
{
return;
}
}
//Table
if ((Origional.Type == ELuaValueType::Table) || (ToMerge.Type == ELuaValueType::Table)) {
TPair<FLuaValue, FLuaValue> Pair;
int32 MaxDex = MaxTableIndex(Origional);
while (IterateTable(ToMerge, Pair)) {
if (Pair.Key.Type == ELuaValueType::Integer)
{
MaxDex += 1;
Origional.SetFieldByIndex(MaxDex, Pair.Value);
}
else if (HasTableField(Origional, Pair.Key.ToString()))
{
FLuaValue Hold = Origional.GetField(Pair.Key.ToString());
if ((Pair.Value.Type == ELuaValueType::Table) && (Hold.Type == ELuaValueType::Table))
{
//Hold = OrigionalObj->LuaGetField(P.Key);
MergeLuaJsonObject(Hold, Pair.Value, Override);
}
else if((Pair.Value.Type == ELuaValueType::UObject) && (Hold.Type == ELuaValueType::UObject))
{
MergeLuaJsonObject(Hold, Pair.Value, Override);
}
else if (Override) {
Origional.SetField(Pair.Key.ToString(), Pair.Value);
}
}
else
{
Origional.SetField(Pair.Key.ToString(), Pair.Value);
}
}
}
//Object
else if ((Origional.Type == ELuaValueType::UObject) || (ToMerge.Type == ELuaValueType::UObject)) {
UMyDynamicObject* ToMergeObj = Cast<UMyDynamicObject>(ULuaBlueprintFunctionLibrary::Conv_LuaValueToObject(ToMerge));
UMyDynamicObject* OrigionalObj = Cast<UMyDynamicObject>(ULuaBlueprintFunctionLibrary::Conv_LuaValueToObject(Origional));
TMap<FString, FLuaValue> Keys = ToMergeObj->GetTableContent();
for (TPair<FString, FLuaValue>& P: Keys)
{
FLuaValue Hold;
if (OrigionalObj->hasField(P.Key))
{
if ((P.Value.Type == ELuaValueType::Table) || (P.Value.Type == ELuaValueType::UObject))
{
Hold = OrigionalObj->LuaGetField(P.Key);
MergeLuaJsonObject(Hold, P.Value, Override);
}
else if (Override)
{
OrigionalObj->LuaSetField(P.Key, P.Value);
}
}
else
{
OrigionalObj->LuaSetField(P.Key, P.Value);
}
//OrigionalObj->LuaSetField(P.Key, P)
}
Origional = FLuaValue(OrigionalObj);
}
}
Iterates and retrieves the next key in a table
bool UJsonMergetFunctionLibrary::IterateTable(FLuaValue Table, TPair<FLuaValue, FLuaValue>& Pair)
{
if (Table.Type != ELuaValueType::Table)
return false;
ULuaState* L = Table.LuaState;
if (!L)
return false;
if (Pair.Key.IsNil())
{
L->FromLuaValue(Table);
L->PushNil(); // first key
}
if (L->Next(-2))
{
Pair.Key = L->ToLuaValue(-2);
Pair.Value = L->ToLuaValue(-1);
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, Pair.Key.ToString());
L->Pop(); // pop the value
return true;
}
else
{
L->Pop(); // pop the table
return false;
}
}
Checks if the table contains a key
bool UJsonMergetFunctionLibrary::HasTableField(FLuaValue Table, FString FieldName)
{
if (Table.Type != ELuaValueType::Table)
return false;
ULuaState* L = Table.LuaState;
if (!L)
return false;
L->FromLuaValue(Table);
L->PushNil(); // first key
while (L->Next(-2))
{
FLuaValue Key = L->ToLuaValue(-2);
if (Key.ToString() == FieldName)
{
L->Pop(); // pop the value
L->Pop(); // pop the table
return true;
}
else {
L->Pop(); // pop the value
}
}
L->Pop(); // pop the table
return false;
}
1.I have tried poping both the value and the key when calling next within the loop, and than reassigning the old key once the internal next call ends.
2.I have tried googling it and Wasn't able to find anything.
3.I added a limit to how many times the loop can execute, to test if a infinite loops is causing the crash.
4.I tried making a function in pure lua that loops through a table and calls next within it(Didn't work, but do note I'm new to lua).
None of what I've tried has worked. What I want to happen is that when both Original and ToMerge have the same key, and the value assigned to that key is table I want it to perform recursion and merge those two tables than once thats done return to the execution on the original loop. Any help would be appreciated.

Do I need to add some kind of check before continuing when hopping to other void

I'm new(-ish) to C++, and I'm trying to learn something new every day. Today I'm trying to figure this out.
Do I have to check if Valmis == 1 so it won't continue/return before Realtest() is complete?
This is just an example.
int valmis = 0;
void test::Main()
{
//just some basic stuff
do
{
if (!strcmp())
{
//here too
float testing = 0;
printf("Test? 1=do it Else=Nothing\n");
scanf(" %f", &testing);
if (testing == 1)
{
Realtest();
//Realtest needs to be completed before continuing
if (valmis == 1) //Do I need this or does it continue after RealTest() is complete without this?
return (somethingmate = true);
}
else
{
return (somethingmate = true);
}
}
} while();
return (somethingmate = false);
}
void test::Realtest()
{
//doing something here that I need to do before continuing in main/letting somethingmate to become true
valmis = 1; //do i need this?
}

std::unordered_map::count is not working in my code

I have a doubt with the solution of this question which is stated below -
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Strings["aa", "ab"] should return false and strings["aa", "aab"] should return true according to question.
Here is the code which I have attempted in the first place and I'm not getting a required output as mentioned above.
unordered_map<char,int>umap;
for(char m:magazine)
{
umap[m]++;
}
for(char r:ransomNote)
{
if(umap.count(r)<=1)
{
return false;
}
else{
umap[r]--;
}
}
return true;
}
In the above code, I have used umap.count(r)<=1 to return false if there is no key present.
For the strings ["aa","aab"], it is returning true but for strings ["aa","ab"], it is also returning true but it should return false.
Then I used another way to solve this problem by using just umap[r]<=0 in the place of umap.count(r)<=1 and it is working just fine and else all code is same.
bool canConstruct(string ransomNote, string magazine) {
unordered_map<char,int>umap;
for(char m:magazine)
{
umap[m]++;
}
for(char r:ransomNote)
{
if(umap[r]<=0)
{
return false;
}
else{
umap[r]--;
}
}
return true;
}
I'm not able to get what i'm missing in the if condition of first code. Can anyone help me to state what I'm doing wrong in first code. Any help is appreciated.
unordered_map::count returns the number of items with specified key.
As you don't use multi_map version, you only have 0 or 1.
Associated value doesn't change presence of key in map.
To use count, you should remove key when value reaches 0:
for (char r : ransomNote) {
if (umap.count(r) == 0) {
return false;
} else {
if (--umap[r] == 0) {
umap.erase(r);
}
}
}
return true;

How do I separate String and Integer variables in same one resources

I'm bit hardly to separate the variable String and Integer to passed in "if()" statement.
I have two variable "buffString" and "buffInteger", when i put the buffString in the "if()" statement, I wanna to take String "Data Stop" in the buffString variable, and then set to the RichEdit to showing "Data Stop", but the problem when the buffInteger filled same as buffString its error come in the process. and compilers say "Data Stop" is not valid Integer value
void __fastcall TfrmServer::ComPort1RxChar(TObject *Sender, int Count)
{
String buff;
ComPort1->ReadStr(buff,255);
BUF_RX->Data_suhu = buff; // source
int bufferInteger;
String bufferString;
try{
if(!buff.IsEmpty())
{
bufferInteger = StrToInt(BUF_RX->Data_suhu);
bufferString = BUF_RX->Data_suhu;
if(bufferString == "Data Stop")
{
ad_log_Serial("AT89S52 -> "+BUF_RX->Data_suhu,clRed);
}
else
{
ad_log_Serial("AT89S52 -> "+IntToStr(bufferInteger),clRed);
txtTemp->Text = IntToStr(bufferInteger).c_str();
}
LedRx->States->Items[0]->Value = true;
}
else
{
LedRx->States->Items[0]->Value = false;
}
}__finally
{
delete(BUF_RX);
}
}
what should i do this code?
Move your bufferInteger assignment to the else clause:
else
{
bufferInteger = StrToInt(BUF_RX->Data_suhu);
ad_log_Serial("AT89S52 -> "+IntToStr(bufferInteger),clRed);
txtTemp->Text = IntToStr(bufferInteger).c_str();
}
Since it's not being used outside it, there's no point in having it there. And assuming that any string that's not "Data Stop" is supposed to represent an integer, the code will not fail this way.

C++ array - uses only first line

i have problems with the array function.. -
i put my mac on fist line and then says me approved , but if is on 2st line rejected. my real one is B6 on end. /address down are not real../
in heder file settings >
#define CLIENTSNUMBER 2
BOOL Checking2(LPCSTR MacID);
cpp >
char ClientMacs[CLIENTSNUMBER*1][18] = {
"5A-77-77-97-87-B7",
"5A-77-77-97-87-B6"
};
BOOL Checking2(LPCSTR MacID)
{
for(int x=0;x<CLIENTSNUMBER;x++)
{
if(!strcmp(MacID,ClientMacs[x]))
{
MessageBoxA(NULL,MacID,"APPROVED!",MB_OK);
return false;
} else {
MessageBoxA(NULL,MacID,"REJECTED!",MB_OK);
return false;
}
}
return false;
}
Because you return from your function (breaking out of your loop) when something matches or doesn't match. It will never actually loop.
Edit because it's a slow morning:
You need to go through the entire array and look at every element for a match before declaring it's rejected:
BOOL Checking2(LPCSTR MacID)
{
for(int x=0;x<CLIENTSNUMBER;x++)
{
if(strcmp(MacID,ClientMacs[x]) == 0)
{
MessageBoxA(NULL,MacID,"APPROVED!",MB_OK);
return false;
}
}
MessageBoxA(NULL,MacID,"REJECTED!",MB_OK);
return false;
}
Also, do you really mean to return false in both cases? I would assume if you find a match it should return true