I have problem in this c program

#include <stdio.h>

int main()
{
  int this_is_a_number;

  printf( "Please enter a number: " );
  scanf( "%d", &this_is_a_number );
  printf( "You entered %d", this_is_a_number );
  getchar();
  return 0;
}

when i press ctrl+F9 the output screen appears
" Please enter a number:"
and when i enter the number(5) i press the enter key
the program is redirected to the compiler again.
When i run it second time then i can see the output
"you entered 5";

Recommended Answers

All 27 Replies

That sounds like the focus of your windows is changing. Is Ctrl+F9 running the debugger? What IDE are you using?

That sounds like the focus of your windows is changing. Is Ctrl+F9 running the debugger? What IDE are you using?

i am using Borlands turbo C++ compiler 3.0

What if you put two getchar calls instead of one?

Make your output screen a tiled window, before you start the program. If you need to see more of it, use your mouse, only.

Obviously, you can't leave keystrokes in the input buffer, and not have it affect your program, which relies on the keyboard input buffer.

How about if you finish your output with "\n"?

How about if you finish your output with "\n"?

NO its not working i tried....

What if you put two getchar calls instead of one?

ya it works...but after the second getchar the code exits

Why you have use getchar() ?

The getchar() removes one char from the input stream - in this case, the newline char, left whenever you use scanf().

When things are going right, it may not be a problem, but when they don't, you may need to remove unwanted content from the input stream.

Here's a very small example of how bad input stream can goof up a program badly:

#include <stdio.h>

int main(void) {
   int num=1;

   while(num) {
      scanf("%d", &num);
      printf("Num = %d  Enter 0 to quit. \n", num);
   }

   return 0;
}

Looks innocent enough, doesn't it? Run it through once and enter integers to see that it works, as it should.

Now, run it again, but enter a letter, instead of a number, and watch the chaos!

Lastly, put this line of code, anywhere in the loop:
getchar();

recompile the program, and run it again. ;)

The difference is like night and day, don't you agree?

This is just one of the reasons why scanf() is for input of formatted code, only (generally), and by "trusted user's". General user's should have their input taken as a string ( fgets() ), and be examined carefully before accepting it as legit input to your program.

if u knw about the concept of buffer then easily u can this problem urself........

i am using Borlands turbo C++ compiler 3.0

That came out in 1991!

Add the header file <conio.h> and put getch() at the end of main() function before return statement and see if it works.

Add the header file <conio.h> and put getch() at the end of main() function before return statement and see if it works.

conio.h is pure evil!

It not portable (only works properly under DOS) and isnt standards compliant either

conio.h is pure evil!

It not portable (only works properly under DOS) and isnt standards compliant either

While I agree with you about both <conio.h> and the Turbo C++ compiler, it is my understanding that in both India and Pakistan, the university systems have standardized on Turbo C++ and generally teach DOS style console I/O as part of their coursework. The OP may not have any choice in the matter.

While I agree with you about both <conio.h> and the Turbo C++ compiler, it is my understanding that in both India and Pakistan, the university systems have standardized on Turbo C++ and generally teach DOS style console I/O as part of their coursework. The OP may not have any choice in the matter.

He has the choice to go to a real university where they teach you relevant technology........

He has the choice to go to a real university where they teach you relevant technology........

In that case, he'd be studying HTML and either Python or Ruby right now, and we wouldn't be having this conversation (or at least not for another few semesters). While I have used C++ professionally myself, I'd expect that for the current classes of graduates, C++ jobs are the exception rather than the rule.

Seriously, though, when I say 'standardized', I mean it. Every accredited university in those countries is required to teach the same courses, in the same manner, using the same tools, no exceptions. It's a sad situation, but chances are, the OP doesn't have the choice of going to what you would call a 'real university'.

He has the choice to go to a real university where they teach you relevant technology........

In that case, he'd be studying HTML and either Python or Ruby right now, and we wouldn't be having this conversation (or at least not for another few semesters). While I have used C++ professionally myself, I'd expect that for the current classes of graduates, C++ jobs are the exception rather than the rule.

Really? You mean universities no longer teach C++ as a relevant technology? HTML is their relevant technology?

So sad... In that case, go into sports instead of computers. Hopefully they teach students how to run and catch properly.

Really? You mean universities no longer teach C++ as a relevant technology? HTML is their relevant technology?

Well, not as their first-semester technology, in most cases, at least, a policy I agree with wholeheartedly - C++ is hardly suited for beginners. Unfortunately, many are dropping C and C++ entirely, which is short-sighted to say the least.

OTOH, it's true that the majority of CS students are likely to do more web-related work than systems or applications programming, so yes, the relevance for most is questionable. Whether we should be basing our curriculum entirely on what students are likely to need for their jobs is another question altogether.

I'd expect that for the current classes of graduates, C++ jobs are the exception rather than the rule.

My wwell paid (recently graduated) job is writing C++ applications for embedded linux devices. My web-developmnent friend is currently unemployed.....

My wwell paid (recently graduated) job is writing C++ applications for embedded linux devices. My web-developmnent friend is currently unemployed.....

If you were in India or Pakistan, your statement would be relevant. As it is, your statement is irrelevant... :icon_wink:

I see that it was a mistake to make that off-hand comment about relevancy; but no matter. Let's return to the issue at hand, shall we?

Regarding the buffering issue, I agree with Adak about using fgets() and strtol() rather than scanf() for numeric input. Being able to vet input is important, if only so you can handle user mistakes (not to mention malicious users).

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#define MAXBUF 256

int main(void)
{
    char buffer[MAXBUF], *endpt;
    long num = 1;
    int len = 0;

    for( ; ; )
    {
        printf("Num = %ld Enter X to quit. \n", num);
        endpt = fgets(buffer, MAXBUF - 1, stdin);

        if (endpt == NULL)
        {
            puts("An I/O error has occurred. Exiting.");
            exit(-1);
        }

        buffer[MAXBUF] = '\0';        /* make certain that the str is delimited */
        len = strlen(buffer);
        buffer[len - 1] = '\0';       /* eliminated the newline */

        num = strtol(buffer, &endpt, 10);

        if (toupper(buffer[0]) == 'X')
        {
            break;
        }
        else if (endpt == buffer)
        {
            printf("%s is not a valid integer.\n", buffer);
            num = 1;
            continue;
        }

    }

    return 0;
}

Not so simple a program after that, admittedly, but definitely more robust.

I have problem in this c program

#include <stdio.h>

int main()
{
  int this_is_a_number;

  printf( "Please enter a number: " );
  scanf( "%d", &this_is_a_number );
  printf( "You entered %d", this_is_a_number );
  getchar();
  return 0;
}

when i press ctrl+F9 the output screen appears
" Please enter a number:"
and when i enter the number(5) i press the enter key
the program is redirected to the compiler again.
When i run it second time then i can see the output
"you entered 5";

just re write like this

#include <stdio.h>
#include<conio.h>
int main()
{
  int this_is_a_number;

  printf( "Please enter a number: " );
  scanf( "%d", &this_is_a_number );
  printf( "You entered %d", this_is_a_number );
  getch();
  return 0;
#include <stdio.h>
#include<conio.h>
int main()
{
  int this_is_a_number;

  printf( "Please enter a number: " );
  scanf( "%d", &this_is_a_number );
  printf( "You entered %d", this_is_a_number );
  getch();
  return 0;

No no no, silly. It should be like this:

#include<stdio.h>
#include<conio.h>

void main()
{
    clrscr();
    int this_is_number;
    printf("enter number");
    scanf("%d", this_is_number);
    printf("you entereded %d", this_is_number);
    getch();
}

You can't be a proper conio noob without also voiding your main(), calling clrscr() unnecessarily at the beginning of every program, compiling as C++ without knowing it, completely ignoring user friendly output, and habitually misusing scanf() (with the claim "it works for me" when challenged). You get bonus points for placing a newline at the beginning of printf() format strings instead of the end, but that's not a requirement.

commented: narue you are a legend +15
commented: That'll teach 'em!!! +17
commented: Take care, goddess, not to become fat, eating newbies for breakfast ;) +13

hey its nothing but you put getchar() instead of getch().there is a diff b/w getchar() and getch().

hey its nothing but you put getchar() instead of getch().there is a diff b/w getchar() and getch().

Yes, and the difference is that getch() is wrong. The whole concept of keeping the window open with interactive input is wrong, in my opinion, but getchar() is a more portable way to do it than getch().

Yes, and the difference is that getch() is wrong. The whole concept of keeping the window open with interactive input is wrong, in my opinion, but getchar() is a more portable way to do it than getch().

Well its working with getch()...

commented: Another "it works for me therefore it must be right" response. Try it with a REAL compiler. -4

Well its working with getch()...

"It works for me" is not synonymous with "it's correct".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.