0

I wrote my code for a program that finds the prime factors and distinct prime factors of any integer between 1 and 2000. However, now I need to write code that will loop the program and ask the user for another number until the user wants to stop. It would look as followed:

Do you want to try another number? Say Y(es) or N(o): y //Then it would ask for a number between 1 and 2000, and the program will run.

Do you want to try another number? Say Y(es) or N(o): n ---> "Thank you for using my program. Good Bye!:

I have attempted writing the code for this, but as you can see I got stuck at the end where I put the comments in place of code. I don't know how to loop it so the program will repeat again. That is the only thing I feel like I am stuck on. I feel like my code below for this problem is correct, it just needs to loop the program which I am unsure on how to do. Hope you can help.

int main()  {
  unsigned num;
  char response;

  printf("Please enter a positive integer greater than 1 and less than 2000:");
  scanf("%d", &num);
  if (num > 1 && num < 2000){
    printf("The distinctive prime facters are given below: \n");
    printDistinctPrimeFactors(num);
    printf("All of the prime factors are given below: \n");
    printPrimeFactors(num);
  }
  else {
    printf("Sorry that number does not fall within the given range.\n");
  }
  printf("Do you want to try another number? Say Y(es) or N(o): \n");
  response = getchar();
  if(response == 'Y' || answer == 'y')
  //then loop back through program
  //else print "Good Bye!"
  }
  return 0;
}
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
Tucker Sampson
  • 41
  • 1
  • 2
  • 7
  • 1
    @Tucker, it's bad form to overwrite your question with a completely different question. It makes the answers go completely out of context. I'm going to roll back your edit for now - please post a new question for the GLX problem. – Carl Norum Jan 31 '13 at 17:56

3 Answers3

1

You want to put a do {...} while(condition) around your code. The condition would be response =='y' || response =='Y'. print 'Goodbye' when you're out of the loop and your good. Something like this:

int main() {
    char response;
    do {
        //your code
    } while(response =='y' || response =='Y');
    printf("Goodbye");
    return 0;
}

This is different from a regular while loop because it checks the condition after the first run of your loop body.

Stephen
  • 2,365
  • 17
  • 21
0

The basic idea goes something like this:

char response = 'y';
do {
    workSomeMagic();
    response = getNextInput();
while ((response == 'y') || (response == 'Y'));

Obviously, the workSomeMagic() and getNextInput() need to be fleshed out but they're unrelated to the question at hand, which is how to do a loop while a certain condition is true.

workSomeMagic() is basically your number input and prime factor calculations, while getNextInput() retrieves a character from the user.

I'd be wary of using getchar() for this though since, if you enter "y", you'll get both y and <newline> in the input stream and the newline will cause the next iteration to exit.

Better to use a line-based input function such as the excellent one found here.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0
int main()  
{
char response = 'y';
do {

 /*
  your 
  code
  here
*/
printf("Do you want to try another number? Say Y(es) or N(o): \n");
response = getch();

} while ((response == 'y') || (response == 'Y'));
 printf("Goodbye");
 return 0;
}