Cin je predmet, ki se uporablja za sprejemanje vnosa od uporabnika, vendar ne dovoljuje sprejemanja vnosa v več vrsticah. Za sprejem več vrstic uporabimo funkcijo getline(). To je vnaprej določena funkcija, opredeljena v a glava datoteke, ki se uporablja za sprejemanje vrstice ali niza iz vhodnega toka, dokler ni odkrit ločilni znak.
Sintaksa funkcije getline():
Funkcijo lahko predstavimo na dva načina:
- Prvi način deklaracije je posredovanje treh parametrov.
istream& getline( istream& is, string& str, char delim );
Zgornja sintaksa vsebuje tri parametre, tj. je, str , in delim .
Kje,
je: Je objekt razreda istream, ki določa, od kod brati vhodni tok.
str: Je objekt niza, v katerem je shranjen niz.
gimp shrani kot jpegdelim: To je razmejitveni znak.
Povratna vrednost
Ta funkcija vrne objekt vhodnega toka, ki je posredovan funkciji kot parameter.
- Drugi način deklaracije je posredovanje dveh parametrov.
istream& getline( istream& is, string& str );
Zgornja sintaksa vsebuje dva parametra, tj. je in str . Ta sintaksa je skoraj podobna zgornji sintaksi; razlika je le v tem, da nima razmejitvenega značaja.
Kje,
je: Je objekt razreda istream, ki določa, od kod brati vhodni tok.
str: Je objekt niza, v katerem je shranjen niz.
Povratna vrednost
brisanje predpomnilnika npm
Ta funkcija vrne tudi vhodni tok, ki je kot parameter posredovan funkciji.
Razumejmo skozi primer.
Najprej si bomo ogledali primer, kjer vzamemo uporabniški vnos brez uporabe funkcije getline().
#include #include using namespace std; int main() { string name; // variable declaration std::cout << 'Enter your name :' <>name; cout<<' hello '<<name; return 0; } < pre> <p>In the above code, we take the user input by using the statement <strong>cin>>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 'John Miller' as user input, but only 'John' was displayed. Therefore, we conclude that cin does not consider the character when the space character is encountered.</p> <p> <strong>Let'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 << 'Enter your name :' << std::endl; getline(cin,name); // implementing a getline() function cout<<' 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 << 'Enter your profile :' << std::endl; getline(cin,profile,' '); // implementing getline() function with a delimiting character. cout<<' 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('') 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& 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's understand through an example.</strong> </p> <pre> #include #include using namespace std; int main() { char fruits[50]; // array declaration cout<< 'Enter your favorite fruit: '; cin.getline(fruits, 50); // implementing getline() function std::cout << ' Your favorite fruit is :'<<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>
V zgornjem izhodu smo dali ime 'John Miller' kot uporabniški vnos, vendar je bil prikazan samo 'John'. Zato sklepamo, da cin ne upošteva znaka, ko naleti na presledek.
Rešimo zgornjo težavo s funkcijo getline().
#include #include using namespace std; int main() { string name; // variable declaration. std::cout << 'Enter your name :' << std::endl; getline(cin,name); // implementing a getline() function cout<<\' 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 << 'Enter your profile :' << std::endl; getline(cin,profile,' '); // implementing getline() function with a delimiting character. cout<<\' 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('') 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& 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's understand through an example.</strong> </p> <pre> #include #include using namespace std; int main() { char fruits[50]; // array declaration cout<< 'Enter your favorite fruit: '; cin.getline(fruits, 50); // implementing getline() function std::cout << ' Your favorite fruit is :'<<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>
V zgornjem izhodu lahko opazimo, da sta prikazani obe besedi, tj. John in Miller, kar pomeni, da funkcija getline() upošteva tudi znak za presledkom.
Ko ne želimo prebrati znaka za presledkom, uporabimo naslednjo kodo:
#include #include using namespace std; int main() { string profile; // variable declaration std::cout << 'Enter your profile :' << std::endl; getline(cin,profile,' '); // implementing getline() function with a delimiting character. cout<<\' 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('') 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& 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's understand through an example.</strong> </p> <pre> #include #include using namespace std; int main() { char fruits[50]; // array declaration cout<< 'Enter your favorite fruit: '; cin.getline(fruits, 50); // implementing getline() function std::cout << ' Your favorite fruit is :'<<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>
Niz znakov Getline
Definiramo lahko tudi funkcijo getline() za niz znakov, vendar je njena sintaksa drugačna od prejšnje.
Sintaksa
istream& getline(char* , int size);
V zgornji sintaksi sta dva parametra; eden je char *, in drugo je velikost .
čajna žlička proti jedilni žlici
Kje,
char*: Je kazalec znakov, ki kaže na matriko.
Velikost: Deluje kot ločilo, ki določa velikost matrike, kar pomeni, da vnos ne more preseči te velikosti.
Razumejmo skozi primer.
#include #include using namespace std; int main() { char fruits[50]; // array declaration cout<< 'Enter your favorite fruit: '; cin.getline(fruits, 50); // implementing getline() function std::cout << ' Your favorite fruit is :'<<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>
\' profile>\' hello>' hello>