FORTRAN Read() issue - fortran

Hey so I am trying to learn Fortran basics so that I can use it for a basic physics project. I am having trouble with getting input properly. My code is:
program main
write(*, *) "Enter n:"
read(*, *) n
print *, "Number is ", n
end program main
It is quite simple. Except that when I compile and run it, nothing happens until I enter the input in which it looks like this
gfortran num.f90 -o num
./num
(nothing happens until I type) 3
Enter n:
Number is: 3
Is there a problem with my memory allocation? Or could it be my compiler? I honestly have no clue.

Your program is fine, on my machine it prints the Enter n: before reading the variable. If you do not see the message until you enter a value (and hit Enter), it could have to do with the buffering of your command window you use. For checking this, you could just open an xterminal (type xterm in your command window), and run the program there.

Related

Compiled c++ output file displays random character at end of program?

Not sure if this is an appropriate question, but just recently I've noticed that when I run a C++ program in the terminal when it exits it has a % sign after the last output. For example a hello world program says "hello world%". What is this and how do I get rid of it? I'm on OS X, shell is zsh. Unless I am crazy it has never done this until now.
There are two possibilities that I can think of off hand:
1) You aren't printing a carriage return, so the % prompt appears at the end of the printed text instead of on the next line. (Is the % your standard prompt in the shell?)
2) You are printing past the end of a buffer and getting a random character as a result.
I'd guess #1 based on what you describe, but both could cause the behavior.

Simple D program Output order is wrong

I am learning a new language called "D" but i have a problem when trying to write a simple program
import std.stdio;
void main()
{
double gradeOne;
writeln("Please enter the First Test Grade: ");
readf(" s", &gradeOne);
}
Why does my program ask me for the input first before the output message?
I think its just the DDT problem, when i run the program in command prompt its working fine
Output to Eclipse buffers output by larger data blocks rather than lines. To force output to appear, insert calls to stdout.flush(); before asking for input to ensure it shows up when you want it.
See also: Eclipse console writes output only after the program has finished

Automatic cout flushing

Good day,
I wrote a Java program that starts multiple C++ written programs using the Process object and Runtime.exec() function calls. The C++ programs use cout and cin for their input and output. The Java program sends information and reads information from the C++ programs input stream and outputstream.
I then have a string buffer that builds what a typical interaction of the program would look like by appending to the string buffer the input and output of the C++ program. The problem is that all the input calls get appended and then all the output calls get posted. For example, and instance of the StringBuffer might be something like this...
2
3
Please enter two numbers to add. Your result is 5
when the program would look like this on a standard console
Please enter two numbers to add. 2
3
Your result is 5
The problem is that I am getting the order of the input and output all out of wack because unless the C++ program calls the cout.flush() function, the output does not get written before the input is given.
Is there a way to automatically flush the buffer so the C++ program does not have to worry about calling cout.flush()? Similiar to as if the C++ program was a standalone program interacting with the command console, the programmer doesn't always need the cout.flush(), the command console still outputs the data before the input.
Thank you,
In case someone comes looking for a way to set cout to always flush. Which can be totally fair when doing some coredump investigation or the like.
Have a look to std::unitbuf.
std::cout << std::unitbuf;
At the beginning of the program.
It will flush at every insertion by default.
I can't guarantee that it will fix all of your problems, but to automatically flush the stream when you're couting you can use endl
e.g.:
cout << "Please enter two numbers to add: " << endl;
using "\n" doesn't flush the stream, like if you were doing:
cout << "Please enter two numbers to add:\n";
Keep in mind that using endl can be (relatively) slow if you're doing a lot of outputting
See this question for more info

How do I read an input in Fortran without interrupting the program?

An usual read statement in Fortran interrupts the execution of the program until the RETURN key was pressed. I am looking for a statement that reads any pressed key without waiting for the RETURN key. The program should not stop even if no key was pressed.
Thank you for your answer.
Edit:
Here is some source code that should clarify the question.
Program test1
n=2
do while (n==2)
read (*,*) n
write (*,*) 'Output'
end do
end program test1
Program test2
n=2
do while (n==2)
UnknownReadStatement (*,*) n
write (*,*) 'Output'
end do
end program test2
The program test1 will never write the word "Output" on the screen if no key is pressed.
Using the read statement I am looking for the program test2 should fill the screen with "Output" until a key different from "2" is pressed.
There is an example code for reading a single key from the terminal from Fortran without requiring that the input be terminated by a return key at http://home.comcast.net/~urbanjost/CLONE/GETKEY/getkey.html. I haven't tried this code, so can't vouch for it. His (John Ubran) solution mixes Fortran and C, using the C getkey. Assuming that your compiler supports it (most do), I suggest trying the ISO_C_BINDING method to combine the Fortran and C. This doesn't answer the part about the program proceeding even if no key is pressed -- for that you will have to add some sort of timeout, to give the person a chance to type something, but to timeout and proceed if they don't type by a deadline. Maybe you can modify the solution I linked to...

How do I input data using the eclipse console? (c++)

I'm trying my hand at c++ and am using fedora eclipse (3.4.2) as my IDE.
At the moment I'm trying to enter a string of numbers into the console, get the program to sort them and spit them back out. The program is straight out of a book and works with xcode and through a normal terminal - so I know it's correct.
Basically I run the program and enter a few numbers into the eclipse console, the numbers are coloured green so I know its accepting the input correctly.
When I hit enter, the console jumps to a new line and nothing happens. When I press control+shift+D, nothing happens. When I press control+d, nothing happens.
I use eclipse for python too, and the console works properly. Just hitting enter inputs data to the program.
Am I missing something here? I've spent the last half hour or so trying to figure this out. Can anyone help me? Thanks.
What version of ecplise and what complier are you using? The following worked for me on Eclipse Ganymede with GCC version 3.4.5:
#include <iostream>
using namespace std;
int main() {
int x = 0;
cout << "Type your input here:";
cin >> x ;
cout << "You entered " << x << endl;
return 0;
}
How does your program know that input has ended? It sounds like it accepts multiple lines of input in the console window. Isn't there some magic case that pops you out of that loop so you can process the input that's been collected?
As other said, without the code there's no answer.