comparison function of string and integer - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

comparison function of string and integer

shawnlawshawnlaw Posts: 48Member
i need some help in c to compare 2 interger. iam just learn this whole comparison functions so i have only a basic knowledge of it.
i under stand the steps to compare 2 strings. i have and example of how i would do that which is

using string library


[code]
void menu(link *node)
{
char fname[15],lname[15];

printf("first name?
");
scanf("%*[
]%14[^
]",fname);
printf("last name?
");
scanf("%*[
]%14[^
]",lname);

while (node!=NULL)
if (strcmp(fname,node->record.f_name)||strcmp(lname,node->record.l_name))

node=node->next;
else break;
if (node==NULL)
printf("
");
else
{
printf("Name:%s %s
",node->record.f_name,node->record.l_name);
}
[/code]

that how to do comparison of a string. but i want to compare two integer ida and idb. but i realise i cant use strcmp because it only compares string could someone tell me what libray to use to compare integers and wat comparison funtion, for example the strcmp of the string hindling library.

this is where the error is
if (WHAT SHOOULD BE HERE(ida,node->record.id_a)||WHAT SHOOULD BE HERE(idb,node->record.id_b))

[code]
void menu(link *node)
{
int ida,idb;
printf("id one?
");
scanf("%d",&ida);
printf("id two?
");
scanf("%d",&idb);

while (node!=NULL)
if ((ida,node->record.id_a)||(idb,node->record.id_b))

node=node->next;
else break;
if (node==NULL)
printf("
");
else
{
printf("id numbers:%d %d
",node->record.id_a,node->record.id_b);
}

[code][/code]
«1

Comments

  • IDKIDK Posts: 1,784Member
    <, >, == are all comparison functions. They work as expected and are very easy to use.

    [grey]The one and only [b]Niklas Ulvinge[/b][/grey] [white]aka [b]IDK[/b][/white]

  • shawnlawshawnlaw Posts: 48Member
    : <, >, == are all comparison functions. They work as expected and are very easy to use.
    :
    : [grey]The one and only [b]Niklas Ulvinge[/b][/grey] [white]aka [b]IDK[/b][/white]
    :
    :
    ok i understand that but how do i compare
    [code]if ((ida,node->record.id_a)||(idb,node->record.id_b)) [/code]

    remeber its in a linked list
  • IDKIDK Posts: 1,784Member
    : : <, >, == are all comparison functions. They work as expected and are very easy to use.
    : :
    : : [grey]The one and only [b]Niklas Ulvinge[/b][/grey] [white]aka [b]IDK[/b][/white]
    : :
    : :
    : ok i understand that but how do i compare
    : [code]if ((ida,node->record.id_a)||(idb,node->record.id_b)) [/code]
    :
    : remeber its in a linked list
    :
    I'm not very fammiliar with C/C++ syntax but I think you could do write it like that in new C++, otherwise you'll need to do like this
    (proveided that id_a is an integer)
    [code]if ((ida,node->record.id_a = 0)||(idb,node->record.id_b=0)) [/code]

    Lol, it took me 5 min to write this...
    I'm very unfamiliar with this unix keyboard...

  • shawnlawshawnlaw Posts: 48Member
    : : : <, >, == are all comparison functions. They work as expected and are very easy to use.
    : : :
    : : : [grey]The one and only [b]Niklas Ulvinge[/b][/grey] [white]aka [b]IDK[/b][/white]
    : : :
    : : :
    : : ok i understand that but how do i compare
    : : [code]if ((ida,node->record.id_a)||(idb,node->record.id_b)) [/code]
    : :
    : : remeber its in a linked list
    : :
    : I'm not very fammiliar with C/C++ syntax but I think you could do write it like that in new C++, otherwise you'll need to do like this
    : (proveided that id_a is an integer)
    : [code]if ((ida,node->record.id_a = 0)||(idb,node->record.id_b=0)) [/code]
    :
    : Lol, it took me 5 min to write this...
    : I'm very unfamiliar with this unix keyboard...
    :
    :
    thanks for helping but i dont think that will work

    look at this

    [code]
    while (node!=NULL)
    if (strcmp(fname,node->record.f_name)||strcmp(lname,node->record.l_name)) [/code]

    fname and land are char
    the strcmp compares the two strings.

    now what i need is a comparison function to compare 2 integer
    look at this

    [code]
    if (WHAT SHOOULD BE HERE(ida,node->record.id_a)||WHAT SHOOULD BE HERE(idb,node->record.id_b)) [/code]

    id_a and id_b are int

    what can i place where i have "WHAT SHOOULD BE HERE" to compare them
  • DonotaloDonotalo Posts: 715Member
    : this is where the error is
    : if (WHAT SHOOULD BE HERE(ida,node->record.id_a)||WHAT SHOOULD BE HERE(idb,node->record.id_b))
    :
    : [code]
    : void menu(link *node)
    : {
    : int ida,idb;
    : printf("id one?
    ");
    : scanf("%d",&ida);
    : printf("id two?
    ");
    : scanf("%d",&idb);
    :
    : while (node!=NULL)
    : if ((ida,node->record.id_a)||(idb,node->record.id_b))
    :
    : node=node->next;
    : else break;
    : if (node==NULL)
    : printf("
    ");
    : else
    : {
    : printf("id numbers:%d %d
    ",node->record.id_a,node->record.id_b);
    : }[/code]
    [purple]
    if u want to check whether ida and node->recodrd.id_a are equal or not, then simply use this:
    [code]
    if ((ida [red]==[/red] node->record.id_a)||(idb [red]==[/red] node->record.id_b))
    [/code]
    [/purple]
    [hr][purple]~Donotalo()[/purple]

  • IDKIDK Posts: 1,784Member

    Aha, thats why I didn't get your syntax, the other comp of mine got a very bad font, and I thought that , was . ...
    ok, then it should be like this:

    : [code]
    : if ((ida==node->record.id_a)||HERE(idb==node->record.id_b)) [/code]

  • shawnlawshawnlaw Posts: 48Member
    : : this is where the error is
    : : if (WHAT SHOOULD BE HERE(ida,node->record.id_a)||WHAT SHOOULD BE HERE(idb,node->record.id_b))
    : :
    : : [code]
    : : void menu(link *node)
    : : {
    : : int ida,idb;
    : : printf("id one?
    ");
    : : scanf("%d",&ida);
    : : printf("id two?
    ");
    : : scanf("%d",&idb);
    : :
    : : while (node!=NULL)
    : : if ((ida,node->record.id_a)||(idb,node->record.id_b))
    : :
    : : node=node->next;
    : : else break;
    : : if (node==NULL)
    : : printf("
    ");
    : : else
    : : {
    : : printf("id numbers:%d %d
    ",node->record.id_a,node->record.id_b);
    : : }[/code]
    : [purple]
    : if u want to check whether ida and node->recodrd.id_a are equal or not, then simply use this:
    : [code]
    : if ((ida [red]==[/red] node->record.id_a)||(idb [red]==[/red] node->record.id_b))
    : [/code]
    : [/purple]
    : [hr][purple]~Donotalo()[/purple]
    :
    : [code] [/code]
    ok i dont think ur understanding what goin on.
    take a look at this
    [code]
    void menu(link *node)
    {
    char fname[15],lname[15];

    printf("first name?
    ");
    scanf("%*[
    ]%14[^
    ]",fname);
    printf("last name?
    ");
    scanf("%*[
    ]%14[^
    ]",lname);

    while (node!=NULL)
    if (strcmp(fname,node->record.f_name)||strcmp(lname,node->record.l_name))

    node=node->next;
    else break;
    if (node==NULL)
    printf("
    ");
    else
    {
    printf("Name:%s %s
    ",node->record.f_name,node->record.l_name);
    }

    [/code]

    u notice in that function i have fname is referring to node->record.f_name. in other words fname is goin to be saved in node->record.f_name

    therefore [code] if (strcmp(fname,node->record.f_name)||strcmp(lname,node->record.l_name)) [/code] is correct.
    wat this is sayinh if first name is and the last name does not link to wat is saved in the link list an error message should be displayed. else it print them out.

    f_name is is a struct.

    so if (WHAT SHOOULD BE HERE(ida,node->record.id_a)||WHAT SHOOULD BE HERE(idb,node->record.id_b))
    is the right format just that a comarison comand should be put where
    "WHAT SHOOULD BE HERE" is.

    but i really dont know if there is a function to compare two integer like this thou
  • IDKIDK Posts: 1,784Member

    : so if (WHAT SHOOULD BE HERE(ida,node->record.id_a)||WHAT SHOOULD BE HERE(idb,node->record.id_b))
    : is the right format just that a comarison comand should be put where
    : "WHAT SHOOULD BE HERE" is.
    :
    : but i really dont know if there is a function to compare two integer like this thou
    :
    Why doesn't == work ?

    If you got 2 integers and then want to compare them, the operator (wich is also a func) is ==
    If you want to I think you could do like this:
    [code]
    if (int.operator==(ida,node->record.id_a)||int.operator==(idb,node->record.id_b))
    [/code]
    It is the same as:
    [code]
    if ((ida==node->record.id_a)||(idb==node->record.id_b))
    [/code]
  • DonotaloDonotalo Posts: 715Member
    : [code]
    : void menu(link *node)
    : {
    : char fname[15],lname[15];
    :
    : printf("first name?
    ");
    : scanf("%*[
    ]%14[^
    ]",fname);
    : printf("last name?
    ");
    : scanf("%*[
    ]%14[^
    ]",lname);
    :
    : while (node!=NULL)
    : if (strcmp(fname,node->record.f_name)||strcmp(lname,node- >record.l_name))
    :
    : node=node->next;
    : else break;
    : if (node==NULL)
    : printf("
    ");
    : else
    : {
    : printf("Name:%s %s
    ",node->record.f_name,node->record.l_name);
    : }
    : [/code]
    :
    : u notice in that function i have [red]fname is referring to node->record.f_name. in other words fname is goin to be saved in node->record.f_name[/red]
    [purple]
    completely wrong! [blue]strcmp(const char*, const char*)[/blue] is a function that compares two string provided by the parameters. it returns 0 if they are same. fname is not going to be saved in [blue]node->record.f_name[/blue].
    [/purple]
    :
    : therefore [code] if (strcmp(fname,node->record.f_name)||strcmp(lname,node->record.l_name)) [/code] is correct.
    : wat this is sayinh if first name is and the last name does not link to wat is saved in the link list an error message should be displayed. else it print them out.
    :
    : [red]f_name is is a struct.[/red]
    [purple]
    r u talking about node->record.f_name? then u cant use the strcmp function in the above way. that shud be a compile time error.

    and whatever IDK said is correct also. comparing two integers using [blue]==[/blue] is really calls a comparison function.
    [/purple]
    [hr][purple]~Donotalo()[/purple]

  • shawnlawshawnlaw Posts: 48Member
    : : [code]
    : : void menu(link *node)
    : : {
    : : char fname[15],lname[15];
    : :
    : : printf("first name?
    ");
    : : scanf("%*[
    ]%14[^
    ]",fname);
    : : printf("last name?
    ");
    : : scanf("%*[
    ]%14[^
    ]",lname);
    : :
    : : while (node!=NULL)
    : : if (strcmp(fname,node->record.f_name)||strcmp(lname,node- >record.l_name))
    : :
    : : node=node->next;
    : : else break;
    : : if (node==NULL)
    : : printf("
    ");
    : : else
    : : {
    : : printf("Name:%s %s
    ",node->record.f_name,node->record.l_name);
    : : }
    : : [/code]
    : :
    : : u notice in that function i have [red]fname is referring to node->record.f_name. in other words fname is goin to be saved in node->record.f_name[/red]
    : [purple]
    : completely wrong! [blue]strcmp(const char*, const char*)[/blue] is a function that compares two string provided by the parameters. it returns 0 if they are same. fname is not going to be saved in [blue]node->record.f_name[/blue].
    : [/purple]
    : :
    : : therefore [code] if (strcmp(fname,node->record.f_name)||strcmp(lname,node->record.l_name)) [/code] is correct.
    : : wat this is sayinh if first name is and the last name does not link to wat is saved in the link list an error message should be displayed. else it print them out.
    : :
    : : [red]f_name is is a struct.[/red]
    : [purple]
    : r u talking about node->record.f_name? then u cant use the strcmp function in the above way. that shud be a compile time error.
    :
    : and whatever IDK said is correct also. comparing two integers using [blue]==[/blue] is really calls a comparison function.
    : [/purple]
    : [hr][purple]~Donotalo()[/purple]
    :
    :
    ok take a look on my question about the code i plca that same thing iam talking about here in blue so look at the code for me i made the changes to it so u can see it.
«1
Sign In or Register to comment.