strcmp

Discussion in 'C++' started by muser, Oct 3, 2003.

  1. muser

    muser Guest

    The following error appears: 'strcmp' : cannot convert parameter 1
    from 'char' to 'const char *'. I've already tried using single
    quotations. the header file
    only contains the struct contents. The whole program is part of an
    example found in my course work. Does strcmp only compare two sets of
    strings or can it be used to determine the end of the string as well?



    #include<iostream>
    #include<iomanip>
    #include<cstdlib>
    #include<cstring>
    #include "program2v.h"

    using namespace std;





    int main()
    {


    crecord *string1, *string2, t_str;

    *string1 = t_str;

    /// the error while( strcmp(*string1->customername, "\0"))
    {
    *string2 = *string1 + 1;
    while( strcmp(*string2->customername, '\0'))
    {
    if(strcmp( *string1->customername, *string2->customername) > 0 )
    {
    t_str = *string1;
    *string1 = *string2;
    *string2 = t_str;

    }
    string2++;
    }
    string1++;
    }


    return 0;
    }
    muser, Oct 3, 2003
    #1
    1. Advertising

  2. "muser" <> wrote in message
    news:...
    > The following error appears: 'strcmp' : cannot convert parameter 1
    > from 'char' to 'const char *'. I've already tried using single
    > quotations. the header file
    > only contains the struct contents. The whole program is part of an
    > example found in my course work. Does strcmp only compare two sets of
    > strings or can it be used to determine the end of the string as well?
    >
    >
    >
    > #include<iostream>
    > #include<iomanip>
    > #include<cstdlib>
    > #include<cstring>
    > #include "program2v.h"
    >
    > using namespace std;
    >
    >
    >
    >
    >
    > int main()
    > {
    >
    >
    > crecord *string1, *string2, t_str;
    >
    > *string1 = t_str;


    Uhm, 'string1' is a wild pointer, and you are just mighty lucky you did
    not blow up your program right here. Make sure 'string1' and 'string2' point
    to valid objects before working with them.

    >
    > /// the error while( strcmp(*string1->customername, "\0"))
    > {
    > *string2 = *string1 + 1;


    Same here. You are dereferencing pointer with an unknown value ...

    > while( strcmp(*string2->customername, '\0'))


    What are you trying to do? 'strcmp', as the name suggests, takes two
    strings (ie. char-arrays) and compares them. It looks like you want to
    compare plain chars? If so, do this:

    while (*string2->customername != '\0')

    > {
    > if(strcmp( *string1->customername, *string2->customername) > 0 )


    What is the type of crecord::customername? char* ?

    > {
    > t_str = *string1;
    > *string1 = *string2;
    > *string2 = t_str;
    >
    > }
    > string2++;
    > }
    > string1++;
    > }
    >
    >
    > return 0;
    > }


    No offense, but I suggest you get a good tutorial online or a book
    before coding more. From that snippet above, you seem to be confused by
    pointers and objects and string manipulation.

    hth
    --
    jb

    (replace y with x if you want to reply by e-mail)
    Jakob Bieling, Oct 3, 2003
    #2
    1. Advertising

  3. muser

    Kris Wempa Guest

    "muser" <> wrote in message
    news:...
    > /// the error while( strcmp(*string1->customername, "\0"))
    > {
    > *string2 = *string1 + 1;
    > while( strcmp(*string2->customername, '\0'))
    > {
    > if(strcmp( *string1->customername, *string2->customername) > 0 )
    > {


    Where is your structure defined ? Specifically, I need to know what data
    type the "customername" field is. Also, the '\0' argument is not a valid
    parameter type to strcmp(). What are you trying to accomplish ? If
    customername is a pointer to a character string and you want to test if the
    string is empty, you can just do something like:

    while (*(string2->customername) == '\0')
    Kris Wempa, Oct 3, 2003
    #3
  4. muser

    Default User Guest

    muser wrote:
    >
    > The following error appears: 'strcmp' : cannot convert parameter 1
    > from 'char' to 'const char *'. I've already tried using single
    > quotations. the header file
    > only contains the struct contents. The whole program is part of an
    > example found in my course work. Does strcmp only compare two sets of
    > strings or can it be used to determine the end of the string as well?
    >
    > #include<iostream>
    > #include<iomanip>
    > #include<cstdlib>
    > #include<cstring>
    > #include "program2v.h"
    >
    > using namespace std;
    >
    > int main()
    > {
    >
    >
    > crecord *string1, *string2, t_str;


    What is crecord?

    > *string1 = t_str;


    You deferenced a pointer without having set it to any value. Where do
    you think string1 points?

    >
    > /// the error while( strcmp(*string1->customername, "\0"))


    What is customername? What type? Are you sure that dereference does what
    you think it does?


    In the future, post *complete* minimal programs. We don't have your
    header file.

    Also, explain what the basic concept of the program is. Your code had no
    comments at all.




    Brian Rodenborn
    Default User, Oct 3, 2003
    #4
  5. muser

    muser Guest

    Default User <> wrote in message news:<>...
    > muser wrote:
    > >
    > > The following error appears: 'strcmp' : cannot convert parameter 1
    > > from 'char' to 'const char *'. I've already tried using single
    > > quotations. the header file
    > > only contains the struct contents. The whole program is part of an
    > > example found in my course work. Does strcmp only compare two sets of
    > > strings or can it be used to determine the end of the string as well?
    > >
    > > #include<iostream>
    > > #include<iomanip>
    > > #include<cstdlib>
    > > #include<cstring>
    > > #include "program2v.h"
    > >
    > > using namespace std;
    > >
    > > int main()
    > > {
    > >
    > >
    > > crecord *string1, *string2, t_str;

    >
    > What is crecord?
    >
    > > *string1 = t_str;

    >
    > You deferenced a pointer without having set it to any value. Where do
    > you think string1 points?
    >
    > >
    > > /// the error while( strcmp(*string1->customername, "\0"))

    >
    > What is customername? What type? Are you sure that dereference does what
    > you think it does?
    >
    >
    > In the future, post *complete* minimal programs. We don't have your
    > header file.
    >
    > Also, explain what the basic concept of the program is. Your code had no
    > comments at all.
    >
    >
    >
    >
    > Brian Rodenborn



    my code again in a simpler reworked format. strcmp produces the
    following error:
    C:\Program Files\Microsoft Visual
    Studio\MyProjects\Valid\program2\program2v.cpp(52) : error C2664:
    'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'

    if string1 and string 2 have to be const chars, how can i dereference
    them later in the function?


    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <cstring>
    #include <cstdlib>


    using namespace std;

    struct crecord {
    long customercode[5];
    char customername[21];
    char customeraddress[61];
    double customerbalance;
    double creditlimit;
    };
    crecord Newcrecord;

    struct irrecord {
    long customercode[5];
    long partnum[5];
    long issue_rec;

    };
    irrecord Newirrecord;

    struct drecord {
    long customercode[5];
    };
    drecord Newdrecord;

    void sort( char* record )
    {
    char t_str;
    char characterstring[5];
    char* string1;
    char* string2;


    strncpy(characterstring, &record[2], 5);
    characterstring[5] = '\0';



    while(*(string1) != '\0')
    {
    *string2 = *string1;
    *string2++;

    while( *(string2) != '\0')
    {
    if(strcmp( *string1, *string2 ) > 0)
    {
    t_str = *string1;
    *string1 = *string2;
    *string2 = t_str;

    }
    string2++;
    }
    string1++;
    }
    return;
    }














    int main()
    {
    const char outfile[] = "A:\\514650VD.DAT";
    ofstream validdata;

    char temp2[256];

    return 0;

    }
    muser, Oct 6, 2003
    #5
  6. muser

    Default User Guest

    muser wrote:
    > my code again in a simpler reworked format. strcmp produces the
    > following error:
    > C:\Program Files\Microsoft Visual
    > Studio\MyProjects\Valid\program2\program2v.cpp(52) : error C2664:
    > 'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
    >
    > if string1 and string 2 have to be const chars, how can i dereference
    > them later in the function?


    The have to be POINTERS to const char. You are passing in a single char.

    > char* string1;
    > char* string2;


    > if(strcmp( *string1, *string2 ) > 0)



    You dereference the two char pointers, which yields a char, which
    generates a diagnostic. Hurray, the system works! Go with this:

    if(strcmp( string1, string2 ) > 0)



    Brian Rodenborn
    Default User, Oct 6, 2003
    #6
  7. (muser) writes:

    > The following error appears: 'strcmp' : cannot convert parameter 1
    > from 'char' to 'const char *'. I've already tried using single
    > quotations. the header file
    > only contains the struct contents.


    ... which would be necessary to understand what's going on here.
    Please post the minimal *complete*, *compilable* example showing your
    problem.

    > The whole program is part of an
    > example found in my course work. Does strcmp only compare two sets of
    > strings or can it be used to determine the end of the string as well?
    >
    >
    >
    > #include<iostream>
    > #include<iomanip>
    > #include<cstdlib>
    > #include<cstring>
    > #include "program2v.h"
    >
    > using namespace std;
    >
    >
    >
    >
    >
    > int main()
    > {
    >
    >
    > crecord *string1, *string2, t_str;
    >
    > *string1 = t_str;


    Here you're assigning to a pointer for which no memory has been allocated.

    > /// the error while( strcmp(*string1->customername, "\0"))


    It seems that crecord::customername is a char - strcmp is for comparing
    char*. If you really want to compare two chars (which I doubt),
    just use operator==, e.g.

    if (*string1->customername == '\0')

    Note the single quote.
    I'm also wondering what you want to achieve with dereferencing
    string1->customername - but without the header file, it's impossible to guess.

    HTH & kind regards
    frank

    --
    Frank Schmitt
    4SC AG phone: +49 89 700763-0
    e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
    Frank Schmitt, Oct 9, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shane Peck

    strcmp problem

    Shane Peck, Sep 19, 2003, in forum: C++
    Replies:
    6
    Views:
    2,351
    Kevin Goodsell
    Sep 22, 2003
  2. Andrej Hocevar

    please help with strcmp()

    Andrej Hocevar, Jul 19, 2003, in forum: C Programming
    Replies:
    3
    Views:
    325
    Gordon Burditt
    Jul 19, 2003
  3. Allan Bruce

    strcmp but with '\n' as the terrminator

    Allan Bruce, Jul 19, 2003, in forum: C Programming
    Replies:
    53
    Views:
    1,385
    Arthur J. O'Dwyer
    Jul 30, 2003
  4. Robert Mens

    segfault when strcmp

    Robert Mens, Oct 22, 2003, in forum: C Programming
    Replies:
    6
    Views:
    574
    Peter Nilsson
    Oct 23, 2003
  5. Eirik

    strcmp() And if

    Eirik, Dec 13, 2003, in forum: C Programming
    Replies:
    11
    Views:
    755
    Lew Pitcher
    Dec 17, 2003
Loading...

Share This Page