Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#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
Updated 27-Nov-12 11:59am
v3

you pressed 'y; and '\n'(Enter).two chars.
you can modify it
C++
#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');
}
 
Share this answer
 
v2
Comments
ho_khalaf 27-Nov-12 15:38pm    
your code is good but when i use it like this did not work
#include <stdio.h>
#include <stdlib.h>

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
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.
C
#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:
C
#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]
 
Share this answer
 
v2
Comments
ho_khalaf 27-Nov-12 15:40pm    
your code is good but when i use it like this did not work.
#include <stdio.h>
#include <stdlib.h>

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.
[no name] 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.
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.
 
Share this answer
 
Try this

C++
#include <stdio.h>
#include <stdlib.h>
 
int main(void) {
    char again = 'y';
   //...............
}
 
Share this answer
 
v2
Comments
ho_khalaf 27-Nov-12 15:41pm    
your code is good but when i use it like this did not work.
#include <stdio.h>
#include <stdlib.h>

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!!

C++
#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
C++
int main()
{
       char c = 'y';
       while(c == 'y' || c == 'Y')
       {
            printf("Give y or Y to loop again: ");
            scanf("%c", &c);
            fflush(stdout);
       }
}
 
Share this answer
 
v2
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
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900