Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. do-while did not work

do-while did not work

Scheduled Pinned Locked Moved C / C++ / MFC
career
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    ho_khalaf
    wrote on last edited by
    #1

    #include <stdio.h>
    #include <stdlib.h>

    int main(void) {
    char again;
    do{
    printf("insert y or Y to repeat");
    fflush(stdout);
    scanf("%c",&again);
    }while(again=='y'||again=='Y');
    }

    i wrote this code to create a loop that when insert y or Y do the job again but did not work.when i enter first y the loop be end.

    A W 2 Replies Last reply
    0
    • H ho_khalaf

      #include <stdio.h>
      #include <stdlib.h>

      int main(void) {
      char again;
      do{
      printf("insert y or Y to repeat");
      fflush(stdout);
      scanf("%c",&again);
      }while(again=='y'||again=='Y');
      }

      i wrote this code to create a loop that when insert y or Y do the job again but did not work.when i enter first y the loop be end.

      A Offline
      A Offline
      Andy411
      wrote on last edited by
      #2

      As mentioned in the QA section: Please don't cross post. I decided to answer you here. Did you take a look at "again" in the debugger? You will recognize that in the second round thru the loop it will have the value 0x0a == \r it's the carriage return from your input. You can try something like this to catch the CR:

      char cr;
      char again;
      do
      {
      printf("insert y or Y to repeat");
      fflush(stdout);

      again = getchar();
      cr = getchar();
      

      }while(again=='y'||again=='Y');

      H 1 Reply Last reply
      0
      • A Andy411

        As mentioned in the QA section: Please don't cross post. I decided to answer you here. Did you take a look at "again" in the debugger? You will recognize that in the second round thru the loop it will have the value 0x0a == \r it's the carriage return from your input. You can try something like this to catch the CR:

        char cr;
        char again;
        do
        {
        printf("insert y or Y to repeat");
        fflush(stdout);

        again = getchar();
        cr = getchar();
        

        }while(again=='y'||again=='Y');

        H Offline
        H Offline
        ho_khalaf
        wrote on last edited by
        #3

        thank you very much Andy411.its work good. what do you use

        cr = getchar();

        what do this code

        A 1 Reply Last reply
        0
        • H ho_khalaf

          thank you very much Andy411.its work good. what do you use

          cr = getchar();

          what do this code

          A Offline
          A Offline
          Andy411
          wrote on last edited by
          #4

          How do you learn C? Don't you have a book or a tutoroial with an index? :confused: That's what google answered me: http://www.cplusplus.com/reference/cstdio/getchar/[^] PS: Sorry if my answer sounds a bit rude, but I am realy confused about the question what getchar does. If I were you, my first step would be asking google, bing are whatever searchmachine you want. Or taking a look inside a book. If I don't understand the description/answer there, I would ask in a forum again.

          1 Reply Last reply
          0
          • H ho_khalaf

            #include <stdio.h>
            #include <stdlib.h>

            int main(void) {
            char again;
            do{
            printf("insert y or Y to repeat");
            fflush(stdout);
            scanf("%c",&again);
            }while(again=='y'||again=='Y');
            }

            i wrote this code to create a loop that when insert y or Y do the job again but did not work.when i enter first y the loop be end.

            W Offline
            W Offline
            water moon2012
            wrote on last edited by
            #5

            run your code ,you will get the result. "insert y or Y to repeatY insert y or Y to repeat" then code end. it is useless.

            fflush(stdout);

            you can write

            fflush(stdin);

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups