C/C++ Help on char data type

madurax86

Member
Jun 29, 2006
4,385
88
0
can anyone tell me how to compare a char variable to a constant char?

the code below shows what i want to do but it doesnt compile:(
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
char s[10];
int i;
s="sf(sf)fs";
for (i=0;i<10;i++) if (s=="(") cout<<"";
system("PAUSE");
return EXIT_SUCCESS;
}

gives no error but it doesnt make the exe
pls help
thanx
 

x-pert

Member
Jun 13, 2006
20,952
77
0
Can you interpret this in simple terms?

s="sf(sf)fs";
for (i=0;i<10;i++) if (s=="(") cout<<"";


And *argv[] is a pointer to a pointer...


Just add these as well and see
#include <stdio.h>
#include <string.h>
 

madurax86

Member
Jun 29, 2006
4,385
88
0
x-pert said:
Can you interpret this in simple terms?

s="sf(sf)fs";
for (i=0;i<10;i++) if (s=="(") cout<<"";


And *argv[] is a pointer to a pointer...


Just add these as well and see
#include <stdio.h>
#include <string.h>


those are default code in DevC++ they have no effect to char comparing right?
i dont want to include string.h - i went thru it earlier i had problems in converting string to double so im thinkin of not using that in future lesi kramayak nada? u know without strcmp() or str.compare() stuff
 

x-pert

Member
Jun 13, 2006
20,952
77
0
madurax86 said:
those are default code in DevC++ they have no effect to char comparing right?
i dont want to include string.h - i went thru it earlier i had problems in converting string to double so im thinkin of not using that in future lesi kramayak nada? u know without strcmp() or str.compare() stuff

string.h is the common class people use machang....

Is this correct in you opinion?

int main(int argc, char *argv[])
{
char s[10]; // char array of size 10
int i;
s="sf(sf)fs"; // store this string in the char array. which is 8 chars.
for (i=0;i<10;i++) if (s=="(") cout<<""; // compare each of the array elements with '('. if true output blank?
system("PAUSE");
return EXIT_SUCCESS;
}

just to test the coding... get rid of the s=sf.. line and use a cin statement and see...
 

madurax86

Member
Jun 29, 2006
4,385
88
0
x-pert said:
Oh and when comparing characters, you have to use single quotes. not double quotes.

hehe soryyo i didnt c dat blank output scene i just wrote it here...ok i'll use single quotation marks n c
 

madurax86

Member
Jun 29, 2006
4,385
88
0
x-pert said:
string.h is the common class people use machang....

Is this correct in you opinion?

int main(int argc, char *argv[])
{
char s[10]; // char array of size 10
int i;
s="sf(sf)fs"; // store this string in the char array. which is 8 chars.
for (i=0;i<10;i++) if (s=="(") cout<<""; // compare each of the array elements with '('. if true output blank?
system("PAUSE");
return EXIT_SUCCESS;
}

just to test the coding... get rid of the s=sf.. line and use a cin statement and see...


the code is k other than the output blank thing its no use ne :P
 

x-pert

Member
Jun 13, 2006
20,952
77
0
This should do the trick

char s[10]="sf(sf)fs";



This works. I checked with VS.

int main(int argc, char *argv[])
{
char s[10]="sf(sf)fs";
int i;

for (i=0;i<10;i++) if (s=='(') cout<<"";
system("PAUSE");
return EXIT_SUCCESS;
}
I guess you can realise the reason right?
 

madurax86

Member
Jun 29, 2006
4,385
88
0
i got it machan thanks!! but still mingw32 doesnt support char comparing but the borland turbo c++ 5.2 compiler does i'll have to change compilers but then mi proggy wont run on linux neh cant you tell me a way to fix it in mingw32(u know if there is a option for that ...)
 

x-pert

Member
Jun 13, 2006
20,952
77
0
madurax86 said:
i got it machan thanks!! but still mingw32 doesnt support char comparing but the borland turbo c++ 5.2 compiler does i'll have to change compilers but then mi proggy wont run on linux neh cant you tell me a way to fix it in mingw32(u know if there is a option for that ...)
That I'm not quite sure :S

Should be able to use the -gcc compiler though.


edit: -gcc
 
Last edited: