Problems with int to char...

This is a discussion on Problems with int to char... within the C++ Programming forums, part of the General Programming Boards category; I'm using the following code to use an int variable (hard coded here but normally cinned by a user) as ...

  1. #1
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    Angry Problems with int to char...

    I'm using the following code to use an int variable (hard coded here but normally cinned by a user) as the beginning of a filename, this code should work, but i think i may be messing up my steps, i get the error: 'can not turn char* to int' when i try to compile, here's the code...anyone know whats wrong??
    Code:
    int studentID="04390"
    
    
    template <class T> std::string toString(const T& val) {
    	std::ostringstream strWriter;
    	strWriter << val;
    	return strWriter.str();
    }
    
    int main() {
    
    	std::string filename=toString<int>(studentID) + ".xml";
    	std::ofstream file (filename.c_str());
    	//etc.
    return 0;
    }
    thanks for your help
    PHP and XML
    Let's talk about SAX

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    int studentID="04390"

    Simple. You are trying to set StudentID (an int) to the string "04390" (a char *).

    You may want to do this instead:

    int StudentID = 4390; //(notice, no preceding zero's are included)

    or:

    char* StudentID = "04390";
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  2. Replies: 3
    Last Post: 05-13-2007, 09:55 AM
  3. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM

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