logo

C++ getline()

Cin je objekt, který se používá k přijímání vstupu od uživatele, ale neumožňuje přijímat vstup ve více řádcích. Abychom přijali více řádků, použijeme funkci getline(). Je to předdefinovaná funkce definovaná v a hlavičkový soubor používaný k přijetí řádku nebo řetězce ze vstupního proudu, dokud nenarazí na oddělovací znak.

Syntaxe funkce getline():

Existují dva způsoby reprezentace funkce:

  • Prvním způsobem deklarace je předání tří parametrů.
 istream& getline( istream& is, string& str, char delim ); 

Výše uvedená syntaxe obsahuje tři parametry, tj. je, str , a sdílím .

Kde,

je: Je to objekt třídy istream, který definuje, odkud se má číst vstupní proud.

str: Je to objekt typu string, ve kterém je uložen řetězec.

zarovnat css obrázek
podíl: Je to vymezující znak.

Návratová hodnota

Tato funkce vrací objekt vstupního proudu, který je předán jako parametr funkci.

  • Druhým způsobem deklarace je předání dvou parametrů.
 istream& getline( istream& is, string& str ); 

Výše uvedená syntaxe obsahuje dva parametry, tj. je a str . Tato syntaxe je téměř podobná výše uvedené syntaxi; rozdíl je pouze v tom, že nemá žádný vymezující charakter.

Kde,

je: Je to objekt třídy istream, který definuje, odkud se má číst vstupní proud.

podřetězec v jazyce Java

str: Je to objekt typu string, ve kterém je uložen řetězec.

Návratová hodnota

Tato funkce také vrací vstupní proud, který je předán funkci jako parametr.

Pojďme to pochopit na příkladu.

Nejprve se podíváme na příklad, kde vezmeme uživatelský vstup bez použití funkce getline().

 #include #include using namespace std; int main() { string name; // variable declaration std::cout &lt;&lt; &apos;Enter your name :&apos; &lt;&gt;name; cout&lt;<'
hello '<<name; return 0; } < pre> <p>In the above code, we take the user input by using the statement <strong>cin&gt;&gt;name,</strong> i.e., we have not used the <strong>getline()</strong> function.</p> <p> <strong>Output</strong> </p> <pre> Enter your name : John Miller Hello John </pre> <p>In the above output, we gave the name &apos;John Miller&apos; as user input, but only &apos;John&apos; was displayed. Therefore, we conclude that cin does not consider the character when the space character is encountered.</p> <p> <strong>Let&apos;s resolve the above problem by using getline() function.</strong> </p> <pre> #include #include using namespace std; int main() { string name; // variable declaration. std::cout &lt;&lt; &apos;Enter your name :&apos; &lt;&lt; std::endl; getline(cin,name); // implementing a getline() function cout&lt;<'
hello '<<name; return 0;} < pre> <p>In the above code, we have used the <strong>getline()</strong> function to accept the character even when the space character is encountered.</p> <p> <strong>Output</strong> </p> <pre> Enter your name : John Miller Hello John Miller </pre> <p>In the above output, we can observe that both the words, i.e., John and Miller, are displayed, which means that the getline() function considers the character after the space character also.</p> <p> <strong>When we do not want to read the character after space then we use the following code:</strong> </p> <pre> #include #include using namespace std; int main() { string profile; // variable declaration std::cout &lt;&lt; &apos;Enter your profile :&apos; &lt;&lt; std::endl; getline(cin,profile,&apos; &apos;); // implementing getline() function with a delimiting character. cout&lt;<'
profile is :'<<p>In the above code, we take the user input by using getline() function, but this time we also add the delimiting character(&apos;&apos;) in a third parameter. Here, delimiting character is a space character, means the character that appears after space will not be considered.<p></p> <p> <strong>Output</strong> </p> <pre> Enter your profile : Software Developer Profile is: Software </pre> <h3>Getline Character Array</h3> <p>We can also define the getline() function for character array, but its syntax is different from the previous one.</p> <p> <strong>Syntax</strong> </p> <pre> istream&amp; getline(char* , int size); </pre> <p>In the above syntax, there are two parameters; one is <strong>char</strong> *, and the other is <strong>size</strong> .</p> <p> <strong>Where,</strong> </p> <p> <strong>char*:</strong> It is a character pointer that points to the array.</p> <p> <strong>Size:</strong> It acts as a delimiter that defines the size of the array means input cannot cross this size.</p> <p> <strong>Let&apos;s understand through an example.</strong> </p> <pre> #include #include using namespace std; int main() { char fruits[50]; // array declaration cout&lt;&lt; &apos;Enter your favorite fruit: &apos;; cin.getline(fruits, 50); // implementing getline() function std::cout &lt;&lt; &apos;
Your favorite fruit is :&apos;&lt;<fruits << std::endl; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter your favorite fruit: Watermelon Your favorite fruit is: Watermelon </pre> <hr></fruits></pre></'
profile></pre></'
hello></pre></'
hello>

Ve výše uvedeném výstupu jsme jako uživatelský vstup uvedli jméno 'John Miller', ale zobrazilo se pouze 'John'. Proto docházíme k závěru, že cin nebere v úvahu znak, když narazí na znak mezery.

Pojďme vyřešit výše uvedený problém pomocí funkce getline().

 #include #include using namespace std; int main() { string name; // variable declaration. std::cout &lt;&lt; &apos;Enter your name :&apos; &lt;&lt; std::endl; getline(cin,name); // implementing a getline() function cout&lt;<\'
hello \'<<name; return 0;} < pre> <p>In the above code, we have used the <strong>getline()</strong> function to accept the character even when the space character is encountered.</p> <p> <strong>Output</strong> </p> <pre> Enter your name : John Miller Hello John Miller </pre> <p>In the above output, we can observe that both the words, i.e., John and Miller, are displayed, which means that the getline() function considers the character after the space character also.</p> <p> <strong>When we do not want to read the character after space then we use the following code:</strong> </p> <pre> #include #include using namespace std; int main() { string profile; // variable declaration std::cout &lt;&lt; &apos;Enter your profile :&apos; &lt;&lt; std::endl; getline(cin,profile,&apos; &apos;); // implementing getline() function with a delimiting character. cout&lt;<\'
profile is :\'<<p>In the above code, we take the user input by using getline() function, but this time we also add the delimiting character(&apos;&apos;) in a third parameter. Here, delimiting character is a space character, means the character that appears after space will not be considered.<p></p> <p> <strong>Output</strong> </p> <pre> Enter your profile : Software Developer Profile is: Software </pre> <h3>Getline Character Array</h3> <p>We can also define the getline() function for character array, but its syntax is different from the previous one.</p> <p> <strong>Syntax</strong> </p> <pre> istream&amp; getline(char* , int size); </pre> <p>In the above syntax, there are two parameters; one is <strong>char</strong> *, and the other is <strong>size</strong> .</p> <p> <strong>Where,</strong> </p> <p> <strong>char*:</strong> It is a character pointer that points to the array.</p> <p> <strong>Size:</strong> It acts as a delimiter that defines the size of the array means input cannot cross this size.</p> <p> <strong>Let&apos;s understand through an example.</strong> </p> <pre> #include #include using namespace std; int main() { char fruits[50]; // array declaration cout&lt;&lt; &apos;Enter your favorite fruit: &apos;; cin.getline(fruits, 50); // implementing getline() function std::cout &lt;&lt; &apos;
Your favorite fruit is :&apos;&lt;<fruits << std::endl; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter your favorite fruit: Watermelon Your favorite fruit is: Watermelon </pre> <hr></fruits></pre></\'
profile></pre></\'
hello>

Ve výše uvedeném výstupu můžeme pozorovat, že jsou zobrazena obě slova, tj. John a Miller, což znamená, že funkce getline() bere v úvahu i znak za znakem mezery.

Když nechceme číst znak za mezerou, použijeme následující kód:

 #include #include using namespace std; int main() { string profile; // variable declaration std::cout &lt;&lt; &apos;Enter your profile :&apos; &lt;&lt; std::endl; getline(cin,profile,&apos; &apos;); // implementing getline() function with a delimiting character. cout&lt;<\'
profile is :\'<<p>In the above code, we take the user input by using getline() function, but this time we also add the delimiting character(&apos;&apos;) in a third parameter. Here, delimiting character is a space character, means the character that appears after space will not be considered.<p></p> <p> <strong>Output</strong> </p> <pre> Enter your profile : Software Developer Profile is: Software </pre> <h3>Getline Character Array</h3> <p>We can also define the getline() function for character array, but its syntax is different from the previous one.</p> <p> <strong>Syntax</strong> </p> <pre> istream&amp; getline(char* , int size); </pre> <p>In the above syntax, there are two parameters; one is <strong>char</strong> *, and the other is <strong>size</strong> .</p> <p> <strong>Where,</strong> </p> <p> <strong>char*:</strong> It is a character pointer that points to the array.</p> <p> <strong>Size:</strong> It acts as a delimiter that defines the size of the array means input cannot cross this size.</p> <p> <strong>Let&apos;s understand through an example.</strong> </p> <pre> #include #include using namespace std; int main() { char fruits[50]; // array declaration cout&lt;&lt; &apos;Enter your favorite fruit: &apos;; cin.getline(fruits, 50); // implementing getline() function std::cout &lt;&lt; &apos;
Your favorite fruit is :&apos;&lt;<fruits << std::endl; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter your favorite fruit: Watermelon Your favorite fruit is: Watermelon </pre> <hr></fruits></pre></\'
profile>

Pole znaků Getline

Můžeme také definovat funkci getline() pro pole znaků, ale její syntaxe je odlišná od předchozí.

co to znamená xd

Syntax

 istream&amp; getline(char* , int size); 

Ve výše uvedené syntaxi jsou dva parametry; jeden je char *, a druhý je velikost .

Kde,

znak*: Je to znakový ukazatel, který ukazuje na pole.

Velikost: Funguje jako oddělovač, který definuje velikost pole, což znamená, že vstup nemůže tuto velikost překročit.

Pojďme to pochopit na příkladu.

 #include #include using namespace std; int main() { char fruits[50]; // array declaration cout&lt;&lt; &apos;Enter your favorite fruit: &apos;; cin.getline(fruits, 50); // implementing getline() function std::cout &lt;&lt; &apos;
Your favorite fruit is :&apos;&lt;<fruits << std::endl; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Enter your favorite fruit: Watermelon Your favorite fruit is: Watermelon </pre> <hr></fruits>