Like Tree1Likes
  • 1 Post By quzah

Can we make a variable return a NULL value when we hit enter?

This is a discussion on Can we make a variable return a NULL value when we hit enter? within the C Programming forums, part of the General Programming Boards category; Can we make a variable return a NULL value when we hit enter, so we can proceed the code's execution ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    61

    Can we make a variable return a NULL value when we hit enter?

    Can we make a variable return a NULL value when we hit enter, so we can proceed the code's execution after enter?

    Code:
    int dummy = 0;
    
    // some code here;
    
    scanf("%d",dummy); //wait till the user hit enter;
    
    // some code here;
    Thank You In Advance

  2. #2
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    978
    just use getchar()

  3. #3
    Registered User
    Join Date
    Dec 2010
    Location
    Lucknow, India
    Posts
    72
    Use getch() or getchar() :-)

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    i have tried this but didn't work:

    Code:
    // some code here;
    
    getchar();  //wait till the user hit enter;
    
    // some code here;
    Last edited by Laythe; 04-03-2011 at 12:08 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    What are you trying to do?
    And 'didn't work' don't tell us much.

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by Laythe View Post
    i have tried this but didn't work:

    Code:
    // some code here;
    
    getchar();  //wait till the user hit enter;
    
    // some code here;
    You're not gonna store anything if you call getchar like that. You have to assign a variable to it, like this.

    Code:
    int x = getchar();
    What are you trying to do, exactly?
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    what i am trying to do is this:

    // some code here

    // wait till the user hit enter without the need to input anything,
    // just a dummy variable to accept the NULL entered value

    // some code here

  8. #8
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by Laythe View Post
    what i am trying to do is this:
    You don't need a variable, then. I don't see how you couldn't do that with a simple getchar call.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    here is my code:

    Code:
    #include <stdio.h>
    
    float FahrenheitToCelsius(float x);
    float CelsiusToFahrenheit(float x);
    
    int main()
    {
        float x;
        int   Choise;
    
    start:
        x = Choise = 0;
    
        printf("Chose 1 Or 2 To Convert The Temperature:\n");
        printf("1. FahrenheitToCelsius\n");
        printf("2. CelsiusToFahrenheit\n");
        printf("Choise: ");
        scanf("%d", &Choise);
    
        if(Choise == 1)
        {
            printf("\nPlease Enter A Temperature In Fahrenheit: ");
            scanf("%f", &x);
            printf("The Entered    Temperature In Fahrenheit Is: %f\n", x);
            printf("The Equivalent Temperature In Celsius    Is: %f\n", FahrenheitToCelsius(x));
        }
        else
            if(Choise == 2)
            {
                printf("\nPlease Enter A Temperature In Celsius: ");
                scanf("%f", &x);
                printf("The Entered    Temperature In Celsius    Is: %f\n", x);
                printf("The Equivalent Temperature In Fahrenheit Is: %f\n", CelsiusToFahrenheit(x));
            }
    
        getchar();  // this getchar() is not doing the job in this case;
        goto start;
    }
    
    float FahrenheitToCelsius(float x)
    {
        float C;
        float F;
    
        C = F = 0;
    
        F = x;
    
        C = (5.0/9.0)*(F-32.0);
    
        return C;
    }
    
    float CelsiusToFahrenheit(float x)
    {
        float C;
        float F;
    
        C = F = 0;
    
        C = x;
    
        C = (5.0/9.0)*(F-32.0);
    
        F = ((9.0/5.0)*C)+32.0;
    
        return F;
    }
    might you please tell me why getchar() is not working as you said it should solve the problem, thank you.
    Last edited by Laythe; 04-03-2011 at 08:05 PM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always just read the FAQ on: Cprogramming.com FAQ > How do I get my program to wait for a keypress?


    Quzah.
    Laythe likes this.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 07:00 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 11:50 AM
  4. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 04:43 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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