Thread: help!! i have the wierdest problem??

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    7

    help!! i have the wierdest problem??

    okay so I write to see programs in an IDE compiler, the programs run fine through the IDE, but when I run them on and other computer, the program closes before the final output is shown. Now see the weird thing is I had getchar and return in their, and it kept on doing the same thing; it never stopped for more input. If the only way I was able to correct this is with fflush (stdin), but I have never had to use this before. At first I thought maybe it was some type of corrupt or damaged development environment, so I uninstalled and deleted everything; and then reinstall it all fresh and stuff. Has anyone else come across this problem? Is there another solution besides flush?


    here is my code...but it does it with everything, even hello world!


    Code:
       // this is where i come to test ........, by moi!!
    
    #include <stdio.h>
    
    main()
    
    {
        //Defining response
            int iResponse = 0;
    
            //Displaying options for controler erotica
    
        printf("\n\tFemale Erotic Nerve Control Center\n");
        printf("\nCell 1\tTurn Orgasims On\n");
        printf("\nCell 2\tTurn Orgasims Off\n");
    
            //tell me what to do
           printf("\nEnter Neuron Cell: ");
           scanf("%d", &iResponse);
    
           //What will happen
           if (iResponse == 1)
            printf("\nErotic Sensations Now On,\n");
    
           else if (iResponse == 2)
            printf("\nErotic Sensations Now Off\n");
    
           printf("\nexit now!\n");
    
            fflush (stdin);
            
           getchar();
    
           return 0;
    
    
    }

  2. #2
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    You return 0 but your main is void.

    void main is the death coming closer time to time.

    Better int main(void)
    Last edited by ch4; 12-17-2008 at 11:49 AM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    My answer is not as dramatic:

    Whenever you have a scanf(), it leaves behind the \n (newline) char at the end, on the keyboard buffer.

    So when your code gets down to getchar(), well, it does get a char alright - it gets that newline char, right off the keyboard buffer.

    fflush(stdin), is one way to (maybe) get rid of the newline char on the input stream, but you shouldn't be flushing input streams, only output streams. It's undefined.

    Answer that is defined is to add a getchar(), right after the scanf(), to pull the newline char off, and then everything will work right. Get rid of the fflush(stdin), of course.

  4. #4
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Quote Originally Posted by Adak View Post
    My answer is not as dramatic:
    Not like death but like a flower.
    If you try to smell it you may sneeze but you may don't.
    Different people have different behavior (sometimes like computers).

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    7
    omfg you are my hero Adak....i love you!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM