Thread: char + int = ?

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    21

    char + int = ?

    Ok lets think about that: You have a char x, and you want to abb a int y to the end. This is usefull, if you have many files, and instead of giving them names i want them just to have numbers. I am thinkink about something liek the strcat funktion or something like that.

    Thankx to all of you.

    -tare

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If I've understood what you want to do, here's two methods -

    Code:
    #include <iostream> 
    #include <cstdio> 
    #include <sstream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
    	char text[]="abcdefg";
    	int num =10;
    
    	//Option 1
    	char chars1[30];
    	ostringstream os;
    	os <<  text << num;
    	strcpy(chars1, os.str().c_str());
    	cout << chars1 << endl;
    
    	//Option 2
    	char chars2[30];
    	sprintf(chars2,"%s%d",text,num);
    	printf("%s\n",chars2);
    
    	return 0;
    }
    zen

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    21
    ok thanks man, both options are working but i have couple questions:

    ostringstream os; //What is that for?
    os << text << num; //What is that doing?
    strcpy(chars1, os.str().c_str()); //What is os.str().c_str() ????

    -lata tare

  4. #4
    Unregistered
    Guest
    My understanding is that ostringstreams are a mechanism to manipulate strings by writing directly to the string object. You create an ostringstream object just like you create a fstream or ifstream or ofstream oblject and use it to write to the string. In this case the << operator first writes the contents of the variable text to the ostringstream object os and then a second << operator writes the contents of the variable num to the string eventhough num is of type int rather than a string. This obviates the need to somehow convert an int to a string. However, once in the ostringstream object you need to get the information back out.

    os.str().c_str() converts the information in the ostringstream object os first to a form that can then be transfered to the routine c_style string chars1, so you can then print the value in chars1 (what was in os) to the screen with cout.

    Another way to convert from one type, say int, to a string is to use the C style function called sprintf() as opposed to using the C++ stream class ostringstream.

    If you happen to have the conio.h file it has functions called itoa(), ltoa(), and (I believe) ftoa() that can do this conversion for you as well. The problem is that these functions are not standard C++ functions and may not port to other compilers, and conio.h is not ANSI C++ standard and therefore not included in all compilers either. On the other hand sprintf() and ostringstreams are ANSI standard and should port to any compiler that is in compliance with ANSI standards.

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    os.str().c_str(), obtains a string object from the stringstream, os.str() will return a string object, this string object is then converted to constant C style string. This is shorthand for doing -

    string temp = os.str();
    strcpy(chars1,temp.c_str());

    For doing simple things like this sprintf() would probably be the best (but less type safe) option as it is more straightforward, and doesn't need the several several conversion steps.

    A futher method would be to use a strstream instead of a stringstream. A char* is obtained directly from a strstream, but I believe they've been depreciated in the C++ standard, so for future portability they're probably not a good option.

    Also, even if _itoa() is not supported by your compiler, the function is fairly simple so you could always implement your own if needed.
    zen

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    21
    Thanks unregistered stranger!!!
    now i have a new problem:

    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>

    i need also

    #include <istream.h>
    #include <ostream.h>
    #include <streamb.h>
    #include <strstrea.h>
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    #include <time.h>

    But than i get an error message...
    Why can't i use some files with .h and some without????
    What is the .h for????

    Thanks again.
    tare

  7. #7
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    In some cases the old style headers are incompatible with the new. In each case these are what you should be including (in the std namespace).

    #include <istream.h> - <fstream>
    #include <ostream.h> - <fstream>
    #include <streamb.h> - <streambuf>
    #include <strstrea.h> - <strstream>
    #include <iostream.h> -<iostream>
    #include <fstream.h> - <fstream>
    #include <stdlib.h> -<cstdlib>
    #include <string.h>- <cstring>
    #include <stdio.h>- <cstudio>
    #include <time.h> - <ctime>
    zen

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    21

    tare

    are the new style headers different than the old ones?

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    21
    Can anyone give me an example how to add an number to the end of a char with the itoa() function? I tried it but i cant figure it out...

    thankx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM