printf in a while loop is printing twice instead of once [duplicate]












1















This question already has an answer here:




  • scanf() leaves the new line char in the buffer

    3 answers




In this code it need to calculate the number of times the operators appear.
It doesn't calculate it and while running the code it prints "Please enter a note. to finish press Q:" twice and i don't know whats wrong.



#include <stdio.h>

int main(void)
{

char note;
int result1 = 0, result2 = 0, result3 = 0, result4 = 0;


do
{
printf("Please enter a note. to finish press Q:n");
scanf("%c", &note);
switch (note)
{
case'+':
result1 = result1 + 1;
break;
case'-':
result2 = result2 + 1;
break;
case'*':
result3 = result3 + 1;
break;
case'/':
result4 = result4 + 1;
break;
}

} while (note != 'Q');

printf("+ appears %d timesn", result1);
printf("- appears %d timesn", result2);
printf("* appears %d timesn", result3);
printf("/ appears %d timesn", result4);

system("pause");
}









share|improve this question















marked as duplicate by Govind Parmar, Andrew Henle, pushkin, Community Nov 21 '18 at 20:13


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Next time you copy & paste the code from your IDE to your question exactly how it is.
    – Swordfish
    Nov 20 '18 at 18:16










  • Not identical question - but worth reading: stackoverflow.com/questions/13542055/…
    – Igal S.
    Nov 20 '18 at 18:21












  • You are reading the character (ex. +) and then reading a newline (you pressed enter). You can not print the Plese enter a note .. if the last character is newline or if you receive a newline scanf again or similar.
    – Kamil Cuk
    Nov 20 '18 at 18:24










  • Try using getchar() instead of scanf()
    – iVoid
    Nov 20 '18 at 19:14










  • Tip: always add a default case to a switch statement. This helps you detect unexpected events.
    – Tim Randall
    Nov 20 '18 at 20:01
















1















This question already has an answer here:




  • scanf() leaves the new line char in the buffer

    3 answers




In this code it need to calculate the number of times the operators appear.
It doesn't calculate it and while running the code it prints "Please enter a note. to finish press Q:" twice and i don't know whats wrong.



#include <stdio.h>

int main(void)
{

char note;
int result1 = 0, result2 = 0, result3 = 0, result4 = 0;


do
{
printf("Please enter a note. to finish press Q:n");
scanf("%c", &note);
switch (note)
{
case'+':
result1 = result1 + 1;
break;
case'-':
result2 = result2 + 1;
break;
case'*':
result3 = result3 + 1;
break;
case'/':
result4 = result4 + 1;
break;
}

} while (note != 'Q');

printf("+ appears %d timesn", result1);
printf("- appears %d timesn", result2);
printf("* appears %d timesn", result3);
printf("/ appears %d timesn", result4);

system("pause");
}









share|improve this question















marked as duplicate by Govind Parmar, Andrew Henle, pushkin, Community Nov 21 '18 at 20:13


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Next time you copy & paste the code from your IDE to your question exactly how it is.
    – Swordfish
    Nov 20 '18 at 18:16










  • Not identical question - but worth reading: stackoverflow.com/questions/13542055/…
    – Igal S.
    Nov 20 '18 at 18:21












  • You are reading the character (ex. +) and then reading a newline (you pressed enter). You can not print the Plese enter a note .. if the last character is newline or if you receive a newline scanf again or similar.
    – Kamil Cuk
    Nov 20 '18 at 18:24










  • Try using getchar() instead of scanf()
    – iVoid
    Nov 20 '18 at 19:14










  • Tip: always add a default case to a switch statement. This helps you detect unexpected events.
    – Tim Randall
    Nov 20 '18 at 20:01














1












1








1








This question already has an answer here:




  • scanf() leaves the new line char in the buffer

    3 answers




In this code it need to calculate the number of times the operators appear.
It doesn't calculate it and while running the code it prints "Please enter a note. to finish press Q:" twice and i don't know whats wrong.



#include <stdio.h>

int main(void)
{

char note;
int result1 = 0, result2 = 0, result3 = 0, result4 = 0;


do
{
printf("Please enter a note. to finish press Q:n");
scanf("%c", &note);
switch (note)
{
case'+':
result1 = result1 + 1;
break;
case'-':
result2 = result2 + 1;
break;
case'*':
result3 = result3 + 1;
break;
case'/':
result4 = result4 + 1;
break;
}

} while (note != 'Q');

printf("+ appears %d timesn", result1);
printf("- appears %d timesn", result2);
printf("* appears %d timesn", result3);
printf("/ appears %d timesn", result4);

system("pause");
}









share|improve this question
















This question already has an answer here:




  • scanf() leaves the new line char in the buffer

    3 answers




In this code it need to calculate the number of times the operators appear.
It doesn't calculate it and while running the code it prints "Please enter a note. to finish press Q:" twice and i don't know whats wrong.



#include <stdio.h>

int main(void)
{

char note;
int result1 = 0, result2 = 0, result3 = 0, result4 = 0;


do
{
printf("Please enter a note. to finish press Q:n");
scanf("%c", &note);
switch (note)
{
case'+':
result1 = result1 + 1;
break;
case'-':
result2 = result2 + 1;
break;
case'*':
result3 = result3 + 1;
break;
case'/':
result4 = result4 + 1;
break;
}

} while (note != 'Q');

printf("+ appears %d timesn", result1);
printf("- appears %d timesn", result2);
printf("* appears %d timesn", result3);
printf("/ appears %d timesn", result4);

system("pause");
}




This question already has an answer here:




  • scanf() leaves the new line char in the buffer

    3 answers








c while-loop printf do-while






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 22:45









dustinos3

4611616




4611616










asked Nov 20 '18 at 18:15









Shir Ben Avi

61




61




marked as duplicate by Govind Parmar, Andrew Henle, pushkin, Community Nov 21 '18 at 20:13


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Govind Parmar, Andrew Henle, pushkin, Community Nov 21 '18 at 20:13


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Next time you copy & paste the code from your IDE to your question exactly how it is.
    – Swordfish
    Nov 20 '18 at 18:16










  • Not identical question - but worth reading: stackoverflow.com/questions/13542055/…
    – Igal S.
    Nov 20 '18 at 18:21












  • You are reading the character (ex. +) and then reading a newline (you pressed enter). You can not print the Plese enter a note .. if the last character is newline or if you receive a newline scanf again or similar.
    – Kamil Cuk
    Nov 20 '18 at 18:24










  • Try using getchar() instead of scanf()
    – iVoid
    Nov 20 '18 at 19:14










  • Tip: always add a default case to a switch statement. This helps you detect unexpected events.
    – Tim Randall
    Nov 20 '18 at 20:01


















  • Next time you copy & paste the code from your IDE to your question exactly how it is.
    – Swordfish
    Nov 20 '18 at 18:16










  • Not identical question - but worth reading: stackoverflow.com/questions/13542055/…
    – Igal S.
    Nov 20 '18 at 18:21












  • You are reading the character (ex. +) and then reading a newline (you pressed enter). You can not print the Plese enter a note .. if the last character is newline or if you receive a newline scanf again or similar.
    – Kamil Cuk
    Nov 20 '18 at 18:24










  • Try using getchar() instead of scanf()
    – iVoid
    Nov 20 '18 at 19:14










  • Tip: always add a default case to a switch statement. This helps you detect unexpected events.
    – Tim Randall
    Nov 20 '18 at 20:01
















Next time you copy & paste the code from your IDE to your question exactly how it is.
– Swordfish
Nov 20 '18 at 18:16




Next time you copy & paste the code from your IDE to your question exactly how it is.
– Swordfish
Nov 20 '18 at 18:16












Not identical question - but worth reading: stackoverflow.com/questions/13542055/…
– Igal S.
Nov 20 '18 at 18:21






Not identical question - but worth reading: stackoverflow.com/questions/13542055/…
– Igal S.
Nov 20 '18 at 18:21














You are reading the character (ex. +) and then reading a newline (you pressed enter). You can not print the Plese enter a note .. if the last character is newline or if you receive a newline scanf again or similar.
– Kamil Cuk
Nov 20 '18 at 18:24




You are reading the character (ex. +) and then reading a newline (you pressed enter). You can not print the Plese enter a note .. if the last character is newline or if you receive a newline scanf again or similar.
– Kamil Cuk
Nov 20 '18 at 18:24












Try using getchar() instead of scanf()
– iVoid
Nov 20 '18 at 19:14




Try using getchar() instead of scanf()
– iVoid
Nov 20 '18 at 19:14












Tip: always add a default case to a switch statement. This helps you detect unexpected events.
– Tim Randall
Nov 20 '18 at 20:01




Tip: always add a default case to a switch statement. This helps you detect unexpected events.
– Tim Randall
Nov 20 '18 at 20:01












2 Answers
2






active

oldest

votes


















0














To debug this, insert



  printf("note='%c'n", note);


immediately after the scanf.
You'll see the problem immediately.






share|improve this answer





























    0














    Using scanf("%c", %c), you will be reading one character at a time. Which is fine, but..



    scanf will not "re-scan" for input, until the buffer is clear.



    So, if the user were to input something longer than 1 character, your loop will iterate for every character the string is long + 1 (n counts as a character).



    Example (note it loops 3 times +, +, n):



    Please enter a note. to finish press Q:
    ++
    Please enter a note. to finish press Q:
    Please enter a note. to finish press Q:
    Please enter a note. to finish press Q:


    That's the trick. You need to change your code to accommodate this (i.e. hide the please enter message until empty or read a char array note[128]... scanf('%s', note) vs. single character with a sub loop to iterate the over the input, etc. or some other way).






    share|improve this answer






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      To debug this, insert



        printf("note='%c'n", note);


      immediately after the scanf.
      You'll see the problem immediately.






      share|improve this answer


























        0














        To debug this, insert



          printf("note='%c'n", note);


        immediately after the scanf.
        You'll see the problem immediately.






        share|improve this answer
























          0












          0








          0






          To debug this, insert



            printf("note='%c'n", note);


          immediately after the scanf.
          You'll see the problem immediately.






          share|improve this answer












          To debug this, insert



            printf("note='%c'n", note);


          immediately after the scanf.
          You'll see the problem immediately.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 19:04









          maharvey67

          36219




          36219

























              0














              Using scanf("%c", %c), you will be reading one character at a time. Which is fine, but..



              scanf will not "re-scan" for input, until the buffer is clear.



              So, if the user were to input something longer than 1 character, your loop will iterate for every character the string is long + 1 (n counts as a character).



              Example (note it loops 3 times +, +, n):



              Please enter a note. to finish press Q:
              ++
              Please enter a note. to finish press Q:
              Please enter a note. to finish press Q:
              Please enter a note. to finish press Q:


              That's the trick. You need to change your code to accommodate this (i.e. hide the please enter message until empty or read a char array note[128]... scanf('%s', note) vs. single character with a sub loop to iterate the over the input, etc. or some other way).






              share|improve this answer




























                0














                Using scanf("%c", %c), you will be reading one character at a time. Which is fine, but..



                scanf will not "re-scan" for input, until the buffer is clear.



                So, if the user were to input something longer than 1 character, your loop will iterate for every character the string is long + 1 (n counts as a character).



                Example (note it loops 3 times +, +, n):



                Please enter a note. to finish press Q:
                ++
                Please enter a note. to finish press Q:
                Please enter a note. to finish press Q:
                Please enter a note. to finish press Q:


                That's the trick. You need to change your code to accommodate this (i.e. hide the please enter message until empty or read a char array note[128]... scanf('%s', note) vs. single character with a sub loop to iterate the over the input, etc. or some other way).






                share|improve this answer


























                  0












                  0








                  0






                  Using scanf("%c", %c), you will be reading one character at a time. Which is fine, but..



                  scanf will not "re-scan" for input, until the buffer is clear.



                  So, if the user were to input something longer than 1 character, your loop will iterate for every character the string is long + 1 (n counts as a character).



                  Example (note it loops 3 times +, +, n):



                  Please enter a note. to finish press Q:
                  ++
                  Please enter a note. to finish press Q:
                  Please enter a note. to finish press Q:
                  Please enter a note. to finish press Q:


                  That's the trick. You need to change your code to accommodate this (i.e. hide the please enter message until empty or read a char array note[128]... scanf('%s', note) vs. single character with a sub loop to iterate the over the input, etc. or some other way).






                  share|improve this answer














                  Using scanf("%c", %c), you will be reading one character at a time. Which is fine, but..



                  scanf will not "re-scan" for input, until the buffer is clear.



                  So, if the user were to input something longer than 1 character, your loop will iterate for every character the string is long + 1 (n counts as a character).



                  Example (note it loops 3 times +, +, n):



                  Please enter a note. to finish press Q:
                  ++
                  Please enter a note. to finish press Q:
                  Please enter a note. to finish press Q:
                  Please enter a note. to finish press Q:


                  That's the trick. You need to change your code to accommodate this (i.e. hide the please enter message until empty or read a char array note[128]... scanf('%s', note) vs. single character with a sub loop to iterate the over the input, etc. or some other way).







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 '18 at 21:25

























                  answered Nov 20 '18 at 20:50









                  static_cast

                  5141415




                  5141415















                      Popular posts from this blog

                      Create new schema in PostgreSQL using DBeaver

                      Deepest pit of an array with Javascript: test on Codility

                      Costa Masnaga