Click here to Skip to main content
Sign Up to vote bad
good
See more: C++Ccrosspost
#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.
 
[edit]code blocks fixed[/edit]
Posted 26-Nov-12 19:39pm
Edited 27-Nov-12 11:59am
Nelek27K


5 solutions

I works. But it doesn't work the way you think it does.
As a simple workaround you may use a string instead of char as input for scanf, e.g.
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char buf[0x100];
    do{
        printf("insert y or Y to repeat");
        fflush(stdout);
        while (scanf("%s",buf) != 1);
    }while(buf[0]=='y'|| buf[0]=='Y');
}
 

[Update]
try this one:
#include <stdio.h>
#include <stdlib.h>
int scan_frac(int *num,int *denom);
 
int main(void){
  int num1,denom1;
  char buf[0x100];
  do{
    if ( scan_frac(&num1,&denom1) == 2)
    {
      if ( denom1 != 0)
      {
        printf("%d/%d = %g\n", num1, denom1, ((double)num1)/denom1);
        fflush(stdout);
      }
    }
 
    printf("insert (y) to do another division\n");
    fflush(stdout);
    scanf("%s", buf);
  }while(buf[0]=='y'||buf[0]=='Y');
 
  return 0;
}
int scan_frac(int *num,int *denom){
  int values;
  int n,dn;
  printf("insert numerator and denominator (use blank as separator): ");
  fflush(stdout);
  values = scanf("%d %d",&n,&dn);
  *num=n;
  *denom=dn;
  return values;
}
[/Update]
  Permalink  
Comments
ho_khalaf - 27-Nov-12 15:40pm
your code is good but when i use it like this did not work. #include #include void scan_frac(int *num,int *denom); int main(void){ int num1,denom1; char again; do{ scan_frac(&num1,&denom1); printf("insert (y)to do another problem"); fflush(stdout); again=getchar(); getchar(); }while(again=='y'||again=='Y'); return 0; }
ho_khalaf - 27-Nov-12 17:34pm
thank you very much.i test your solution and worked good. you are good programer in c\c++ programing.
CPallini - 27-Nov-12 17:45pm
You are welcome. Thanks for your appreciation.
midle110 - 27-Nov-12 19:21pm
my English is only the begining,but you program is much better than me.
CPallini - 28-Nov-12 3:46am
It is only experience, both your English and your programming skills will be soon better.
Albert Holguin - 27-Nov-12 19:05pm
Does he really understand why his code doesn't work?
ho_khalaf - 28-Nov-12 9:14am
Yes i understand.I use a "string" instead of "char" as input for "scanf".
Albert Holguin - 28-Nov-12 10:11am
Why?
ho_khalaf - 28-Nov-12 10:24am
because of compiler bug.when use char in scanf("%c",a) in big program scanf read hole line and it dose not read just a character.
midle110 - 29-Nov-12 1:20am
scanf("%s",buff),scanf("%c",buff),you should know the difference of %s and %c
you pressed 'y; and '\n'(Enter).two chars.
you can modify it
 
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char again;
    do{
        printf("insert y or Y to repeat");
        fflush(stdout);
        scanf("%c",&again);
		getchar();
    }while(again=='y'||again=='Y');
}
  Permalink  
Comments
ho_khalaf - 27-Nov-12 15:38pm
your code is good but when i use it like this did not work #include #include void scan_frac(int *num,int *denom); int main(void){ int num1,denom1; char again; do{ scan_frac(&num1,&denom1); printf("insert (y)to do another problem"); fflush(stdout); again=getchar(); getchar(); }while(again=='y'||again=='Y'); return 0; } void scan_frac(int *num,int *denom){ int n,dn; printf("insert numerator and denominator:"); fflush(stdout); scanf("%d%d",&n,&dn); *num=n; *denom=dn; }
Albert Holguin - 27-Nov-12 19:01pm
Edit: Fixed code block html tags
You should have noticed from the replies to you other questions.
Only ask your question ONCE.
Cross-posting like this will only annoy those people who might otherwise help you.
  Permalink  
Try this
 
#include <stdio.h>
#include <stdlib.h>
 
int main(void) {
    char again = 'y';
   //...............
}
  Permalink  
Comments
ho_khalaf - 27-Nov-12 15:41pm
your code is good but when i use it like this did not work. #include #include void scan_frac(int *num,int *denom); int main(void){ int num1,denom1; char again; do{ scan_frac(&num1,&denom1); printf("insert (y)to do another problem"); fflush(stdout); again=getchar(); getchar(); }while(again=='y'||again=='Y'); return 0; }
Albert Holguin - 27-Nov-12 18:59pm
Edit: Fixed code block html tags
I dont know how correct is that write below!!
But when you scan a char as a string it works!!
 
#include <stdio.h>
#include <stdlib.h>

int main()
{
      char c;
      do
      {
            printf("Give y or Y to loop again: ");
            scanf("%s", &c);
            fflush(stdout);
      }while(c == 'y' || c == 'Y');
}

 
If do while loot not works again just try the while loop
int main()
{
       char c = 'y';
       while(c == 'y' || c == 'Y')
       {
            printf("Give y or Y to loop again: ");
            scanf("%c", &c);
            fflush(stdout);
       }
}
  Permalink  
Comments
Albert Holguin - 27-Nov-12 19:00pm
Edit: Fixed code block html tags

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Prasad_Kulkarni 407
1 Sergey Alexandrovich Kryukov 340
2 _Amy 338
3 Christian Graus 273
4 Rohan Leuva 250
0 Sergey Alexandrovich Kryukov 6,649
1 Prasad_Kulkarni 3,281
2 _Amy 3,065
3 OriginalGriff 2,989
4 CPallini 2,696


Advertise | Privacy | Mobile
Web04 | 2.6.130617.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid