Click here to Skip to main content
11,429,154 members (62,435 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C crosspost
#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 20:39pm
Edited 27-Nov-12 12:59pm
Nelek70.7K
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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  
v2
Comments
ho_khalaf at 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 at 27-Nov-12 19:01pm
   
Edit: Fixed code block html tags
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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  
v2
Comments
ho_khalaf at 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 at 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 at 27-Nov-12 17:45pm
   
You are welcome.
Thanks for your appreciation.
midle110 at 27-Nov-12 19:21pm
   
my English is only the begining,but you program is much better than me.
CPallini at 28-Nov-12 3:46am
   
It is only experience, both your English and your programming skills will be soon better.
Albert Holguin at 27-Nov-12 19:05pm
   
Does he really understand why his code doesn't work?
ho_khalaf at 28-Nov-12 9:14am
   
Yes i understand.I use a "string" instead of "char" as input for "scanf".
Albert Holguin at 28-Nov-12 10:11am
   
Why?
ho_khalaf at 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 at 29-Nov-12 1:20am
   
scanf("%s",buff),scanf("%c",buff),you should know the difference of %s and %c
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try this

#include <stdio.h>
#include <stdlib.h>
 
int main(void) {
    char again = 'y';
   //...............
}
  Permalink  
v2
Comments
ho_khalaf at 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 at 27-Nov-12 18:59pm
   
Edit: Fixed code block html tags
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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  
v2
Comments
Albert Holguin at 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)



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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100