problems with prototype function looping

This is a discussion on problems with prototype function looping within the C Programming forums, part of the General Programming Boards category; hey guys im learning to make my own functions and iv had a little trouble it seems my function is ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    53

    Unhappy problems with prototype function looping

    hey guys im learning to make my own functions and iv had a little trouble it seems my function is returning what i want but for some reason when i put it in a do while loop it just keeps looping instead of exiting when i want, you should be able to see what i mean from this sample text. Thanks in advance much appreciated the people on this site are so helpful.

    Code:
    #include <stdio.h>
    
    const int TRUE = 1;
    const int FALSE = 0;
    
    int isY(char ch);
    
    int main()
    {
    
    	int x = 1;
    	int choice;
    	
    	while (x = 1)
    	{
    		choice = isY(choice);
    		if (choice == FALSE)
    		x++;
    	}
    	return 0;
    }
    
    int isY(char ch)
    {
    	int result;
    	printf("Say this massage again\n(Y/N)? ");
    	scanf("%c", &ch);
    	fflush(stdin);
    	printf("\n");
    	if ( (ch == 'y') || (ch == 'Y') )
    		result = TRUE;
    	else
    		result = FALSE;
    		
    	return result;
    }
    for some reason it just keeps looping even if something other than y or Y is entered.

    p.s. fflush(stdin) works fine on my compiler (quincy 2005) and i have not yet been tought a better way to flush the standard input thing, but that should not be the problem here. thanks again

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    you should use break wihin your if block of while loop.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dezz101 View Post
    p.s. fflush(stdin) works fine on my compiler (quincy 2005) and i have not yet been tought a better way to flush the standard input thing, but that should not be the problem here. thanks again
    Yes, and if you change to another compiler it may not - the point about "don't use fflush(stdin)" is not that it doesn't work when the C library supports it, but rather that it's a bad habit to use it, because it's by no means guaranteed to work (and it may even crash in some C library implementations).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    A better way to flush the input buffer, here you go!
    http://cpwiki.sourceforge.net/Common...flush_stdin.21
    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.

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Write your own:
    Code:
    void flush(FILE *in)
    {
        int ch;
        while ((ch=fgetc(in)) != EOF)
        {
            if (ch == '\n') break;
        }
    }

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    Code:
    while (x = 1)
    You probably meant to say:

    Code:
    while (x == 1)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  5. Big problems with the Text function
    By GaPe in forum C Programming
    Replies: 26
    Last Post: 05-22-2002, 10:42 AM

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