problem with strcat

This is a discussion on problem with strcat within the C++ Programming forums, part of the General Programming Boards category; Hello im kinda new with C++ and im trying to have a program that reads in a keyboard press while ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    7

    problem with strcat

    Hello im kinda new with C++ and im trying to have a program that reads in a keyboard press while the programs continues to run. i seem to have that part of it ok but im trying to create a string with each new key pressed but i get a error that i cant seem to get around. Any help would be good
    Code:
    #include <iostream>
    #include <conio.h>
    #include <ctype.h>    //please do not worrry about all these headers 
    #include <cstring>	  // i have been trying different commands
    #include <stdio.h>
    
    using namespace std;
    int main(void)
    {
    	char string[256];
    	int kb;
    	while(kb!= 27)
    	{
    	if (_kbhit())
    		{
    		kb =_getch();
    		_putch(kb);
    		if (kb != 13)
    		{
    			string[0] = '\0';
    			char c = char(kb);
    			strcat(string, c);  // problem is here. i get a error which is 
    								//" cannot convert parameter 2 from 'char' to 'const char *'"
    		}
    		}
    	}
    }
    Last edited by morngrym; 02-19-2010 at 12:06 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You use strcat like this:

    strcat(string1, string2)

    you are trying to code

    strcat(string1, char1)
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I know you made a comment about the headers, but I will say something anyways. This is the C++ forum, so I imagine you're intending to write a "C++" program, but your headers are both C++ and C. Also, if you're using C++, working with the "string" type is generally easier than working with "char*" or "char[]".

    As stated, "strcat" requires a "char*". When concatenating strings its good to make sure not to go out of bounds. The function "strncat" is used for this, and
    Code:
    strncat(strDest, strSrc, 5);
    will append up to 5 characters from strDest to the end of strSrc. In your case you only want to append one character, so you'd pass 1. But you still have a "char" and you need it to be a "char*", so you could pass "&c" to get its address. Alternatively, you could just create a "char*" and assign it the address of the char. Using C++ "string"s, it'd be as easy as "myString += myChar;"

    Next, to do more than one thing at the "same" time, you'd need to make your program multi-threaded. One thread to read the input, and the other thread to do your other task. This concept requires reading a tutorial, and cant be easily explained in a single post here.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    7
    ok i have strcat with 2 strings but i cant convert the keypress into a string, i changed
    Code:
    .
    char c = char(kb);
    to
    Code:
    char c[1] = char(kb);
    and that one gives an error now.
    i guess what im asking is can it be changed into the proper format or do i have to change how i get my keypress and if so what command should i use.

    thanks

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Instead of

    char c

    try

    char c[2]

    and init c[1] to '\0'.

    Then, everywhere you are using c, change it to use c[0]. You can then strcat(string,c).
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    7

    thanks

    Thanks i seem to have it working how i want now, the strcat(string, &c) seems to work well

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    I think that strcat is the wrong approach here: you are trying to append a character, not concatenate a string, and besides, when used in a loop like this it becomes slow. Rather, keep track of the current character (e.g., by storing the current string index), and assign to it accordingly. This also makes it easier for you to check that you are within the bounds of the array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    6
    Quote Originally Posted by morngrym View Post
    Thanks i seem to have it working how i want now, the strcat(string, &c) seems to work well
    That will not work since strcat required a NULL terminated string. The address of c does not contain the NULL terminated string. Most likely, your code will crash.

    Code:
    #include <iostream>
    #include <conio.h>
    #include <ctype.h>    //please do not worrry about all these headers 
    #include <cstring>	  // i have been trying different commands
    #include <stdio.h>
    
    using namespace std;
    int main(void)
    {
        char *p, *endp;
    	char string[256];
    	char kb;
    
    	memset(string, 0, sizeof(string));
    	
    	p = string;
    	pend = p + (sizeof(string) - 1);
    	
    	kb = 0;
    	while(kb!= 27 && p != endp) {
    		if (_kbhit()) {
    			kb =_getch();
    			_putch(kb);
    			if (kb != 13) {
    				*p++ = kb;
    			}
    		}
    	}
    }
    Last edited by NightCode; 02-19-2010 at 04:09 PM.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    morngrym, what NightCode said about using "strcat" with a char is correct. However, if you notice in my post above, you'll see I suggested "strncat", so that you can specify (at most) how many characters are copied. If you're using the function with a char, then you specify to copy one character.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    2,432
    since this is the C++ forum, and not the C forum, why not just use std::string? is this a class assignment where you are not allowed to use std::string?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by nadroj
    However, if you notice in my post above, you'll see I suggested "strncat", so that you can specify (at most) how many characters are copied. If you're using the function with a char, then you specify to copy one character.
    Yeah, but frankly that sounds like a hack, is still needlessly inefficient, and does not solve the existing buffer overflow problem (i.e., the loop still needs to check that the number of characters entered does not exceed the maximum length of the string).

    I really do not see why there is a fixation with strcat and strncat when the aim is to append character by character.

    Quote Originally Posted by Elkvis
    since this is the C++ forum, and not the C forum, why not just use std::string?
    Yes, this will avoid the buffer overflow problem, as well as the need to append the null character after you are done with my suggestion (if you have not initialised the string buffer with null characters).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I agree that its a sneaky workaround, and it comes with potential caveats. I also agree, as I stated earlier, that he/she should be probably be using C++ "string"s instead of managing "char[]"s manually. A key principle of programming is reuse, and in OOP that is object reuse: use "string"s because they were written so you don't have to!

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,313
    Using strncat() with an allowed length of 1 to eliminate the need for a trailing zero byte is an exceedingly ugly hack. While it will technically work, such things are also an open invitation for some future programmer - including yourself - to misunderstand what's going on, and make dangerous changes.


    On other things in the code provided in the OP, this
    Code:
            int kb;
    	while(kb!= 27)
    formally gives undefined behaviour (accessing the value of an uninitialised int). In practice, unless you are unlucky enough that an uninitialised int gets the value 27, this will work. But it is better to explicitly initialise kb to be some value other than 27. You're less likely to get in trouble if you make simple changes to eliminate a potential problem rather than assuming you will always get lucky.

    Also, be aware that <conio.h>, _kbhit(), _getch(), and _putch() are generally specific to a particular compiler - and not guaranteed to exist with other compilers.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STRCAT problem
    By Frusciante in forum C Programming
    Replies: 9
    Last Post: 07-04-2007, 06:15 AM
  2. Problem with strcat
    By firyace in forum C Programming
    Replies: 9
    Last Post: 05-15-2007, 02:31 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Problem with strcat() function
    By sureshkumarct in forum C Programming
    Replies: 6
    Last Post: 01-03-2007, 07:05 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21