Input buffer to get a an input, C programming












0














I'm in my first steps in C programming, and I came across a task I can not find a solution to.

The task is simple: taking a character from the user.

After that, you receiving a string from the user until the user types the * character.

Then print the number of times the user typed the first character.



I was able to solve the problem using char [SIZE]ת when I placed a maximum input size of 255 bytes (#define SIZE 255).

Nevertheless, my teacher tells me that although the solution is working well, this was not the purpose of the exercise, also, I can not assume a maximum string size.



He asks us to use the input buffer.

No dynamic memory allocation is used in the exercise, and only the stdio.h library is allowed.



I read a lot about the input buffer, but I still have not found the possibility to solve the exercise - how can I absorb value from the user without knowing its size?



I would be happy to receive assistance and tips on how to use the input buffer correctly.

Or more focused, how to input values (string of characters) into the input buffer, and then go over each character separately in this string and process it.



Thank You










share|improve this question





























    0














    I'm in my first steps in C programming, and I came across a task I can not find a solution to.

    The task is simple: taking a character from the user.

    After that, you receiving a string from the user until the user types the * character.

    Then print the number of times the user typed the first character.



    I was able to solve the problem using char [SIZE]ת when I placed a maximum input size of 255 bytes (#define SIZE 255).

    Nevertheless, my teacher tells me that although the solution is working well, this was not the purpose of the exercise, also, I can not assume a maximum string size.



    He asks us to use the input buffer.

    No dynamic memory allocation is used in the exercise, and only the stdio.h library is allowed.



    I read a lot about the input buffer, but I still have not found the possibility to solve the exercise - how can I absorb value from the user without knowing its size?



    I would be happy to receive assistance and tips on how to use the input buffer correctly.

    Or more focused, how to input values (string of characters) into the input buffer, and then go over each character separately in this string and process it.



    Thank You










    share|improve this question



























      0












      0








      0







      I'm in my first steps in C programming, and I came across a task I can not find a solution to.

      The task is simple: taking a character from the user.

      After that, you receiving a string from the user until the user types the * character.

      Then print the number of times the user typed the first character.



      I was able to solve the problem using char [SIZE]ת when I placed a maximum input size of 255 bytes (#define SIZE 255).

      Nevertheless, my teacher tells me that although the solution is working well, this was not the purpose of the exercise, also, I can not assume a maximum string size.



      He asks us to use the input buffer.

      No dynamic memory allocation is used in the exercise, and only the stdio.h library is allowed.



      I read a lot about the input buffer, but I still have not found the possibility to solve the exercise - how can I absorb value from the user without knowing its size?



      I would be happy to receive assistance and tips on how to use the input buffer correctly.

      Or more focused, how to input values (string of characters) into the input buffer, and then go over each character separately in this string and process it.



      Thank You










      share|improve this question















      I'm in my first steps in C programming, and I came across a task I can not find a solution to.

      The task is simple: taking a character from the user.

      After that, you receiving a string from the user until the user types the * character.

      Then print the number of times the user typed the first character.



      I was able to solve the problem using char [SIZE]ת when I placed a maximum input size of 255 bytes (#define SIZE 255).

      Nevertheless, my teacher tells me that although the solution is working well, this was not the purpose of the exercise, also, I can not assume a maximum string size.



      He asks us to use the input buffer.

      No dynamic memory allocation is used in the exercise, and only the stdio.h library is allowed.



      I read a lot about the input buffer, but I still have not found the possibility to solve the exercise - how can I absorb value from the user without knowing its size?



      I would be happy to receive assistance and tips on how to use the input buffer correctly.

      Or more focused, how to input values (string of characters) into the input buffer, and then go over each character separately in this string and process it.



      Thank You







      c input buffer






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 '18 at 15:28









      iElden

      640317




      640317










      asked Nov 18 '18 at 13:32









      JavaStarterJavaStarter

      61




      61
























          1 Answer
          1






          active

          oldest

          votes


















          1














          There is no need to store all characters. Once you have read a character you can throw it away and just increase a counter. Something like this:



          #include <stdio.h>

          int main() {
          char c, first;
          int counter=0;
          printf("Enter first character: ");
          scanf("%c", &first);
          do {
          scanf("%c", &c);
          if(c == first)
          counter++;
          } while (c != '*');
          printf("You entered '%c' %d timesn", first, counter);
          }


          Output:



          Enter first character: a
          aaaaa*
          5


          or



          Enter first character: a
          aabbaa*
          You entered 'a' 4 times


          Note:



          As been pointed out in the comments, scanf is not a good tool for this kind of stuff. I would advice against the usage of it, unless you know it is the right tool. But that's beside the point. The point here was to show you that you don't need to store the whole input buffer. If you want to look at alternate input methods (as William Pursell suggested in the comments) you could have a look at fgetc, getc, or getchar for reading single characters. fread is also a tool you should get familiar with.






          share|improve this answer























          • but if I type the next string: asjue7tasd, without "enter" or "space" between each character, will the buffer will hold it all? and we'll be able to process each one of the letters separately? We'll see counter = 2? (In case 'a' was chosen from the first place)
            – JavaStarter
            Nov 18 '18 at 14:10












          • @JavaStarter There's no buffer, read the answer again.
            – Purple Ice
            Nov 18 '18 at 15:16










          • @JavaStarter Don't ask what will happen with a certain input. Compile it and try for yourself. If it does not work, well THEN you have a good follow up question.
            – Broman
            Nov 18 '18 at 15:43










          • @Broman you're right, I was away from the keyboard and could not do the test myself. In any case, thanks for the explanation, working properly. But I want to understand what is happening "behind the scenes" - how can the variable c defined as char, contains more than one value? Or is not that the case?
            – JavaStarter
            Nov 18 '18 at 16:43












          • You're just reading a single character. There is absolutely no need to use scanf for this, and doing so is a terrible thing to show a beginner. Beginners should have it constantly driven into their brain to avoid scanf like the plague. It is absolutely the wrong way to read a single character.
            – William Pursell
            Nov 18 '18 at 23:39











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361420%2finput-buffer-to-get-a-an-input-c-programming%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          There is no need to store all characters. Once you have read a character you can throw it away and just increase a counter. Something like this:



          #include <stdio.h>

          int main() {
          char c, first;
          int counter=0;
          printf("Enter first character: ");
          scanf("%c", &first);
          do {
          scanf("%c", &c);
          if(c == first)
          counter++;
          } while (c != '*');
          printf("You entered '%c' %d timesn", first, counter);
          }


          Output:



          Enter first character: a
          aaaaa*
          5


          or



          Enter first character: a
          aabbaa*
          You entered 'a' 4 times


          Note:



          As been pointed out in the comments, scanf is not a good tool for this kind of stuff. I would advice against the usage of it, unless you know it is the right tool. But that's beside the point. The point here was to show you that you don't need to store the whole input buffer. If you want to look at alternate input methods (as William Pursell suggested in the comments) you could have a look at fgetc, getc, or getchar for reading single characters. fread is also a tool you should get familiar with.






          share|improve this answer























          • but if I type the next string: asjue7tasd, without "enter" or "space" between each character, will the buffer will hold it all? and we'll be able to process each one of the letters separately? We'll see counter = 2? (In case 'a' was chosen from the first place)
            – JavaStarter
            Nov 18 '18 at 14:10












          • @JavaStarter There's no buffer, read the answer again.
            – Purple Ice
            Nov 18 '18 at 15:16










          • @JavaStarter Don't ask what will happen with a certain input. Compile it and try for yourself. If it does not work, well THEN you have a good follow up question.
            – Broman
            Nov 18 '18 at 15:43










          • @Broman you're right, I was away from the keyboard and could not do the test myself. In any case, thanks for the explanation, working properly. But I want to understand what is happening "behind the scenes" - how can the variable c defined as char, contains more than one value? Or is not that the case?
            – JavaStarter
            Nov 18 '18 at 16:43












          • You're just reading a single character. There is absolutely no need to use scanf for this, and doing so is a terrible thing to show a beginner. Beginners should have it constantly driven into their brain to avoid scanf like the plague. It is absolutely the wrong way to read a single character.
            – William Pursell
            Nov 18 '18 at 23:39
















          1














          There is no need to store all characters. Once you have read a character you can throw it away and just increase a counter. Something like this:



          #include <stdio.h>

          int main() {
          char c, first;
          int counter=0;
          printf("Enter first character: ");
          scanf("%c", &first);
          do {
          scanf("%c", &c);
          if(c == first)
          counter++;
          } while (c != '*');
          printf("You entered '%c' %d timesn", first, counter);
          }


          Output:



          Enter first character: a
          aaaaa*
          5


          or



          Enter first character: a
          aabbaa*
          You entered 'a' 4 times


          Note:



          As been pointed out in the comments, scanf is not a good tool for this kind of stuff. I would advice against the usage of it, unless you know it is the right tool. But that's beside the point. The point here was to show you that you don't need to store the whole input buffer. If you want to look at alternate input methods (as William Pursell suggested in the comments) you could have a look at fgetc, getc, or getchar for reading single characters. fread is also a tool you should get familiar with.






          share|improve this answer























          • but if I type the next string: asjue7tasd, without "enter" or "space" between each character, will the buffer will hold it all? and we'll be able to process each one of the letters separately? We'll see counter = 2? (In case 'a' was chosen from the first place)
            – JavaStarter
            Nov 18 '18 at 14:10












          • @JavaStarter There's no buffer, read the answer again.
            – Purple Ice
            Nov 18 '18 at 15:16










          • @JavaStarter Don't ask what will happen with a certain input. Compile it and try for yourself. If it does not work, well THEN you have a good follow up question.
            – Broman
            Nov 18 '18 at 15:43










          • @Broman you're right, I was away from the keyboard and could not do the test myself. In any case, thanks for the explanation, working properly. But I want to understand what is happening "behind the scenes" - how can the variable c defined as char, contains more than one value? Or is not that the case?
            – JavaStarter
            Nov 18 '18 at 16:43












          • You're just reading a single character. There is absolutely no need to use scanf for this, and doing so is a terrible thing to show a beginner. Beginners should have it constantly driven into their brain to avoid scanf like the plague. It is absolutely the wrong way to read a single character.
            – William Pursell
            Nov 18 '18 at 23:39














          1












          1








          1






          There is no need to store all characters. Once you have read a character you can throw it away and just increase a counter. Something like this:



          #include <stdio.h>

          int main() {
          char c, first;
          int counter=0;
          printf("Enter first character: ");
          scanf("%c", &first);
          do {
          scanf("%c", &c);
          if(c == first)
          counter++;
          } while (c != '*');
          printf("You entered '%c' %d timesn", first, counter);
          }


          Output:



          Enter first character: a
          aaaaa*
          5


          or



          Enter first character: a
          aabbaa*
          You entered 'a' 4 times


          Note:



          As been pointed out in the comments, scanf is not a good tool for this kind of stuff. I would advice against the usage of it, unless you know it is the right tool. But that's beside the point. The point here was to show you that you don't need to store the whole input buffer. If you want to look at alternate input methods (as William Pursell suggested in the comments) you could have a look at fgetc, getc, or getchar for reading single characters. fread is also a tool you should get familiar with.






          share|improve this answer














          There is no need to store all characters. Once you have read a character you can throw it away and just increase a counter. Something like this:



          #include <stdio.h>

          int main() {
          char c, first;
          int counter=0;
          printf("Enter first character: ");
          scanf("%c", &first);
          do {
          scanf("%c", &c);
          if(c == first)
          counter++;
          } while (c != '*');
          printf("You entered '%c' %d timesn", first, counter);
          }


          Output:



          Enter first character: a
          aaaaa*
          5


          or



          Enter first character: a
          aabbaa*
          You entered 'a' 4 times


          Note:



          As been pointed out in the comments, scanf is not a good tool for this kind of stuff. I would advice against the usage of it, unless you know it is the right tool. But that's beside the point. The point here was to show you that you don't need to store the whole input buffer. If you want to look at alternate input methods (as William Pursell suggested in the comments) you could have a look at fgetc, getc, or getchar for reading single characters. fread is also a tool you should get familiar with.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 '18 at 15:16

























          answered Nov 18 '18 at 13:45









          BromanBroman

          6,256112241




          6,256112241












          • but if I type the next string: asjue7tasd, without "enter" or "space" between each character, will the buffer will hold it all? and we'll be able to process each one of the letters separately? We'll see counter = 2? (In case 'a' was chosen from the first place)
            – JavaStarter
            Nov 18 '18 at 14:10












          • @JavaStarter There's no buffer, read the answer again.
            – Purple Ice
            Nov 18 '18 at 15:16










          • @JavaStarter Don't ask what will happen with a certain input. Compile it and try for yourself. If it does not work, well THEN you have a good follow up question.
            – Broman
            Nov 18 '18 at 15:43










          • @Broman you're right, I was away from the keyboard and could not do the test myself. In any case, thanks for the explanation, working properly. But I want to understand what is happening "behind the scenes" - how can the variable c defined as char, contains more than one value? Or is not that the case?
            – JavaStarter
            Nov 18 '18 at 16:43












          • You're just reading a single character. There is absolutely no need to use scanf for this, and doing so is a terrible thing to show a beginner. Beginners should have it constantly driven into their brain to avoid scanf like the plague. It is absolutely the wrong way to read a single character.
            – William Pursell
            Nov 18 '18 at 23:39


















          • but if I type the next string: asjue7tasd, without "enter" or "space" between each character, will the buffer will hold it all? and we'll be able to process each one of the letters separately? We'll see counter = 2? (In case 'a' was chosen from the first place)
            – JavaStarter
            Nov 18 '18 at 14:10












          • @JavaStarter There's no buffer, read the answer again.
            – Purple Ice
            Nov 18 '18 at 15:16










          • @JavaStarter Don't ask what will happen with a certain input. Compile it and try for yourself. If it does not work, well THEN you have a good follow up question.
            – Broman
            Nov 18 '18 at 15:43










          • @Broman you're right, I was away from the keyboard and could not do the test myself. In any case, thanks for the explanation, working properly. But I want to understand what is happening "behind the scenes" - how can the variable c defined as char, contains more than one value? Or is not that the case?
            – JavaStarter
            Nov 18 '18 at 16:43












          • You're just reading a single character. There is absolutely no need to use scanf for this, and doing so is a terrible thing to show a beginner. Beginners should have it constantly driven into their brain to avoid scanf like the plague. It is absolutely the wrong way to read a single character.
            – William Pursell
            Nov 18 '18 at 23:39
















          but if I type the next string: asjue7tasd, without "enter" or "space" between each character, will the buffer will hold it all? and we'll be able to process each one of the letters separately? We'll see counter = 2? (In case 'a' was chosen from the first place)
          – JavaStarter
          Nov 18 '18 at 14:10






          but if I type the next string: asjue7tasd, without "enter" or "space" between each character, will the buffer will hold it all? and we'll be able to process each one of the letters separately? We'll see counter = 2? (In case 'a' was chosen from the first place)
          – JavaStarter
          Nov 18 '18 at 14:10














          @JavaStarter There's no buffer, read the answer again.
          – Purple Ice
          Nov 18 '18 at 15:16




          @JavaStarter There's no buffer, read the answer again.
          – Purple Ice
          Nov 18 '18 at 15:16












          @JavaStarter Don't ask what will happen with a certain input. Compile it and try for yourself. If it does not work, well THEN you have a good follow up question.
          – Broman
          Nov 18 '18 at 15:43




          @JavaStarter Don't ask what will happen with a certain input. Compile it and try for yourself. If it does not work, well THEN you have a good follow up question.
          – Broman
          Nov 18 '18 at 15:43












          @Broman you're right, I was away from the keyboard and could not do the test myself. In any case, thanks for the explanation, working properly. But I want to understand what is happening "behind the scenes" - how can the variable c defined as char, contains more than one value? Or is not that the case?
          – JavaStarter
          Nov 18 '18 at 16:43






          @Broman you're right, I was away from the keyboard and could not do the test myself. In any case, thanks for the explanation, working properly. But I want to understand what is happening "behind the scenes" - how can the variable c defined as char, contains more than one value? Or is not that the case?
          – JavaStarter
          Nov 18 '18 at 16:43














          You're just reading a single character. There is absolutely no need to use scanf for this, and doing so is a terrible thing to show a beginner. Beginners should have it constantly driven into their brain to avoid scanf like the plague. It is absolutely the wrong way to read a single character.
          – William Pursell
          Nov 18 '18 at 23:39




          You're just reading a single character. There is absolutely no need to use scanf for this, and doing so is a terrible thing to show a beginner. Beginners should have it constantly driven into their brain to avoid scanf like the plague. It is absolutely the wrong way to read a single character.
          – William Pursell
          Nov 18 '18 at 23:39


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361420%2finput-buffer-to-get-a-an-input-c-programming%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to send String Array data to Server using php in android

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Is anime1.com a legal site for watching anime?