Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8359859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:17:15+00:00 2026-06-09T11:17:15+00:00

Possible Duplicate: Why doesn't getchar() wait for me to press enter? I continue to

  • 0

Possible Duplicate:
Why doesn't getchar() wait for me to press enter?

I continue to learn C, and at this stage I have something that is not clear to me.
When I write a program, that has several printf();, at the end, where I ask a user to press Enter key to finish the program, I have to write getchar(); twice, because when I write it once it does not work. I use getchar(); only at the end of the program, nowhere else.

I work on Ubuntu. I write in C.

here is my latest work:

#include<stdio.h>
main()
{
int m,n,r,k,q,l;
printf("This program will help you to find GCD & LCM of 2 non-negative integers\n");
printf("Now, you'll be asked to enter your integers, press Enter to continue");
getchar();
printf("Enter first integer:");
scanf("%i", &m);
printf("Enter second integer:");
scanf("%i", &n);
while(m<0 || n<0)
{
printf("The integers cannot be negative! You'll be asked to enter integers again.\n");
printf("Enter first integer:");
scanf("%i", &m);
printf("Enter second integer:");
scanf("%i", &n);
}
while(m==0 && n==0)
{
printf("Both of the integers cannot be zero at the same time! You'll be asked to enter integers again.\n");
printf("Enter first integer:");
scanf("%i", &m);
printf("Enter second integer:");
scanf("%i", &n);
}
if(n>m)
{
int b;
b=n;
n=m;
m=b;
}
r=m%n;
if(r==0)
{
printf("The GCD of these integers is %i\n", n);
printf("The LCM of these integers is %i\n", m);
printf("Press Enter to finish");
getchar();
getchar();
return 0;
}
k=n%r;
while(k>0)
{
r=k;
k=q;
}
l=(m*n)/r;
printf("The GCD of these integers is %i\n", r);
printf("The LCM of these integers is %i\n", l);
printf("Press Enter to finish");
getchar();
getchar();
return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:17:16+00:00Added an answer on June 9, 2026 at 11:17 am

    The reason you need 2 getchar() is because

    1. the last scanf() call left the ENTER waiting in the buffer
    2. the 1st getchar() “ate” that ENTER
    3. the 2nd getchar() waits for input.

    To properly deal with user input, use fgets() and sscanf() instaed of the simpler scanf(). Define a buffer for these functions, for example

    char buffer[1000];
    

    and then replace your scanf() calls with the pair

    fgets(buffer, sizeof buffer, stdin);
    sscanf(buffer, "%d", &n);
    

    In the future you might want to also check the return value of sscanf() to detect invalid inputs, like foo42ENTER

    if (sscanf(buffer, "%d", &n) != 1) /* invalid input */;
    

    Edit (using strtol() rather than sscanf() is even better — thanks to @Scooter)

    char buffer[1000];
    char *err;
    /* ... */
    fgets(buffer, sizeof buffer, stdin); /* error checking ommited */
    n = strtol(buffer, &err, 10);
    /* error checking ommited */
    /* ... */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why doesn't this closure have access to the 'this' keyword? - jQuery
Possible Duplicate: Why doesn't this code produce the desired result? I have the code:
Possible Duplicate: NSTimer doesn't stop I have this code: [NSTimer scheduledTimerWithTimeInterval:110.0 target:self selector:@selector(targetMethod:) userInfo:nil
Possible Duplicate: .Net Console Application that Doesn’t Bring up a Console I have a
Possible Duplicate: What does mysql_real_escape_string() do that addslashes() doesn't? I have been reviewing articles
Possible Duplicate: Why doesn't this division work in python? A simple problem I'm having
Possible Duplicate: getElementByClass().setAttribute doesn't work Why this: document.getElementsByClassName('cke_source').setAttribute('name', mymessage) Is returning: TypeError: Object #<NodeList>
Possible Duplicate: $(this) doesn't work in a function Im having a problem targeting the
Possible Duplicate: Most vexing parse: why doesn't A a(()); work? I am having this
Possible Duplicate: problem with template inheritance This code doesn't compile in GCC: template <typename

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.