0

Possible Duplicate:
Debugging code in C

Can someone tell me what is wrong with my code and why it is producing this output. The output gets weird once I type in "yes"...I want my code to work correctly for Y/y and N/n...but anything else typed into my code I want it to say "Invalid Input. Please try again." Could someone edit my code so it does that.

Code:

int main(){
  unsigned num;
  char response;

  do{
     printf("Please enter a positive integer greater than 1 and less than 2000: ");
     scanf("%d", &num);
     if (num > 1 && num < 2000){
        printf("All the prime factors of %d are given below: \n", num);
        printPrimeFactors(num);
        printf("\n\nThe distinct prime factors of %d are given below: \n", num);
        printDistinctPrimeFactors(num);
     }
     else{
        printf("\nSorry that number does not fall between 1 and 2000.\n");
     }
     printf("\n\nDo you want to try another number? Say Y(es) or N(o): ");
     getchar();
     response = getchar();
  }
 while(response == 'Y' || response == 'y'); // if response is Y or y then program runs again
 printf("Thank you for using my program. Good Bye!\n\n"); //if not Y or y, program terminates
 return 0;
}

Output:

Please enter a positive integer greater than 1 and less than 2000: 1600
All the prime factors of 1600 are given below:
2 2 2 2 2 2 5 5

The distinct prime factors of 1600 are given below:
2 5

Do you want to try another number? Say Y(es) or N(o): yes
Please enter a positive integer greater than 1 and less than 2000: All the prime factors of 1600 are given below:
2 2 2 2 2 2 5 5

The distinct prime factors of 1600 are given below:
2 5

Do you want to try another number? Say Y(es) or N(o): Thank you for using my program. Good  Bye!
2
  • you need another while loop around your getchar, keep looping till its either a y or n, every time its not, tell them its not Commented Feb 1, 2013 at 22:39
  • StackOverflow does not work by posting your not-working code here and asking someone to edit it for you so it will work. This is not a homework completion site or code writing service. You need to post only the relevant code, and ask specific questions about how you can solve the problem. The FAQ has more information about what types of question to ask here, as well as some tips on how to ask them in a way that will improve your chances of getting help. Good luck. Commented Feb 1, 2013 at 22:41

1 Answer 1

2

You input the string "yes" but then you only read a single character from that string, leaving "es" in the input. This is then attempted to be read by the scanf call and as it's not a number it will fail, leaving the letters still in the input buffer.

Sign up to request clarification or add additional context in comments.

6 Comments

How do I fix this...I have to use getchar() for this assignment. So, in that case do you think my professor will let this weird output slide since she specifically specified to use getchar()?
scanf will return an integer count of the number of values it reads. It isn't being checked either. It should be a 1 that is returned if it reads exactly one integer as is requested. So hitting return where the number is asked for would cause an issue too since the value of num wont be changed. Perhaps setting num to 0 inside the loop would make it more intelligible.
Does your instructor require that "yes" and "no" be accepted as inputs, or only single-character responses, like Y and N?
@mikemikee The answer is really staring you in the face... You write three letters for input, but read only one letter...
Think about doing a getchar and useing it. Then do some more getchar calls in a loop until you get the end of line character. Your test can see if what you get is right. You might think about what happens if they type ' ' and then 'y' or 'n' so you have to make the input code smarter.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.