Const Char to Char Error

This is a discussion on Const Char to Char Error within the C++ Programming forums, part of the General Programming Boards category; Here is an example code: Code: #include <iostream> #include <cstring> int main() { const char* a; a = "Some text"; ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    193

    Const Char to Char Error

    Here is an example code:

    Code:
    #include <iostream>
    #include <cstring>
    
    int main()
    {
    const char* a;
    a = "Some text";
    strcat(a, "some more text");
    return 0;
    }
    Returns Error: "invalid conversion from 'const char*' to 'char*'"

    Is there any way to add "some more text" to "Some text" without changing the variable type (const char)? Also, if I try:

    strcat(a, (const char*)"some more text");

    It returns the same error. Any help is greatly appreciated.
    Last edited by stickman; 08-28-2004 at 08:37 PM.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    I believe you need to typecast it to (char*) not (const char*)

    oh.....oops, I see what you did. You can't strcat to the end of a constant, because it's, well, a constant. If you want to strcat to the end, make it like so:

    Code:
    char buff[64]="blah ";
    
    strcat(buff,"more blah");

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Yeah, but I want to be able to do this:

    Code:
    #include <iostream>
    #include <cstring>
    
    char blah(const char* blahblah); //char* gives me an error, so I use const char* and I believe this creates the function
    
    char blah(const char* blahblah)
    {
    strcat(blahblah, " more blah");
    }
    
    int main()
    {
    blah("blah");
    return 0;
    }
    That should return "blah more blah", but it gives me the same error as before.

    EDIT: Nevermind about the "char*" giving me an error in this program, but in the other one I made which uses this, gives me an error, but I'll try again.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Now I'm confused. I made this test program and it compiles with no errors, but when I execte it, it won't execute. Any reason for that?

    Code:
    #include <iostream>
    #include <cstring>
    
    char blah(char* blahblah); //char* gives me an error, so I use const char* and I believe this creates the function
    
    char blah(char* blahblah)
    {
    strcat(blahblah, " more blah");
    std::cout << blahblah
    }
    
    int main()
    {
    blah("blah");
    std::cin.get(); //so it waits for user to hit enter
    return 0;
    }

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Why not do the easy thing and use the string class considering its standard
    Code:
    #include <iostream>
    #include <string>
    using std::string;
    
    void blah(string blahblah);
    
    int main()
    {
    blah("blah");
    std::cin.get(); //so it waits for user to hit enter
    return 0;
    }
    void blah(string blahblah)
    {
    blahblah=blahblah+"Blah party";
    std::cout << blahblah;
    }
    And btw your program was having issues cause your function wasn't returning a value
    Woop?

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    On a side note...

    Have a look at this

    Code:
    void Func(); //+---Function prototype---+
    
    void Func() 
    {
    
        //+---When you declare a function before the main         
        //function you dont need a prototype---+
    
    }
    
    int main()
    {
    
       //+---main function---+
    
    }
    Basically what I am saying

    Is that when you make a function before main you dont eed a prototype

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Quote Originally Posted by prog-bman
    Why not do the easy thing and use the string class considering its standard
    Code:
    #include <iostream>
    #include <string>
    using std::string;
    
    void blah(string blahblah);
    
    int main()
    {
    blah("blah");
    std::cin.get(); //so it waits for user to hit enter
    return 0;
    }
    void blah(string blahblah)
    {
    blahblah=blahblah+"Blah party";
    std::cout << blahblah;
    }
    And btw your program was having issues cause your function wasn't returning a value
    Thanks. Now how do I add int a to blah?

    int a=0;
    blahblah=blahblah+a;

    That would try to add a and blahblah.

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Quote Originally Posted by Vicious
    On a side note...

    Have a look at this

    Code:
    void Func(); //+---Function prototype---+
    
    void Func() 
    {
    
        //+---When you declare a function before the main         
        //function you dont need a prototype---+
    
    }
    
    int main()
    {
    
       //+---main function---+
    
    }
    Basically what I am saying

    Is that when you make a function before main you dont eed a prototype
    Oh ok. I guess I didn't understand that as I read a few tutorials and was wondering were they were getting "prototype". Thanks for the info.

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    I think I did something very bad:

    int a=0;
    blahblah=blahblah+(char)(int)a;

    Is that bad?

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    what are you trying to do if you want to cast it all you do is
    blah=blah + (char) a;
    but i don't see why you are needing to do that just make another string.
    string a = "whatever";
    blah=blah+a;
    Woop?

  11. #11
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    I'm trying to add an integer to a string and everytime I do, it tries to add 1 plus blah and returns an error.

  12. #12
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Using the std::string class is good, but it can't hurt to know about c-style strings either. Your problem was trying to append a const object--which you can't do. A possible solution is using a temporary object:

    Code:
    #include <iostream>
    
    char blah(const char* blahblah)
    {
    char* temp = new char[strlen(blahblah)+11]; //allocate space 
    //(add length of blahblah with length of " more blah"
    strcpy(temp,blahblah);//copy blahblah into temp object
    strcat(temp, " more blah"); 
    std::cout<<temp;
    }
    
    int main()
    {
    blah("blah");
    }
    Also, you can check the faq for ways to convert an integer to a string
    Last edited by JaWiB; 08-28-2004 at 10:25 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  13. #13
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    How do I add 0 to blah?:

    int a=0;
    blahblah=blahblah+a;

    I don't want to:

    blahblah=blahblah+"0";

    because the variable a changes in my program. Basically what I'm trying to say is I am randomly generating a number and I want to add the number to the string

    "The Number is:" a

    Is this possible?

  14. #14
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    well when you print it you could just do

    Code:
    std::cout << "The Number is: " << a;

  15. #15
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Yeah, but I want to save it to a string so I can add many of those strings to it like:

    The Number is: a
    The Number is: a

    So on and so forth. So the code would look like:

    char blah;
    int a=0;
    blah="The Number is:";
    blah=blah+a;

    That should output:
    The Number is:0, but doesn't because you can't add 0 to The Number is: or I just haven't figured out how.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  5. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM

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