|
You are comparing a character array (a pointer) with a pointer to a constant string:
if(gend == "female")
Those pointers will never be identical.
If you want to compare strings (the content pointed to by the pointers), you must compare each element (character) of the strings. There is the strcmp[^] C standard library function to do this:
if (!strcmp(gend, "female")) 
|
|
|
|
|
ohh.. i see.. thank you very much :3
|
|
|
|
|
I wonder: what IDE / compiler are you using? Isn't the debugging possible?
|
|
|
|
|
it is possible.. btw im just a beginner :3
|
|
|
|
|
This is very interesting. Thanks for your support. Getting a wonderful experience. With best compliments.
Shiv Prakash
|
|
|
|
|
I have an application in Visual C++ 6.0, but I need to send an arrangement of bytes to another computer through a net cable, using socket. Please that somebody please tells me how to make it or that they show me an example.
|
|
|
|
|
Plenty of example out there on the web. Did you look?
Search[^]
|
|
|
|
|
|
Hello,
Could you please provide some examples "
In C++ compiler providing the default copy constructor ,then why do we need copy constructor . "
Thanks and Regards,
Ranjithkumar.S
ranjith
|
|
|
|
|
When you want something other than the default behavior.
|
|
|
|
|
provide some example
ranjith
|
|
|
|
|
If this was a write code to order service, what sort of an example would I write for you? But since this is not a coding service, no, I won't do your homework for you.
|
|
|
|
|
|
Copy constructors - cppreference.com[^]
Default Copy Constructor - C++ Forum[^]
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Basically when you need custom logic in copying. For example, dynamic memory allocation in pointer members.
Same thing is applicable in assignment operator overloading too.
|
|
|
|
|
There are tons of examples out there, here is but one[^].
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
May be it is orthogonal information, If we want to hide/restrict usage of default copy constructor, we need to provide override declaration in private section of class, and no definition for that. This ensures preventing usages of copy constructor
|
|
|
|
|
I have read a lot of stuf of programmers triying to swap arrays using for over every element like here (two pages):
[^]
There also other methods that involves making swapping element by element but it is hidden:
std::swap(array1,array2);
It would be better using pointers, with only 3 operations all the elements are swapped:
double *a,*b; two arrays
double **ptr1,**ptr2,**ptrx;
ptr1=&a;ptr2=&b;
Swapping:
ptrx=ptr1;ptr1=ptr2;ptr2=ptrx;
Example code:
<pre lang="c++">
#include <iostream>
using namespace std;
double *a;
double *b;
class c_class { public: double x; } ;
void main()
{
a = new double[7];
b = new double[7];
long i;
for (i = 0; i < 7; i++)
{
a[i] = i + 100;
b[i] = i + 300;
}
double **ptr1 = &a,**ptr2=&b,**ptrx;
cout << "=== SWAPPING ARRAYS ===" << endl;
cout << "STEP 1: ptr1= " << endl;
for (i = 0; i < 7; i++) cout << (*ptr1)[i] << " ";
cout << " ptr2= ";
for (i = 0; i < 7; i++) cout << (*ptr2)[i] << " ";
cout << endl;
cout << " Swapping the arrays" << endl;
ptrx = ptr1; ptr1 = ptr2; ptr2 = ptrx;
cout << "STEP 2: ptr1= ";
for (i = 0; i < 7; i++) cout << (*ptr1)[i] << " ";
cout << " ptr2= ";
for (i = 0; i < 7; i++) cout << (*ptr2)[i] << " ";
cout << endl;
cout << " Swapping the arrays" << endl;
ptrx = ptr1; ptr1 = ptr2; ptr2 = ptrx;
cout << "STEP 3: ptr1= ";
for (i = 0; i < 7; i++) cout << (*ptr1)[i] << " ";
cout << " ptr2= ";
for (i = 0; i < 7; i++) cout << (*ptr2)[i] << " ";
cout << endl;
cout << "\n\n=== SWAPPING CLASSES ==="<<endl;
c_class clas1, clas2;
clas1.x = 1000.1;
clas2.x = 5000.5;
c_class *pc1, *pc2,*pcx;
pc1 = &clas1; pc2 = &clas2;
cout << "Class 1 and 2 x value: " << pc1->x << " "<<pc2->x<<endl;
cout << " Swapping the classes " << endl;
pcx = pc1; pc1 = pc2; pc2 = pcx;
cout << "Class 1 and 2 x value: " << pc1->x << " " << pc2->x << endl;
cout << "\n\n======end========\n"; getchar(); getchar();
The only problem is that the arrays must be declared initially as pointers. If they are declared as:
double a[7];, the vs2013 does mark pointer assigment as an error:
double *ptr=a;
Is there a way to solve it?
|
|
|
|
|
All you are doing there is changing the pointers, which makes no real sense. What problem are you trying to solve?
|
|
|
|
|
Given his code it will be a sort routine like bubble sort, but it looks like homework to me so I am reluctant.
Usually they are asked to do an in place array sort and it. He hasn't worked out how to typecast the pointers properly and it seems to escape him that you can just malloc the arrays which is what his "new" statement hides.
In vino veritas
modified 20-Sep-16 6:53am.
|
|
|
|
|
The idea is use the pointers to avoid copying all the data.
An example, if there are 3 arrays of pixels: A, B, C and I want to order the arrays I can make the following operation:
if (distance(A,B)>distance(A,C))
swap(B,C);
I tried this code that swaps anything but it does not compile:
#include <iostream>
#include <string>
using namespace std;
void swap(void *&a,void *&b)
{
void *x=a;a=b;b=x;
}
void main()
{
int a[3]={100,101,102};
int b[3]={200,201,202};
cout << "a:"<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
cout << "b:"<<b[0]<<" "<<b[1]<<" "<<b[2]<<endl;
swap((void *) a,(void *) b);
cout << "a:"<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
cout << "b:"<<b[0]<<" "<<b[1]<<" "<<b[2]<<endl;
cout << "=== END ===" << endl; getchar();
}
modified 18-Aug-17 5:26am.
|
|
|
|
|
Why are you using void* in your swap method and calls? Void pointers do not point to anything so the compiler cannot figure out what to do. Since both arrays are of int type, then that is the type of pointer you should be using. Also your swap method does not make much sense, again you are swapping pointers not data.
|
|
|
|
|
Because I would like to swap pointers, not to copy all the data. If I swap 2 arrays of 1k size using pointers is 1000x times faster than copying all the data.
This code do it using void * pointers (but I do not know if it could work in other SO), so the next step is to place it in a function:
int a[3]={100,101,102};
int b[3]={200,201,202};
void *x;int *a1=a,*b1=b;
x=(void *) a1;a1=b1;b1=(int *) x;
cout << "a:"<<a1[0]<<" "<<a1[1]<<" "<<a1[2]<<endl;
cout << "b:"<<b1[0]<<" "<<b1[1]<<" "<<b1[2]<<endl;
I tried this function using C++11 and seems to work:
template<class A>
void swap(A *&a,A *&b)
{
A *x=a;a=b;b=x;
}
void main()
{
int a[3]={100,101,102};
int b[3]={200,201,202};
int *a1=a,*b1=b;
swap(a1,b1);
cout << "a:"<<a1[0]<<" "<<a1[1]<<" "<<a1[2]<<endl;
cout << "b:"<<b1[0]<<" "<<b1[1]<<" "<<b1[2]<<endl;
}
Unfortunately I cannot call swap using a and b:
swap(a,b)
modified 21-Aug-17 3:52am.
|
|
|
|
|
void *x;int *a1=a,*b1=b;
x=(void *) a1;a1=b1;b1=(int *) x;
That just makes no sense, x should be int* the same as a and b .
|
|
|
|
|
As I am sure this is homework I won't give you the answer, what I will tell you is the problem with the typecast
Using the dereference operator (the *) is like mathematics there is an order to things just like in mathematics.
If I gave you 2 + 3 * 4 and you wanted the add before the multiply you need brackets (2 + 3) * 4 the mult carries higher precedence normally.
Same problem happens in dereference operator when using arrays
double **a;
*a[2] has two possible breakdowns so lets use brackets (*a)[2] or *(a[2])
*a[2] is actually equivalent to the latter *(a[2])
I bet that is not what you were expecting and reading it as
Your code doesn't do anything like what your text comments say because you are missing brackets. You need the dereference completed prior to the array use and (*a)[2] is the correct use for you.
Lets give you a simple code
double myArray[6] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 };
double *p = &myArray[0];
double d = p[3];
double **q = &p;
double s = (*q)[3];
double t = *q[2];
Does that last line look familiar?
It is the same if the you are derefencing an object or a class
*a.xyz may naively be intended as (*a).xyz or *(a.xyz)
*a->xyz may naively be intended as (*a)->xyz or *(a->xyz)
So look up and learn the precedence order of dereferencing and if you want the other USE BRACKETS. It's actually good practice to always use brackets if you are dereferencing complex items so you know for sure what the order will be be, you do the same with long chains with mathematics. It's a normal gotcha to not dereference what you think you are on complex items because of dereference precedence order (you can't just read it left to right and expect that order).
In vino veritas
modified 21-Sep-16 1:38am.
|
|
|
|