Ahsaniqbalkmc

C Development With Eclipse Ide -- Problem With Scanf

Recommended Posts

For past few days, I have been trying to get a bit deeper into C development. Until now, I have been using Linux virtual machine to create and compile C programs. The process was simple and straight forward but one thing I hated was it had repetitions. Every time you write a new program you would have to manually go to the terminal and write commands to compile and run the program... If the program had some error, the repetitions could be as many as 20...

So I tried to look for something quicker and more helpful. There is a lot of hype of about eclipse so it was obviously my first choice. I gave it a try and I have to admit that setting it up (which should have been quite simple) wasn't that simple because i didn't have the JDK preinstalled on my system. After installation I had to separately install the C compiler (I used MinGW). Though the process was simple but it would have been much better if the IDE came ready with a compiler...

After setting everything up, I created a simple program to test how it worked. Everything was fine until I tested a program mentioned in this topic: http://forums.xisto.com/topic/52733-c-strange-behavior-with-numbers/
'>C -- Strange behavior with numbers
. The program looks like:

#include <stdio.h>int main(void) {printf("Program: Calculate how many digits in a number\n");int number;printf("Enter a number with maximum 4 digits: ");scanf("%d", &number);int digits;int error = 0;if (number >= 0 && number <=9) {digits = 1;}else if (number >= 10 && number <= 99) {digits = 2;}else if (number >= 100 && number <= 999) {digits = 3;}else if (number >= 1000 && number <= 9999) {digits = 4;}else {error = 1;}if (error == 0) {printf("Digits in number: %d\n", digits);} else {printf("Number has more than 4 digits....\n");}return 0;}

The problem is that the program doesn't print anything until I enter a value. After I enter the value the programs runs correctly and displays the correct result. The following is the output of the program:

02Program: Calculate how many digits in a numberEnter a number with maximum 4 digits: Digits in number: 1

When I run the program, there is a blank screen. I enter the value 02 and press enter and then the remaining two lines appear. The result is correct but the presentation is not..

I am assuming that some settings need to be changed to present the program correctly... Any help ???

Share this post


Link to post
Share on other sites

Have a look at your last printf

printf("Number has more than 4 digits....\n");


This printf ends with "\n", which means "carriage return, linefeed", which can be translated into "after that, next things will be written at the beginning of the next line".
Your first printf, after "int number", is

printf("Enter a number with maximum 4 digits: ");

In this statement, there is no final "\n", this means "please writte "4digits" and stay where you are, next printouts will stay where you are.

Share this post


Link to post
Share on other sites

I think I was unable to make you understand my problem @yordan. What you have said is absolutely right and and I understand it completely. Let me elaborate my problem a little more..

Suppose the name of the file is digitCalc.c

When I run the above program in Linux, I first compile is using the command "gcc -o digitCalc digitCalc.c" and then run it using the command "./digitCalc". I get the following output in the exact order as shown below....

Program: Calculate how many digits in a numberEnter a number with maximum 4 digits:
Then I enter the value 23 and press enter.... The total program output (including the above two lines) now become this:
Program: Calculate how many digits in a numberEnter a number with maximum 4 digits: 23Digits in number: 2
This is exactly how I want the program to behave but when I run the same program in Eclipse, the presentation is changed. Instead of first getting the output of first two printf statements, I get a blank screen in the console. There is nothing there. But when I enter the value 23 in that blank console space and press enter, I get the following output...
23Program: Calculate how many digits in a numberEnter a number with maximum 4 digits: Digits in number: 2

So... I want eclipse to present the program, in its console, in the same way as it is presented by in the linux terminal. There is nothing wrong with the program (because it runs absolutely correctly in the linux terminal). So, in short, my question is how do I make Eclipse present the program in its console in the same manner in which it is presented in the linux terminal....
Edited by Ahsaniqbal111

Share this post


Link to post
Share on other sites

Sorry, I was completely wrong.I did not understand that the C syntax was correct, and the c comiler version was correct, only the Eclipse version was wrong. So, the problem remains, and my explanation is not a real wone.

Share this post


Link to post
Share on other sites

So I googled the issue in hope of finding some help and it turns out that eclipse console has issues running in windows. The problem is posted on the eclipse community forum (here) but unfortunately there aren't any solutions to the issue that I can understand and follow...

It has been mentioned there that the linux version of eclipse doesn't have such a problem so I guess I better try out the linux version...

Share this post


Link to post
Share on other sites

Ahsan, Problem and its solution is explained by Axel Mueller on link provided by in your last post.

 

The Eclipse console has buffering problems on Windows. Basically, it doesn't flush the streams when a newline is received. (Unlike a normal windows console window)

Either, you have to add fflush calls when needed or add the following lines in the start of the main function:

setvbuf(stdout, NULL, _IONBF, 0);

setvbuf(stderr, NULL, _IONBF, 0);

 

But, I prefer you do not add fflush for each instances of printf function rather you can add setvbuf() at the start of your program's main function. Before executing your program clean project first as well, If you won't do that it will execute old compiled file.

 

Hope, this will resolve your issue. :)

Share this post


Link to post
Share on other sites

So to try the linux version of eclipse, I installed Linux Mint 14 cinnamon on my system (creating a dual boot setup with windows 7). And then then after updating the system I went on to install the Java (as a pre-requisite). I have to admit that for some reason I found it extremely extremely painful to install java. It wasn't as simple as running the command "sudo apt-get install sun-java6-jre" and I had to spend some time with google to find a way to install it. Forutnately, I found this page. It did take some time but eventually java did install successfully.
After that, installing eclipse was as easy as downloading and extracting. And to my pleasure, when I tested it with the same program as mentioned above in the topic, it worked like a charm.

So I confirm here that the linux version of eclipse doesn't have the same issue with scanf input as the windows version. And now I have found one more reason to stick to linux instead of windows... And by the way, Linux mint 14 cinnamon is so brilliant.. I really really like it.

Share this post


Link to post
Share on other sites

The problem you will have is that you have to learn how to create portable programs. Your program should work the same way on each hardware.So, if the windows implementation has a problem, you have to find a workaround for the Windows code, and this workaround has to work correctly on the linux system.Of course, this is the last step of your learning curve, but in a normal university cursus this step has to be constantly present in your mind from the very beginning : you have to learn how to write program which work correctly, with a code clearly commented, which can be applied to any environment.That is the main strongpoint of C/C++, the compilers are supposed to handle the same code in an identical way on each hardware environment. Too bad that Eclipse does not follow these rules.

Share this post


Link to post
Share on other sites

I also think that Eclipse being one of the most commonly used applications by developers should have taken special care wih the cross platform compatibility....But for now, I will carry on with my C learning and I hope I reach a stage where I am able to create cross-platform programs.

Share this post


Link to post
Share on other sites

Hi Ahsaniqbal111,I don't actually use Eclipse but I assume it gives you a lot of handy features that probably are useful for any programmer. Eclipse can not help you with cross platform compatibility. It does not even know what you are going to write your code for, you could be programming for a mobile device and that may not even be compatible with your computer.Cross platform compatibility is actually up to you and understanding how to write portable code. The stage to understanding this just means you'll continue testing your code on the platforms you want to support to find out incompatibilities, then you just work it out how you can get it working for that platform.The pain of installing Oracle java is understandable but what I don't understand is why Eclipse did not just work with openjdk, the java implementation that is usually shipped with most linux distributions. I will install Eclipse and see why you had to go through that pain. The implementation of openjdk is not too different from Oracle java, only that Oracle java has added additional features. In that case if Eclipse does rely on those features, that would be the only reason you would need to install it but you could probably get away with not using those features too.Cheers,MC

Share this post


Link to post
Share on other sites

Hi Ahsaniqbal111,I installed Eclipse version 4.2.x and have OpenJDK 1.7.0.x installed. From running Eclipse, it just works. I haven't tested much inside of the IDE but everything seems to work how it should be so I am uncertain why you had to go down the track of installing Oracle java.Cheers,MC

Share this post


Link to post
Share on other sites

Hi Ahsaniqbal111,
I don't actually use Eclipse but I assume it gives you a lot of handy features that probably are useful for any programmer. Eclipse can not help you with cross platform compatibility. It does not even know what you are going to write your code for, you could be programming for a mobile device and that may not even be compatible with your computer.

Cross platform compatibility is actually up to you and understanding how to write portable code. The stage to understanding this just means you'll continue testing your code on the platforms you want to support to find out incompatibilities, then you just work it out how you can get it working for that platform.

Thanks for providing your professional advice on the subject. I actually do understand that the burden of cross-platform compatibility lies on the shoulders of the coder. That is, the code should be written in such a way that the end result is a program that works in multiple environments.

What I wanted to say in my previous post was not that eclipse SHOULD help coders write cross-platform compatible code. It may have useful tools in this regard but the final responsibility is on the coder's part.

What I wanted to say was that eclipse being so commonly used development tool should ITSELF be cross-platform compatible. It is used in a variety of different operating systems and people who have developed this great tool should have given extra effort in making sure that it behaves predictably in different environments. It should not behave differently in windows and linux. For expert developers, this might not be very much of a problem but for learners and beginners it is quite a headache.

The pain of installing Oracle java is understandable but what I don't understand is why Eclipse did not just work with openjdk, the java implementation that is usually shipped with most linux distributions. I will install Eclipse and see why you had to go through that pain. The implementation of openjdk is not too different from Oracle java, only that Oracle java has added additional features. In that case if Eclipse does rely on those features, that would be the only reason you would need to install it but you could probably get away with not using those features too.

Well, I didn't know that eclipse can work on open jdk. I am new to linux as well as eclipse so I have to look for help prior to doing most of the things. And as you might know, installation (of any kind) is not understood by many people who are otherwise quite literate of the windows environment (as it is very different). So I looked for help online and every helping material and tutorial I found online had mentioned it clearly that I need to get java installed before running eclipse. That is why I had to go through the pain of installing java on linux. If I had known, the process would have been much simpler..

Share this post


Link to post
Share on other sites

tutorial I found online had mentioned it clearly that I need to get java installed before running eclipse.

That's the problem. You needed to get java installed. Any kind of java. So, expert linux people know that you should have looked if a java development kit is already installed, and would look of ipenjdk is installed. If not, installing the most famous java environment, which is the Oracle one, was not a bad idea. Simply, it's easy to install on some distros, and seems that on your computer this step was not very easy.

Share this post


Link to post
Share on other sites

Well, thanks to Xisto forums, I now know that java runtime environment doesn't exclusively mean oracle JRE. And I also know now that many linxu distros come pre-installed with openjdk java development, which can be used as an alternative to oracle JRE in most of the situations.

The installation of Oracle java was quite a pain for me because of the number of failures involved. The first tutorial I followed had stated that because of some issues with oracle, the repositories for java JRE have been removed from linux distros and to get java installed, first these repositories need to be added manually. In the process, I learned how to add a repository but unfortunately, I wasn't able to install java this way.

Then there were other tutorials with more or less a similar approach and all of them failed. Finally I found a tutorial where a pre-made script was posted. I didn't understand what that script did and how it did but when I ran it (plus a few other steps), I did get java installed. Here is the link: Techytalk. Maybe someone here can make me understand the process.

So there was a lot of time wasted and a lot of frustration involved in the process. That is why I say it was quite a pain for me.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now