I am experiencing a problem with turbo c while I execute odd loops. The compiler doesnt wait till I give a yes or a no. It just jumps out of the loop. Can anyone help me?Is it a problem with the compiler or my program. The latter is not possible since I tried out a similar program from a book and got similar results. I have Turbo C v3.0 downloaded from the internet.Also, the compiler doesn't seem to understand getch and clrscr functions.
Program was;

#include<stdio.h>
void main()
{
       int i;
       char q='y';
       while(q=='y')
       {
             printf("Enter the number");
             scanf("%d",&i);
             printf("The number you have entered is    %d",i);             
             printf("Want to enter another number y/n\n")
             scanf("%c",&q);
        }
}

Recommended Answers

All 6 Replies

Is there any particular reason why you want to use such compiler?

Explicit facts

No I am a beginner to c and it was suggested that I use this one.

>Can anyone help me?
Post your code, or a suitable facsimile of your code that exhibits the problem.

>Is it a problem with the compiler or my program. The latter is not possible
>since I tried out a similar program from a book and got similar results.

It's not possible that you wrote bad code? It has to be a problem with the compiler? Keep in mind that Turbo C was a mature environment in its day, and a bug in simple loop execution isn't exactly one of those deep dark corners where I'd expect new bugs to be found by beginners in a popular and well tested compiler.

Oh, I see you edited your post with some code. The problem is that the second scanf is reading a newline character left over from the previous call to scanf. 'y' != '\n', so the loop terminates. This is a classic problem with stream input in C. Search the forum for flushing stdin and you'll find plenty of discussions on the topic.

No I am a beginner to c and it was suggested that I use this one.

Thought so.
Stay away from whatever source "suggested" it.
There are a few good recommendations for compilers (free of charge) in that "facts" link I posted previously.

Ok, I,ll try and see what is wrong. Anyway, thanks for the help.

Ok, I,ll try and see what is wrong. Anyway, thanks for the help.

In that particular piece you posted, what's happening is that the ENTER key is been used as part of the answer for the last statement scanf("%c",&q);

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.