How to Compare two Qstrings? [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have to compare two Qstrings in qt,
say,
Qstring str1="1005",str2="1006";
I have tried using ,
if(str1==str2){
return true;
}
&
if(str1.compare(str2)==0)
{
return true;
}
still both methods goes inside if condition & returns true.

You can use :
int x = QString::compare(str1, str2, Qt::CaseInsensitive); // if strings are equal x should return 0

The code below works fine for me.
int main(int argv, char **args)
{
QString str1="1005",str2="1006";
if(str1 == str2)
qDebug()<<"This should not print";
qDebug()<<"Everything Ok";
}
Output:
Everything Ok
The == operator is overloaded for QStrings, as documented here.
I don't know why your code is not working. Recheck other parts of your code.

It worked after Rebuilding the Project , I think this is the problem with QT CREATOR

Related

Tic-Tac-Toe Game c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
XO Game C++
This code returns only "It’s a Tie !!" .. What's the error in my code?
#include <stdio.h>
int main()
{
char a1,b1,c1,a2,b2,c2,a3,b3,c3;
scanf("%c%c%c\n%c%c%c\n%c%c%c",&a1,&b1,&c1,&a2,&b2,&c2,&a3,&b3,&c3);
if(a1==b1==c1=='X'||a2==b2==c2=='X'||a3==b3==c3=='X'||a1==a2==a3=='X'
||b1==b2==b3=='X'||c1==c2==c3=='X'||a1==b2==c3=='X'||c1==b2==a3=='X'){
printf("X wins\n");
}
if(a1==b1==c1=='O'||a2==b2==c2=='O'||a3==b3==c3=='O'||a1==a2==a3=='O'
||b1==b2==b3=='O'||c1==c2==c3=='O'||a1==b2==c3=='O'||c1==b2==a3=='O'){
printf("O wins\n");
}
if(a1!=b1!=c1||a2!=b2!=c2||a3!=b3!=c3||a1!=a2!=a3
||b1!=b2!=b3||c1!=c2!=c3||a1!=b2!=c3||c1!=b2!=a3){
printf("It’s a Tie !!\n");
}
}
a1==b1==c1=='X'
This doesn't do what you think it does. It compares a1 to the boolean result of b1==c1=='X', probably giving false.
To check whether they are all equal, you need
a1=='X' && b1=='X' && c1=='X'

c++ 2D char array element initializing with const char [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying initialize element of array
array[m][n] == char("X");
after printing that element i'm getting the value of it equals д (Russian d); how deal with it, and I'm not even able to initialize that element without parsing const char to char.
You have to write simply as
array[m][n] = 'X';
where 'X' is a character literal.
Or if you like very much string literals then:)
array[m][n] = *"X";
or
array[m][n] = "X"[0];
EDIT: I am sorry. You have also to use the assignmnet operator (=) instead of the comparison operator (==)

Vector items not being added [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Consider the following source code:
map<string,vector<SectionElement *>> _sections;
...
static SharedData *_shared;
...
static int iniHandler(void* user, const char* section, const char* name,
const char* value)
{
map<string,vector<SectionElement*>> iniFile = *(_shared->sections);
auto& iniSection = iniFile[ section];
auto sectionElement = new SectionElement();
sectionElement->name = name;
sectionElement->value = value;
iniSection.push_back( sectionElement);
return 1;
}
The problem with the code is that if I add an element to iniSection it works, but the vector that is retrieved from iniFile does not seem to be the same that is kept in the map. So every time the function iniHandler is called the count of the vector is zero. I am a bit at a loss here and wondering what obvious thing I am missing...
You modify a local map called iniFile. This has no effect on some other map, *(_shared->sections). Perhaps you wanted to make iniFile a reference?

code infinite loop cannot print only once [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
hi I have this simple code to print out book word when i is equal 2
int i;
for (i=0;i!=5;i++)
{
if(i=2)
{
cout << "book";
};
}
i dont know why this code always looping even though I limit i to 5.
if I print i before if condition its printing this
0book3book3book3book3book3book3book3book3book3book3boo....
im using online compiler to compile this.
anyone know how to solve this?
if(i=2)
should be
if(i==2)
Classic mistake
if(i=2) --> assignment operator
if(i==2) --> comparing. Very different.
if(i=2)
is equivalent to
i = 2;
if(i != 0)
you need
if(i == 2)
Yeah as Roger said what you have done is do an assignment within the if statement instead of a comparison.
if(i=2) //assignment
if(i==2)//comparison
personally when comparing a variable to a raw int etc I like to do the following:
if(2==i) //comparison no error
if(2=i) //attempt to assign to raw int :ERROR
Instruction
if(i=2)
mean: assign value 2 to i and check if i!=0.
You had to do:
if(i==2)
Check if i is equal to value 2.
Simply syntax.

C++ Pointers in function parameters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If I declare a function:
vector<int> & call(string *target)
How should I use target in the function for a comparison with another string? For example:
string str;
if(str == //string in target){
...
}
As &target, or simply target? Also, how should the return look? I'm assuming it should be:
return &some_vector;
since that is the type in the function declaration. Finally, what about the opposite? That is:
vector<int> & call(string &target)
When in the function and wanting to use the string, is it as simple as:
*target
On your first question it's
if(str == *target){
On your second question my advice would simply be, don't. It looks like you are trying to return a reference to a local variable. That is a well known newbie mistake that will simply crash your program.
On your third question it's
if(str == target){
You need to dereference the pointer to get the object. That is, use *:
if (str == *target) {
Also, how should the return look?
You're returning a reference, not a pointer. That means you simply need to return the name of the object:
return some_vector;
Finally, what about the opposite?
If target is a reference (NOT a pointer) than you simply use the name of the object. There's no dereference involved with this.