K
K
Karapolo2021-05-11 12:18:11
C++ / C#
Karapolo, 2021-05-11 12:18:11

How to enter an indefinite number of lines in C++?

Hello. I’ll make a reservation right away, I just started studying strings, I don’t know vectors and much more ... You need to enter an indefinite number of strings. The problem is that I don't know how to do it. My knowledge is clearly not enough for this. Please, help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Ukolov, 2021-05-11
@Karapolo

Such a condition means that you need to work with the string character by character, and not write it all into memory, because there may not be enough memory.

1
12rbah, 2021-05-11
@12rbah

Write the problem in full, because it is not clear what you need, but as an option to solve such tasks through a buffer. Conventionally, you copy a 50GB file, but instead of reading 50GB into memory, you read 1MB and remember the position until the copy is over.

W
Wataru, 2021-05-11
@wataru

If this is an arbitrary length in the sense of not "you need to count exactly 5 characters", then you can read in std::string. It will allocate enough memory on its own.
This is how one word is read until a space (or end of file):

std::string s;
std::cin >> s;

This is how the whole line is read until the line break (or the end of the file): Then you can go through string as if it were an array, from 0 to s.length()-1.
std::getline(cin, s);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question