Thread: do-while loop doesn't work

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    67

    do-while loop doesn't work

    for some reason my do-while implementation doesn't work. here is my code:
    Code:
    #include <stdio.h>
    
    char getAns (char mesg[])
    {
    	char ans;
    	printf ("&#37;s", mesg);
    	ans = getchar();
    	getchar();
    	return toupper(ans);
    }
    
    void swap(int *p, int *q)
    {
        int tmp;
    	
        tmp = *p;
        *p = *q;
        *q = tmp;
    }
    
    void bubble (int a[], int N)
    {
    	int i, j;
    	
    	for (i = 0; i < N; i++)
    		for (j = N - 1; i < j; j--)
    		if (a[j - 1] > a[j])
    			swap(&a[j - 1], &a[j]);
    }
    
    int	main (void)
    {	
    	char ans;
    	int i, N;
    	int a[N + 1];
    	do
    	{
    	printf ("Enter the amount of numbers to be sorted: ");
    	scanf ("%d", &N);
    	printf("Now enter the numbers:\n");
    	for (i = 0; i < N; i++){
    		scanf ("%d", &a[i]);}
    	bubble (a, N);
    	for (i = 0; i < N; i++)
    	printf("%d ", a[i]);
    	printf("\nYour numbers were sorted using BubbleSort.\n");
    	ans = getAns ("\nSort again (Y/N) ?");
    	} while (ans == 'Y');
    	return 0;
    }
    thanks for any suggestions

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >return toupper(ans);
    You need to include <ctype.h> for this function.

    >int i, N;
    >int a[N + 1];
    That's wrong on multiple levels. First, N hasn't been initialized, yet it's an automatic variable. The value is indeterminate. Second, unless you're using C99, you can't use a non-constant value as the size for an array.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    Quote Originally Posted by Prelude View Post
    >int i, N;
    >int a[N + 1];

    That's wrong on multiple levels. First, N hasn't been initialized, yet it's an automatic variable. The value is indeterminate. Second, unless you're using C99, you can't use a non-constant value as the size for an array.
    ok i added <ctype.h>...and yes i do realize that it was a stupid mistake, but the loop still doesn't work. Before i added the loop my program worked fine. Regarding the int i, N, i am not sure what C version i am using; since compiler gave no warnings and program functioned fine i assumed i made no big mistakes in the code.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,656
    You're mixing scanf with getchar.

    Your last scanf leaves a \n on the input stream
    Your first getchar reads that \n
    Result, You're here asking a question.

    Several FAQs deal with reading input. Concentrate on the ones which read input using fgets().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    ok cheers now it works. i swapped:
    ans = getchar();
    getchar();

    for:
    getchar();
    ans = getchar():

    and it works. thanks a lot

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,656
    Try typing a space after the last number you input.

    "Works" is entirely subjective, because as far as I can tell, you've just swept the problem under the carpet.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    Your program is perfect in every way.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's not. For a perfect program, I'd say get rid of scanf to read input since it's so error prone to leave crap in the input buffer. The program doesn't check if there's anything left in the input buffer either, so it's a walking time bomb, or rather, it's just luck that the program works as expected.

    Btw, a tip for you: indent each block once and once only. Each if, each while, and each line of code after those should be indented once more. And it's not truly wise to mix spaces and tabs. Use one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Link_26 View Post
    Your program is perfect in every way.
    That can only have been sarcasm, meant to imply the opposite.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A rather poor sarcasm, though, since it doesn't even say what's wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    but ELYSIA how can you completely eliminate scanf from your program when getchar() is extremely cumbersome to use..
    so it would be better to use fflush to flush out the buffer before using getchar() or any other related input function
    isnt it??????

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Quote Originally Posted by ElemenT.usha View Post
    but ELYSIA how can you completely eliminate scanf from your program when getchar() is extremely cumbersome to use..
    Quote Originally Posted by Salem View Post
    Several FAQs deal with reading input. Concentrate on the ones which read input using fgets().
    --------------------------------

    Quote Originally Posted by ElemenT.usha View Post
    so it would be better to use fflush to flush out the buffer before using getchar()
    lulz.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use fgets. No messy input buffer. Reads everything and doesn't leave anything dangling in the input. Read FAQ.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    Quote Originally Posted by zx-1 View Post
    --------------------------------


    lulz.
    so i dint get you my friend... what was lolz about??????

  15. #15
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    The implication of using fflush() on the stdin stream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  4. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM