Problems with a while loop

This is a discussion on Problems with a while loop within the C Programming forums, part of the General Programming Boards category; This isn't all the code of a program nor is it finished so its v.likely to not be perfect yet. ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    137

    Problems with a while loop

    This isn't all the code of a program nor is it finished so its v.likely to not be perfect yet. My problem is with this loop, when the program comes to it, it prints to screen two lots of "choose from the....." and then waits for the input that will get put in &choose.
    Any ideas why this would be. Thanks


    Code:
    while (choose != 'q') {
    	printf("Choose from the following set of options:\n");
    	printf("Get the (m)edian, (a)verage, or (q)uit?: ");
    	scanf("%c",&choose);
    
    //PROBLEM WITH WHILE LOOP ABOVE
    
       int i;
    
    	switch (choose) {
    
       	case 'm':
             bubble_srt(ptr, num);
             for (i=0;i<num;i++)
             	printf("ptr[i] = %d",i,ptr[i]);
          	//printf("The median for your set of numbers will be:\n%f",median);
          	break;
    
    		case 'a':
          	for (i=0;i<num;i++) {
             	sum += ptr[i];};
             mean = (sum/num);
    			printf("The average for your data set will be:\n%f",mean);
             break;
    
          default:
          break;
      	}
    
    }
    http://uk.geocities.com/ca_chorltonkids

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    sounds like you have a newline char left in your input buffer. this problem has been dealt with at least 10 times this week alone. Read some more threads or search for the answer.
    if you see this .... fflush(stdin) recommended,ignore it. its bad info.













    ok i relent.....

    while (getchar() != '\n') continue;

    This will clear ur input buffer. now u decide where to place it in your code.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    Code:
    while (choose != 'q') {
       while (getchar() != '\n') {}
    	printf("Choose from the following set of options:\n");
    	printf("Get the (m)edian, (a)verage, or (q)uit?: ");
    	scanf("%c",&choose);
    Works, thanks
    http://uk.geocities.com/ca_chorltonkids

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 09:09 AM
  2. "for" loop problems....
    By hayai32 in forum C Programming
    Replies: 4
    Last Post: 05-04-2005, 01:20 AM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  4. simple collision detection problems
    By Mr_Jack in forum Game Programming
    Replies: 0
    Last Post: 03-31-2004, 03:59 PM
  5. problems with greatest to least sorting
    By Mr_Jack in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2004, 09:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21