Thread: Combining Data Types, Int + UChar?

  1. #1
    #junkie
    Join Date
    Oct 2004
    Posts
    240

    Combining Data Types, Int + UChar?

    Ok, basically this started out as just trying to combine a string with a number held in a variable, then i relized it goes beyond that.

    Here is a little typed up rough example of what i am trying to do.

    Code:
    int myInt = 0;
    unsigned char myChar;
    myChar = "filename"myInt".tga";
    Does that make any sense? I want a char string that equals "filename0.tga", this is because i have a opengl program and i am taking screenshots of it, and i need the filenames to be dif otherwise they just overwrite each other.

    So every SS taken i basically go myInt++;, and the filename of every pic needs to be "myscreenie"myInt".tga".
    Does that make any sense? If this was flash id simply do "filename"+myInt+".tga", but c++ seems not to like this. Can someone let me know how this can be done?

    I dont know how to combine strings like that, and in my mind, if i remember right, theres also a problem with writing an int into a char too.
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    #include <sstream>
    /* ... */
    std::ostringstream filename;
    filename<<"filename"<<myInt<<".tga"<<std::flush;
    Now filename.str() will give you a std::string
    filename.str().c_str() will give you a const char *

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I dont know how to combine strings like that, and in my mind, if i remember right, theres also a problem with writing an int into a char too.

    did you really expect to fit all that data into a single byte? maybe you should study the language a bit more before writing programs that are sure to break (if they even compile).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    uhh, dunno. From my understanding, and i am new, if you add a string of text "blah blah blah" into a char, it breaks it down into an array correct? and seeing as in theory "1" and "b" contain the same size of information, what is so wrong with that? Like i said i am new, so if my whole the ascii character "1" and "b" are essentially the same theory is wrong, or my char array is wrong, please correct me.

    But hey, atleast sarcastic useless comments like the one you just posted are useful somewhere.... i just cant think of anyplace.

    Anyway, thanks Thantos.
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Quote Originally Posted by Zeusbwr
    uhh, dunno. From my understanding, and i am new, if you add a string of text "blah blah blah" into a char, it breaks it down into an array correct? and seeing as in theory "1" and "b" contain the same size of information, what is so wrong with that? Like i said i am new, so if my whole the ascii character "1" and "b" are essentially the same theory is wrong, or my char array is wrong, please correct me.

    But hey, atleast sarcastic useless comments like the one you just posted are useful somewhere.... i just cant think of anyplace.

    Anyway, thanks Thantos.
    I don't really see how that was a useless comment. No, it will not become a char array. You cannot fit that amount of information in a char variable.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>if you add a string of text "blah blah blah" into a char
    Huh?
    >>it breaks it down into an array correct?
    Wha??

    I think you've got some ideas muddled up here. A string of text is an array of char, with the last element of your 'string' being the character '\0'.

    >>the ascii character "1" and "b" are essentially the same
    To be very anal, I'll nitpick this: What you really mean is, the characters '1' and 'b' have the same type size (note, single quotes) But the problem is, you're trying to somehow insert an integer into a string of characters - and an integer is a binary representation of a number, while a string that looks like a number is just.. a string that looks like a number.

    In C++ you can't just do, "abcd" + myNum + "efgh" and end up with whatever you wanted. What you CAN do, as Thantos has suggested, is use stringstreams to 'output' "abcd" to some stringstream (ostringstream in this case), then output the integer (the stringstream automatically converts it to a string), and then output the next string. The entire process is identical to std::cout, except that it isn't outputted to the screen and you can have as many stringstreams as you want. Then, to get the final result, you use (the stringstream).str() to retrieve the final string, which has been nicely converted and glued together in a std::string object. To then retrieve the C-string version of this string (the array of characters), you use (the std::string).c_str().

    >>theres also a problem with writing an int into a char too.
    Not sure what you mean by this. If you mean:
    Code:
    int foo = 5;
    char bar;
    bar = foo; //Potential overflow
     
    //or:
     
    int foo = 5;
    char bar[10];
    bar = foo; //Type mismatch, they're incompatible
    You'll have to clarify what you mean.

    But hey, atleast sarcastic useless comments like the one you just posted are useful somewhere.... i just cant think of anyplace.
    I'd advise against going in that direction. Sebastiani might have been overly harsh (and perhaps unprovoked), but answering in this way is rarely a good idea - especially since you've just done the exact same thing as he has

    @alpha: I beg to differ. I find the comment quite useless, in that it is short, vague, lacking explanation, made without clarification of the question, and somewhat rude, all at the same time. Your own post is little better.
    Last edited by Hunter2; 01-07-2005 at 05:53 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Actually Zeusbwr, it works like this: A single variable can hold one number. That number can represent an ASCII character. You need to declare a whole array of variables to hold a whole string.

    See the Array Tutorial and the String Tutorial.

    In C++, there are two types of strings and three string-headers.

    The tutorial uses C-style strings (also called character arrays, or null-terminated strings). These are simply an array of ASCII characters with a null (zero) at the end. Note that zero does not represent a printable ASCII character. A zero is represented by a decimal value of 41.... send 41 to your printer and it prints a zero!

    You do not need to include any headers to use C-style strings in your program. The two headers <cstring> and the "old" C-compatible <string.h> are essentially identical. They have handy functions for mainipulating C-style strings.

    You can also use C++ string objects which require the <string> header. The <string> header contains a set of functions for manipulating string objects. (These functions are completely different than the functions in <cstring>.) C++ string objects are generally easier to use (and safer) than C-style strings. At first glance, it might "look" like the whole C++ string is held in one variable, but they are more complex "under the hood".

    [EDIT]
    Hunter2 makes a good point about the use of double quotes...

    char x = 1; // x = 1 (duh!)
    char x = '1' ; // x = 49, which is the ASCII code for the character 1
    char x = "1" ; // ERROR! "1" is a null-terminated string!
    char x[] = "1" // A 2-character array: x[0] = 49, x[1] = 0, and x is automatically a pointer to array x[].
    Last edited by DougDbug; 01-07-2005 at 07:02 PM.

  8. #8
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    well thank you all for the replies, but in reply to Hunter and it also works for alhpa,

    I dunno, im a bit short tempered to people who answer in a manner like he did. And i always had a.. lets say lack of keeping my mouth shut. So in my inability to not express my opinion to people of his kind, i aparently do what i did . Overall i dont have regrets, if i sounded ungreatful, its because i am. Only to him ofcourse, everyone elses were helpful and commentful.

    The way i read his was basically "what you said was stupid so dont even try dipI am sillyI am sillyI am sillyI am sillyI am silly". Which may or may not be true lol, but none the less i learn through experimentation and not studying a book for 5 years so i can program mistake free at some expensive ass college i will never get into anyway.


    And what i said, understabably amazingly horribly explained, was not exactly what i ment with some things. But none the less it answered my questions, so i'll leave it be.

    Again, thanks hunter and the rest



    *edit*oh and btw, i think i was thinking of char[]... maybe
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Quote Originally Posted by Hunter2

    @alpha: I beg to differ. I find the comment quite useless, in that it is short, vague, lacking explanation, made without clarification of the question, and somewhat rude, all at the same time. Your own post is little better.
    my comment was short, yes. i'm sorry if it portrayed else (in no way did I mean to be rude), as it somewhat did explain what my thoughts on clarification of Sebastiani's post.

    Quote Originally Posted by Zeusbwr

    Which may or may not be true lol, but none the less i learn through experimentation and not studying a book for 5 years so i can program mistake free at some expensive ass college i will never get into anyway.
    good to learn from experimentation, as experimentation is needed to learn. one cannot learn purely by studying a book. doing the practical is also needed. but why think you won't get into college? given that statement, i'm assuming you're in high school? don't go with that attitude. if you want into college, get into college. you are your own limitation. work hard, get the grades, you'll get in. but you do need to believe that you can do it. i'm sure you can.

    oh, and with great financial aid from universities these days, not being able to afford it isn't really a viable excuse. there are scholarships out there also, all that is needed is to apply for them.

    either way, you're on your way to being a great programmer. apply the philosophy of programming into thinking about college as well. if you work on programming, you can work on getting into college also. i'm assuming, given your comment, that you do want to go to college.

    sorry for going on like this, but as you have to short temper to useless comments, i have things against people lacking a belief into being able to do things they are capable of (i.e. your comment about college).

    i know, i know, off topic to the original post. sorry.

  10. #10
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    my comment was short, yes. i'm sorry if it portrayed else (in no way did I mean to be rude), as it somewhat did explain what my thoughts on clarification of Sebastiani's post.
    Alpha, I believe he was referring to Sebastiani's post in his explanation of why he thought that post was rude.

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Quote Originally Posted by gcn_zelda
    Alpha, I believe he was referring to Sebastiani's post in his explanation of why he thought that post was rude.
    Yes, but he did state

    Quote Originally Posted by Hunter2
    Your own post is little better.
    and to that lies the reason for my apology.

  12. #12
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    Good point. I missed that.

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Sebastiani
    >> I dont know how to combine strings like that, and in my mind, if i remember right, theres also a problem with writing an int into a char too.

    did you really expect to fit all that data into a single byte? maybe you should study the language a bit more before writing programs that are sure to break (if they even compile).
    since I do not know the language well enough, if I had to write a program in COBOL right now, the simple fact is:

    1) it would probably not compile.
    2) if it did compile, it would probably crash.
    3) if I posted my code on the COBOL board I'd probably get some pretty snide remarks.

    so I read a book or two on COBOL and everybody's happy. problem solved.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    Quote Originally Posted by Sebastiani
    since I do not know the language well enough, if I had to write a program in COBOL right now, the simple fact is:

    1) it would probably not compile.
    2) if it did compile, it would probably crash.
    3) if I posted my code on the COBOL board I'd probably get some pretty snide remarks.

    so I read a book or two on COBOL and everybody's happy. problem solved.

    Ya.. i can see that. But hey thats where we differ. Myself, i have read, (well a book and a half) on c++, and overall am familiar with it except the finer details that it did not delve too deeply in. Small example is that of "DougDbug"'s post. In any matter, i learn the basic facts, what i can, and do what i can from books. However, you would be amazed, that some books do not explain every aspect perfectly clear to every person out there. So hey, whats a guy to do who is not in school nor has friends who know anything beyond football and pizza. I know, ask others who do and hope to god someone like you does not reply. In any matter, guess i didnt dodge that bullet this time.


    And to hunter's reply. I am going to college, but not going into what will sound like some sad sad pitty story oh woe is me bs, i didnt grad from high school, moms a druggie, dropped out when i was 17, friends were druggies, ect. Long story short, moved to WA, and trying to start over. And please dont take this in the wrong way, i am not trying to sound snappy at you, infact i like you, i just dont like pitty stories nor having to write about them.

    In any case, i'll get in, may not be the best, may not cost a ton, but whats the use of studying all this sh-t if i am not going to make some pratical use out of it eh? lol
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>And to hunter's reply
    Wasn't me. I think I've already done the sad sad pity story before, and I've no inclination to repeat it
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SCSI read disc structure C++ help
    By Witchfinder in forum C++ Programming
    Replies: 0
    Last Post: 03-25-2009, 04:24 PM
  2. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM