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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

c++(string/char) problem

  • 27-04-2004 8:04pm
    #1
    Registered Users, Registered Users 2 Posts: 3,153 ✭✭✭


    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