Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

c++(string/char) problem

  • 27-04-2004 09:04PM
    #1
    Registered Users, Registered Users 2 Posts: 3,177 ✭✭✭


    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, Registered Users 2 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