simple fortran program to print back what it reads [duplicate] - fortran

This question already has answers here:
Using a deferred-length character string to read user input
(4 answers)
Reading a string with spaces in Fortran
(2 answers)
Reading a character string of unknown length
(4 answers)
How to read text files with possibly very long lines in Fortran
(2 answers)
Closed last month.
It's a very simple task, but there are two problems with my current implementation.
I want it to read a whole line. It currently only reads the first few characters if they are separated by space. I think this can be fixed with a custom format, but I don't know what to use.
Fortran needs to know how much to allocate and with my current implementation it can only read 10 characters. I want it to read without a limit. The only way that I know that can fix this right now is asking the user, how many characters they want to be read, but other programming languages can do this without it and I don't want to ask the user for additional things.
program main
implicit none
character(:), allocatable :: name
allocate(character(10) :: name)
read *, name
print *, name
deallocate(name)
end program main

Related

Reading Line by Line in C++ for Pairs [duplicate]

This question already has answers here:
reading text with whitespaces in c++
(2 answers)
How to read the whole lines from a file (with spaces)?
(4 answers)
Closed 4 months ago.
I have an input file that looks something like this:
0.1 239.402
0.4. 32342.34
0.7 4.924939
and so on...
I am trying to read it line by line using the fstream extraction operator >>
I need a way to set a while loop condition that would stop at the end of the file, and I need a way to push the two as a pair onto two stacks
The stacks are previously implemented in a class file Stack as linked lists
I currently have something like
double a, double b
std::string t;
Stack av;
Stack bv;
while(i>>a>>t>>b&&(t=="\t"){
av.push(a);
bv.push(b);
}
i know i'm incorrect as it's not always a tab space gap. also the file starts with a couple of spaces until the first row and then has a random number of spaces until the second row, and then a next line.
Is there a way to write a program where it knows when there is a double to read and use the random number of whitespaces as separation between a and b?
I hope this makes sense! Thanks so much for your help as I'm a beginner.

Fortran: user defined integer in file name of "open" statement [duplicate]

This question already has answers here:
Writing multiple output files in Fortran
(2 answers)
Closed 8 years ago.
I want to open a file in fortran with file name "controlinputs.12.dat" and then write into that file. The digit "12" is user defined variable whose value is stored in another variable "k". I have tried following and failed.
k=12
open(10,filename='controlinputs.',k,'.dat')
Tried storing the name in character and then using character to open file.
k=12
fname='controlinputs.',k,'.dat'
open(10,filaname=fname)
This was very simple. I didn't knew how to concatenate characters. I was reading the integer 12 from a file so I saved it in character type then simply used following.
character::k*5
open(10,filename='controlinputs.'//trim(k)//'.dat')

How to validate an integer properly in C++? [duplicate]

This question already has answers here:
How to determine if a string is a number with C++?
(36 answers)
How to convert a command-line argument to int?
(8 answers)
Closed 9 years ago.
I want to validate the input by user to make sure it's an integer (fully). I've tried a few methods, like !cin, but none of them work properly..
Most of methods fail to validate input like this:
32tgf
When there is a number first and then letters, it doesn't fail, but it takes it as valid entry..
Note: It's a project for college and it's specified that the variable should be of type int.
Read into a string, then use e.g. std::stoi or std::strtol to both convert to an integer and validate the input.
Or read into a string, put that string in a std::istringstream which you use to extract the integer. Then check if there's anything more in the istringstream.
I'd recommend the first method though.

How does C++ understand two letter characters [duplicate]

This question already has answers here:
Multicharacter literal in C and C++
(6 answers)
What do single quotes do in C++ when used on multiple characters?
(5 answers)
Closed 9 years ago.
C++ seems to allow up to four characters to be held in single quotes, such as:
char c = 'abcd';
but at runtime, only the last value ('d') seems to be actually stored away. This behavior seems to happen for pairs of two, three, or four (at five the compiler finally calls uncle). But what's the deal with this design? I don't really see the logic in it.

DATA declaration in Fortran [duplicate]

This question already has an answer here:
what does DATA TKX/2HKX/ mean in fortran?
(1 answer)
Closed 1 year ago.
Does anyone know the meaning of 4HEND in the following line which comes from an old Fortran code?
DATA XHEND / 4HEND /
4HEND is a Hollerith code to represent the character string "END ". In very old FORTRAN, the variable XHEND might even be a 4-byte integer or real used to hold character data. If implicit typing was in effect in this program, XHEND would have been a real. Another recent question with Hollerith codes: Writing both characters and digits in an array
It replaces assignment
XHEND='END '