Confused about while loop condition to '\n'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jimhakans
    New Member
    • Sep 2007
    • 41

    Confused about while loop condition to '\n'

    hi,
    Actually i have seen this program in my book and cant get one thing, the while loop, Why we have used condition not equal to carriage return. One more thing this program is not incrementing the wordcount. it is always showing wordcount=1 no matter how many words the user input.
    Please help!
    [CODE=c]{
    int chcount, wordcount;
    char ch;
    chcount=0;
    wordcount=0;
    printf("\nEnter a phrase\n");
    while((ch=getch e()!='\r')
    {
    chcount=chcount +1;
    if(ch==' ')
    wordcount++;
    }
    printf("\nChara cter count=%d",chcou nt);
    printf("\nwordc ount=%d",wordco unt+1);
    }
    [/CODE]
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Okay, a few points.
    1) you don't ask a question. Do you need help in what this is doing? Do you want to modify it? Howso?
    2) Thread title. This thread doesn't really deal with "Output". It's more of a "Confused about while loop condition to '\n' ". Wouldn't you agree? Please try to use more descriptive titles in the future
    3) getche() is non-standard. This is bad. If you don't have to, don't use non-standard functions. Your code may not compile and may be implemented different ways on different systems. If you learn the proper way when you just start out, then you never have to worry about either learning something else later, or porting code that you didn't initially realize would need to be standard.

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      Originally posted by jimhakans
      hi,
      Actually i have seen this program in my book and cant get one thing, the while loop, Why we have used condition not equal to carriage return. One more thing this program is not incrementing the wordcount. it is always showing wordcount=1 no matter how many words the user input.
      Please help!
      [CODE=c]{
      int chcount, wordcount;
      char ch;
      chcount=0;
      wordcount=0;
      printf("\nEnter a phrase\n");
      while((ch=getch e()!='\r')
      {
      chcount=chcount +1;
      if(ch==' ')
      wordcount++;
      }
      printf("\nChara cter count=%d",chcou nt);
      printf("\nwordc ount=%d",wordco unt+1);
      }
      [/CODE]
      It looks like this function only counts words on a line. As soon as you hit the carriage return, you print and exit. I'm not entirely sure why wordcount doesn't work - what's it initialized as? it's not a local variable, right?

      Comment

      • jimhakans
        New Member
        • Sep 2007
        • 41

        #4
        Originally posted by sicarie
        Okay, a few points.
        1) you don't ask a question. Do you need help in what this is doing? Do you want to modify it? Howso?
        2) Thread title. This thread doesn't really deal with "Output". It's more of a "Confused about while loop condition to '\n' ". Wouldn't you agree? Please try to use more descriptive titles in the future
        3) getche() is non-standard. This is bad. If you don't have to, don't use non-standard functions. Your code may not compile and may be implemented different ways on different systems. If you learn the proper way when you just start out, then you never have to worry about either learning something else later, or porting code that you didn't initially realize would need to be standard.
        First of all i am a beginner C programmer. It is your job to tell me what is the proper method without criticizing me. My questing was quite clear that "Why in the while loop we are giving condition ch not equal to carriage return", Instead of giving me answer you are sying that my title is not correct. What matters if my tilte is not correct, the main thing is that you should clear my concept. According to me the output was wrong as wordcount was not incrementing and i also mention that i quite clear cut way.
        Please any body here give me answers without criticizing me.
        Thnaks

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Originally posted by jimhakans
          First of all i am a beginner C programmer. It is your job to tell me what is the proper method without criticizing me. My questing was quite clear that "Why in the while loop we are giving condition ch not equal to carriage return",
          You are right, you did ask a specific question, and I apologize for putting that in there. I would like to continue this off the board, I will PM you in a minute.

          Originally posted by jimhakans
          Instead of giving me answer you are sying that my title is not correct. What matters if my tilte is not correct, the main thing is that you should clear my concept. According to me the output was wrong as wordcount was not incrementing and i also mention that i quite clear cut way.
          Please any body here give me answers without criticizing me.
          Thnaks
          Sadly, any comment on your code is "criticism" . We strive for 'constructive criticism' here. I was not doing that, and I thank you for making sure I am attempting to be the most helpful while still abiding by the rules of the site. If you are going to post on a board open to the entire internet, you should be able to take a little bit of both sides of criticism, and judge which is valuable and has a point. However, my second post was getting to your issues with the code.

          Comment

          • jimhakans
            New Member
            • Sep 2007
            • 41

            #6
            Originally posted by sicarie
            You are right, you did ask a specific question, and I apologize for putting that in there. I would like to continue this off the board, I will PM you in a minute.



            Sadly, any comment on your code is "criticism" . We strive for 'constructive criticism' here. I was not doing that, and I thank you for making sure I am attempting to be the most helpful while still abiding by the rules of the site. If you are going to post on a board open to the entire internet, you should be able to take a little bit of both sides of criticism, and judge which is valuable and has a point. However, my second post was getting to your issues with the code.
            Actually i am a beginner, and i want to learn C, i want to be like you guys. I know very little of C and i think in this site everyone is criticizing me one way or an other. As a beginner i think my logic might be wrong and i might ask rather silly question according to you, But your job is to correct me right. And i think by criticizing someone in return ,it is a very bitter way to answer. One could get disharted.
            But if i have done something wrong on my part i am sorry for that, cause i didnt mean to.

            Comment

            • oler1s
              Recognized Expert Contributor
              • Aug 2007
              • 671

              #7
              I know very little of C and i think in this site everyone is criticizing me one way or an other
              Uhhh, yes. That would sort of be the idea of coming here, right? I mean, the only way you wouldn't be criticized is if you were perfect in your code and behavior. I don't think you're claiming that.

              As a beginner i think my logic might be wrong and i might ask rather silly question according to you,
              True. We'll let you know if it's silly and why it's silly. Don't feel bad about it.

              But your job is to correct me right.
              We have no obligations here. I have a busy day, everyday. Some of us are working, some earning our degrees, some raising a family. That we come here out of our time and offer our advice to you for free is not something you can demand. I'm extremely adapatable, and it's pretty hard to offend me. But I doubt the others are going to be so forgiving of such an attitude.


              And i think by criticizing someone in return ,it is a very bitter way to answer. One could get disharted.
              Oh, and how do you want us to answer?

              Look, fundamentally, you don't seem to understand how forums work. We are professionals with our own lives. We come here, and offer technical advice. It's free, so we aren't going to do something unnecessary and useless like spend time flattering your ego. If something is technically wrong, we will say so. We will do so in a direct manner. Think of it as the same as your doctor saying: "this is wrong about you". He isn't being mean. He isn't making fun of you. He is making a factual statement based on professional and technical judgment.

              The same is true for here. If you post a question on a technical forum, it will be analyzed as such. As a beginner, undoubtedly, you will make mistakes. Those mistakes will be pointed out. You can either realize that our criticism isn't mean spirited. Or you can pay for a tutor who will coddle you for long periods of time with kind words.

              Trust me when I say, you would really, really know, if we were flaming you.

              Comment

              Working...