Thread: How do I do this? And can I do that? (about checking string content and file output)

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    20

    How do I do this? And can I do that? (about checking string content and file output)

    Okay, once again I am in need of some help. Nothing critical, but I'm trying to puzzle together small programs to make my life easier :P. Any help or comment would be very much appreciated .

    Okay, the first thing:

    I'm building up a code to repeat one long, relatively simple process:
    Ask for a multitude of information, store it and output it into a file.

    Now, I can write the numerous cout and cin lines myself, that's hardly the problem. But, at some point, I need to check the string that was entered with cin for a certain value (It needs to detect whether B is in the string, whether E is in the string and whether FB is in the string. If I know how to do that, I can easily tell it to add 1 to the variables B_Counter, E_Counter and FB_Counter or something, to be used later on.).

    I need to know, how can I do this efficiently?

    For example:
    cout would output the following to the screen: "Please enter the components:"
    via cin I would then enter: B, C, F, V, S
    That input should be stored as the string Components.

    Now, I just need to check this string for whether B is present or not (or really any string I would enter). How do I do that?

    If this answer is in a tutorial or something, and I just haven't read it carefully enough, it'd be fine to just direct me to the URL so I can read it .

    My second question would be:

    Is there a way to get my program (in C++, obviously) to make a .doc (or .rtf) file? And, by that, I mean, can I also put bold, italics etc. into that directly from the program?
    Or is that just a matter of knowing the code word/wordpad uses to create these styles (for example, <b>bold</b>) and implementing this into the code?

    for example: If I wanted my file to accept input with cin, and the input would be 'Monkeys', is there a way to automatically have this be transferred to a file as bold, or should I just have my program add <b> before it and </b> after it to produce Monkeys?
    Would <b> and </b> even work? (assuming that's the correct code for word...) or should I save it with <b> and </b> to a txt file and then convert it to Word?
    Or is it not possible at all?

    Thank you so much for any help or insight you might be able to offer. It helps me a lot .

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    1. There are a few ANSI ways of doing this. The simplest (and probably therefore fastest) would be to loop through the string via a pointer and check each char for the char you're looking for

    A C++-ey way of doing it would be to access the char *data() property of the string class.

    2. .RTF is the easiest format to understand of those two, but yeah, there's no language standard way of accessing specific file types. You'd be best off using a higher level library or learning the basics of how RTF is made up.
    we are one

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I need to know, how can I do this efficiently?
    Use getline() to read a whole line, then use the find() method to search for sub-strings which interest you.

    > Is there a way to get my program (in C++, obviously) to make a .doc (or .rtf) file?
    Well RTF is basically like HTML, the formatting is just readable text.
    So once you've read up on the RTF file format, outputting simple RTF documents should be easy.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    wiiire: How would I go about doing that? (loop via pointer, checking chars)

    higher level library?

    Thanks for your suggestions .

    Salem: Ah, thank you. I think I saw those wandering around the tutorials section somewhere...
    And thanks again for your input on the RTF thing.

    You've both been a great help. Thanks a bundle .

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    Grab a copy of the pointer to the underlying char from the string via the char *data() property of the string and increment it, checking each char and incrementing your various counts accordingly as you go, until you reach the end of the string.

    I'd imagine Salem's way would be better if you're only ever going to match one of these chars on any one line

    As far as .doc files go, yes, a library that wraps the task for you so you can just use a higher level implementation.

    I would use RTF (actually, I'd use HTML, hehe).
    Last edited by wiiire; 04-03-2007 at 06:23 AM.
    we are one

Popular pages Recent additions subscribe to a feed