Thread: probs with coding

  1. #16
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    713
    Quote Originally Posted by Structure View Post
    Code:
    <snipped>
    Why not skip the buffer completely? It's not necessary:

    Code:
    #include <stdio.h>#include <stdbool.h>
    
    
    int main(int args, char *argv[]) {
    
    
            FILE *fp = fopen("text.txt", "r");
            bool writeChar = false;
            int ch;
    
    
            while ((ch = fgetc(fp)) != EOF) {
                    if (ch == ']') {
                            writeChar = false;
                            putchar('\n');
                    }
                    if (writeChar) {
                            putchar(ch);
                    }
                    if (ch == '[') writeChar = true;
            }
    
    
            fclose(fp);
            return 0;
    }
    (Also incorporating other suggestions in this thread, e.g., make ch an int and rename readChar to writeChar.)

    text.txt:
    Code:
    foo[This string makes Structure's code crash and burn]foo
    Code:
    $ ./178682
    This string makes Structure's code crash and burn

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Quote Originally Posted by christop
    Why not skip the buffer completely? It's not necessary
    It's necessary to handle cases like "[hello".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    241

    Cool

    There was a simple set of rules, and the code worked within that constraint. I did not feel the need to make it more complex as it was not necessary and should be left to the developer to expand the features.
    "without goto we would be wtf'd"

  4. #19
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Quote Originally Posted by Hodor View Post
    <snipped>f you want me to apologise for attacking you (perceived or otherwise) I'll do that. If you want me to say that your original code wasn't that bad after all then... well, you're out of luck. My reason for not replying to this thread was to hopefully defuse an ugly situation that I had caused, and since lots of people had responded I couldn't see the point of responding anyway

    I just want to be able to ask a question and be responded to in a civil way, without innuendos. A person posting a query is unable to solve the issue him or herself and is, therefore, emotionally aroused, causing what you say, evaluative statements in particular, to have a double impact. It might be good thing to bear that in mind.

  5. #20
    Registered User
    Join Date
    Dec 2017
    Posts
    864
    How do you think we feel when people who know nothing think they magically know better. We see it all the time. It's pathetic and frustrating. That might be a good thing for you to bear in mind.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Quote Originally Posted by Ivory348
    It might be a good idea, I thought, to re-post my original query.
    My query concerned the fact that the buffer wasn't transferring to the two-dimensional string.
    (...)
    My code by the way performs OK, except for my query.
    Your original code is indeed what has traditionally been called "spaghetti code": control criss crosses the code, and is compounded by poor formatting (many statements on a single physical line) and poor naming of the labels (one and two have no meaning relevant to the context of the code), and the use of magic numbers (which you fixed in your revised code). It tends to be harder to follow the flow of control of spaghetti code compared to well structured code, e.g., compare it to Structure's code in post #7 where the flow of control through the loop is clear.

    Also, as you may have found out from now, EOF is a constant, so !EOF is also a constant, i.e., zero.

    Quote Originally Posted by Ivory348
    I thought Structure's improvement of my code was brilliant. I could never have done what he did.
    It is good, but it isn't brilliant: as I mentioned, this is a common technique. Now that you've seen it, hopefully you can now do what he did (But refer to my comments on the oversights and poor style.)

    Quote Originally Posted by Ivory348
    int ch; or chr ch, makes no difference to the running of the code.
    That's probably because for your compiler, char is signed. On another compiler in which char is unsigned, it will make a difference.
    Last edited by laserlight; 01-30-2020 at 04:04 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Hi Laserlight, you responded to text that I removed again after less than a minute!! By the way, I did re-compile my old code with another compiler, and found only minor twitches. But ok, your messages are well received. I will mend my spaghetti-ways. The old fox will need to learn new tricks.
    Last edited by Ivory348; 01-30-2020 at 04:54 PM.

  8. #23
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    713
    Quote Originally Posted by laserlight View Post
    It's necessary to handle cases like "[hello".
    Haha, good point. In that case my code will print "hello". (It's not that clear how to "properly" handle cases like this, as the specifications I've seen in this thread are fairly loose.)

  9. #24
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    713
    Quote Originally Posted by Structure View Post
    I'm fairly certain that Hodor knows exactly what conio.h is and what ch == 93 is supposed to mean. I took their questions to be rhetorical to help the OP think about them to help them improve their code in terms of correctness and readability.

  10. #25
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Quote Originally Posted by john.c View Post
    How do you think we feel when people who know nothing think they magically know better. We see it all the time. It's pathetic and frustrating. That might be a good thing for you to bear in mind.
    As to coding, I will never, and have never, stated that I know better. My remark, to which you referred, was *psychological* in nature. What I will bear in mind though, in response to what you wrote, is that the culture on this forum has long toes.
    Last edited by Ivory348; 01-30-2020 at 05:11 PM.

  11. #26
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    [QUOTE=christop;1293456]Why not skip the buffer completely? It's not necessary:

    The objective of the code was to create an array to be subsequently sorted.
    So printing to the console was not the objective. Why assume a different objective?
    Why look for circumstances that would make Structure's code break down?
    The writer of the original code knows precisely what the parameters are,
    your break-up texts are never going to occur. So why inoculate code for things
    that are not going to happen? Notwithstanding the foregoing, your code
    is informative.

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Quote Originally Posted by Ivory348
    The objective of the code was to create an array to be subsequently sorted.
    So printing to the console was not the objective. Why assume a different objective?
    You did not state that. In post #6, you stated: "I wish to extract those [ .... ] strings and print them." Hence, this is your objective: printing to the console. You might have a different objective in mind, but if you don't state it or if you keep deleting the text in which you stated it, people simply won't know.

    The problem with christop's suggestion is that you stated "extract those [ .... ] strings", which presumably implies that a string like "[hello" should not have "hello" extracted and printed, but using christop's approach of printing characters immediately instead of building a string, "hello" will be printed because the lack of the "]" will not be detected until after "hello" has been printed.

    Quote Originally Posted by Ivory348
    Why look for circumstances that would make Structure's code break down?
    We should always look for circumstances that would make our code break down. The fact is, none of us are perfect, so it's unlikely that we will write perfect non-trivial code the first time round, so we should look for ways that our code is imperfect so as to improve it.

    Quote Originally Posted by Ivory348
    The writer of the original code knows precisely what the parameters are,
    your break-up texts are never going to occur. So why inoculate code for things
    that are not going to happen?
    That's the kind of thinking that led us to the now-obsolete gets function, which is inherently insecure because you have to trust the user to do exactly as the programmer expects. So, in order to write correct and secure programs, we should "inoculate" code for things that we might think aren't going to happen, but remains possible. A user entering string input too long for an array of char certainly is one common example.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Quote Originally Posted by Structure View Post
    Your code looks like a child wrote it. In fact it never works and its always crap. My code continues to supply results and that is what matters. Just stfu already and have some respect for people in general.
    Stop this nonsense. Now. If you didn't notice, I've reprimanded Hodor for making a personal attack Ivory348. Take this as your own reprimand.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Quote Originally Posted by Structure View Post
    There has been multiple posts of mine that were deleted for whatever reason they seem fit.
    It's simple: you're insulting people and making useless comments that will likely rile up people like john.c. john.c's posts have been deleted likewise.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,742
    Quote Originally Posted by Structure View Post
    Your code looks like a child wrote it. In fact it never works and its always crap. My code continues to supply results and that is what matters. Just stfu already and have some respect for people in general.
    What code?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std::map probs
    By BeBu in forum C++ Programming
    Replies: 6
    Last Post: 08-06-2007, 03:41 PM
  2. coding mastermind in C, only got 2 probs..
    By lepricaun in forum C Programming
    Replies: 17
    Last Post: 07-24-2004, 09:15 AM
  3. IDE & DX7+ Probs
    By MicroFiend in forum Game Programming
    Replies: 0
    Last Post: 03-04-2004, 10:54 AM
  4. Probs
    By Empress in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2003, 08:37 PM
  5. Several Probs
    By gamer4life687 in forum C++ Programming
    Replies: 4
    Last Post: 12-25-2002, 07:40 PM

Tags for this Thread