C - equivalent of .NET Console.ReadLine
You could use fgets(), like so:
#include <stdio.h>
fgets(buf, sizeof(buf), stdin);
equivalent of Console.ReadLine() in c++
You are looking for std::getline()
. For example:
#include <string>
std::string str;
std::getline(std::cin, str);
I've little idea what you mean when you say I must also be able to store the value through a pointer.
Update: Looking at your updated question, I can imagine what is happening. The code that reads the choice, i.e. the number 1, 2, etc. is not reading the newline. Then you call getline
which consumes the newline. And then you call getline
again which fetches the string.
How to read a line from the console in C?
You need dynamic memory management, and use the fgets
function to read your line. However, there seems to be no way to see how many characters it read. So you use fgetc:
char * getline(void) {
char * line = malloc(100), * linep = line;
size_t lenmax = 100, len = lenmax;
int c;
if(line == NULL)
return NULL;
for(;;) {
c = fgetc(stdin);
if(c == EOF)
break;
if(--len == 0) {
len = lenmax;
char * linen = realloc(linep, lenmax *= 2);
if(linen == NULL) {
free(linep);
return NULL;
}
line = linen + (line - linep);
linep = linen;
}
if((*line++ = c) == '\n')
break;
}
*line = '\0';
return linep;
}
Note: Never use gets ! It does not do bounds checking and can overflow your buffer
Alternative to Console.ReadLine() to keep the Console visible
If you are still developing application you can run via Ctrl + F5 (Without debugging)
otherwise you can use Console.ReadKey() (same but there is no more option)
What is the difference between read and readline in C#?
Do you mean TextReader.Read
and TextReader.ReadLine
?
One overload of TextReader.Read
reads characters into a buffer (a char[]
), and you can specify how many characters you want it to read (as a maximum). Another reads a single character, returning an int
which will be -1 if you've reached the end of the reader.
TextReader.ReadLine
reads a whole line as a string
, which doesn't include the line terminator.
As far as I'm aware, endl
is more commonly used in conjunction with cout
in C++:
cout << "Here's a line" << endl;
In .NET you'd use
writer.WriteLine("Here's a line")
to accomplish the same thing (for an appropriate TextWriter
; alternatively use Console.WriteLine
for the console).
EDIT: Console.ReadLine
reads a line of text, whereas Console.Read
reads a single character (it's like the parameterless overload of TextWriter.Read
).
Console.ReadLine()
is basically the same as Console.In.ReadLine()
and Console.Read()
is basically the same as Console.In.Read()
.
EDIT: In answer to your comment to the other answer, you can't do:
int x = Console.ReadLine();
because the return type of Console.ReadLine()
is a string, and there's no conversion from string
to int
. You can do
int x = Console.Read();
because Console.Read()
returns an int
. (Again, it's the Unicode code point or -1 for "end of data".)
EDIT: If you want to read an integer from the keyboard, i.e. the user types in "15" and you want to retrieve that as an integer, you should use something like:
string line = Console.ReadLine();
int value;
if (int.TryParse(line, out value))
{
Console.WriteLine("Successfully parsed value: {0}", value);
}
else
{
Console.WriteLine("Invalid number - try again!");
}
C#. Console.ReadLine() for two different obejcts
Console.ReadLine()
does not return any "reader". It just returns String
object which contains text grabbed from console input.
The line
in method name means it will read everything available on the input until it will encounter line break.
If there is no line break, it will wait forever until user will press "enter" key.
If there are many text lines in the bufffer, you need to call ReadLine()
many times.
You can assign player name diretly using:
player1.Name = Console.ReadLine();
Related Topics
Undefined Symbols for Architecture X86_64: Which Architecture Should I Use
Are Data Members Allocated in the Same Memory Space as Their Objects in C++
How to Show Qsqlquerymodel in Qml
Get Private Data Members for Non Intrusive Boost Serialization C++
Preventing Gcc from Automatically Using Avx and Fma Instructions When Compiled with -Mavx and -Mfma
How Is Tr1::Reference_Wrapper Useful
Extract C++ Template Parameters
Why Does My Stl Code Run So Slowly When I Have the Debugger/Ide Attached
How to Detect Text Area from Image
How to Assign a Value to the Pointer 'This' in C++
Hide a C++ Code Block from Intellisense
Multiple "Could Not Be Resolved" Problems Using Eclipse with Mingw
Difference Between Console (/Subsystem:Console) and Windows (/Subsystem:Windows)