How to compare chars at c++
Jan 19, 2012 at 3:24pm UTC
Greetings everyone,
I want to compare chars a c++ but i can't find how to do it.
I tried ctrcmp but it's not for chars. (like "a" or "v")
How can i do it? :/
this is the part of the code i use it...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
void insert_C_P()
{
int i,j,answer3;
char answer2;
char_and_priority temp;
do
{
cout<< "Dwste to zeygos poy epithumeitai\n" ;
cout<< "Dwste ton xarakthra\n" ;
cin>> answer2;
cout<< "Dwste ton arithmo prwtaireothtas\n" ;
cin>> answer3;
if ((!isalpha(answer2))||(!isdigit(answer3))||(answer3<1)||(answer3>99))
{
cout<< "De dwsate swsta stoixeia ksanaprospathiste\n" ;
}
}
while ((!isalpha(answer2))||(!isdigit(answer3))||(answer3<1)||(answer3>99));
count1=count1+1;
array[count1].character=answer2;
array[count1].priority=answer3;
i=count1;
j=i/2;
while (((i>1)&&(strcmp(array[i].character,array[j].character)>0))||((i>1)&&(strcmp(array[i].character,array[j].character)=0)))
{
if (strcmp(array[i].character,array[j].character)=0)
{
if (array[i].priority >array[j].priority)
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
i=j;
j=i/2;
}
}
else
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
i=j;
j=i/2;
}
}
for (t=1;t<=count1;t++)
{
cout<< array[t].character << endl;
cout<< ",\n" ;
cout<< array[t].priority<< endl;
cout<< ")\n" ;
}
}
Jan 19, 2012 at 3:29pm UTC
You can compare char using the standard such tests:
==
,
!=
, etc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <iostream>
int main()
{
char a;
char b;
std::cout << "Enter first char (one char only)" << std::endl;
std::cin >> a;
std::cout << "Enter second char (one char only)" << std::endl;
std::cin >> b;
if (a==b)
{ std::cout << "Same" ;}
if (a!=b)
{std::cout << "Different" ;}
return 0;
}
Last edited on Jan 19, 2012 at 3:30pm UTC
Jan 19, 2012 at 3:36pm UTC
i have to find who is bigger... like a>b or e>c
Jan 19, 2012 at 4:00pm UTC
i have to find who is bigger... like a> b or e> c
Have you tried using
>
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <iostream>
int main()
{
char a;
char b;
std::cout << "Enter first char (one char only)" << std::endl;
std::cin >> a;
std::cout << "Enter second char (one char only)" << std::endl;
std::cin >> b;
if (a>b)
{ std::cout << "First one is bigger" ;}
if (a<b)
{std::cout << "Second one is bigger" ;}
if (a==b)
{std::cout << "They are the same" ;}
return 0;
}
Last edited on Jan 19, 2012 at 4:01pm UTC
Jan 19, 2012 at 4:32pm UTC
nope though it woudln't work..gonna try it.
Jan 19, 2012 at 4:55pm UTC
i don't think it works have you tried it?
Jan 19, 2012 at 6:31pm UTC
It does work. You have made a mistake in your code or build. Here's some sample output:
Enter first char (one char only)
a
Enter second char (one char only)
b
Second one is bigger
Enter first char (one char only)
r
Enter second char (one char only)
s
Second one is bigger
Enter first char (one char only)
p
Enter second char (one char only)
e
First one is bigger
Enter first char (one char only)
g
Enter second char (one char only)
g
They are the same
Jan 19, 2012 at 6:38pm UTC
thank you so much :)
Topic archived. No new replies allowed.