Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

c++(string/char) problem

Options
  • 27-04-2004 9:04pm
    #1
    Registered Users Posts: 3,135 ✭✭✭


    ive got a problem, i'm doing this comptuer programming project. It's mean't to output your lucky lotto numbers and then ask you to input the winning lotto numbers and then tell you how many were matched and what the mathcing number were, i was using a string command to tell me what numbers were matched but my school compilers is old and does not recognise the string command so i have to use char, i cant remember using char any advice ?

    #include <string>
    char buf[12];
    string rightNumbers;
    char buf[12]; sprintf(buf, "\n%d", mynumbersresults[j]); rightNumbers +=buf;

    thats all the code i used for the string command


Comments

  • Registered Users Posts: 491 ✭✭Silent Bob


    You will want to use an array of characters in place of the string.

    There are a number of C functions for performing manipulations on strings as char*s, the most useful are:

    size_t strlen(const char* s); tells you the length of a string, without counting the null terminator at the end.

    char* strcpy(char* dest, const char* src); copies the string pointed to by src to the memory pointed to by dest

    int strcmp(const char* s1, const char* s2); compares the strings pointed to by s1 and s2 lexicographically. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.

    char* strcat(char* dest, const char* src); concatenates string src to string dest, returns a pointer to the new string dest. This function overwrites the null terminator at the end of dest and adds a new null terminator after concatenating src. There must be enough space in dest.

    char* strncat(char* dest, const char* src, size_t n);
    int strncmp(const char* s1, const char* s2, size_t n);
    char* strncpy(char* dest, const char* src, size_t n);

    These are all like the equivalent versions above but they take a parameter indicating how many characters are to be involved in the operation. Null terminators won't be copied if they aren't within the range given.

    strcat() will perform the same operation as += does on a string object, but it won't guarantee correct results unless you ensure that there is enough room in the destination array.


Advertisement