Equivalent of Console.Readline() in C++

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



Leave a reply



Submit