cin er et objekt, der bruges til at tage input fra brugeren, men som ikke tillader at tage input i flere linjer. For at acceptere de flere linjer bruger vi funktionen getline(). Det er en foruddefineret funktion defineret i en header-fil, der bruges til at acceptere en linje eller en streng fra inputstrømmen, indtil det afgrænsende tegn stødes på.
Syntaks for getline() funktion:
Der er to måder at repræsentere en funktion på:
- Den første måde at deklarere på er at videregive tre parametre.
istream& getline( istream& is, string& str, char delim );
Ovenstående syntaks indeholder tre parametre, dvs. er, str , og Jeg deler .
Hvor,
er: Det er et objekt i istream-klassen, der definerer, hvorfra inputstrømmen skal læses.
str: Det er et strengobjekt, hvori streng er gemt.
bellford algoritmedel: Det er den afgrænsende karakter.
Returværdi
Denne funktion returnerer input-stream-objektet, som sendes som en parameter til funktionen.
- Den anden måde at erklære på er at videregive to parametre.
istream& getline( istream& is, string& str );
Ovenstående syntaks indeholder to parametre, dvs. er og str . Denne syntaks ligner næsten ovenstående syntaks; den eneste forskel er, at den ikke har nogen afgrænsende karakter.
Hvor,
er: Det er et objekt i istream-klassen, der definerer, hvorfra inputstrømmen skal læses.
str: Det er et strengobjekt, hvori streng er gemt.
abstraktion i java
Returværdi
Denne funktion returnerer også inputstrømmen, som sendes som en parameter til funktionen.
Lad os forstå gennem et eksempel.
Først vil vi se på et eksempel, hvor vi tager brugerinputtet uden at bruge getline()-funktionen.
#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>
I ovenstående output gav vi navnet 'John Miller' som brugerinput, men kun 'John' blev vist. Derfor konkluderer vi, at cin ikke overvejer karakteren, når mellemrumskarakteren stødes på.
Lad os løse ovenstående problem ved at bruge getline()-funktionen.
kort i java
#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>
I ovenstående output kan vi observere, at begge ordene, dvs. John og Miller, vises, hvilket betyder, at getline()-funktionen også betragter tegnet efter mellemrumstegnet.
Når vi ikke ønsker at læse tegnet efter mellemrum, bruger vi følgende kode:
#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>
Getline Character Array
Vi kan også definere getline()-funktionen for karakterarray, men dens syntaks er forskellig fra den forrige.
Syntaks
istream& getline(char* , int size);
I ovenstående syntaks er der to parametre; den ene er char *, og den anden er størrelse .
Hvor,
char*: Det er en tegnmarkør, der peger på arrayet.
Størrelse: Den fungerer som en afgrænsning, der definerer størrelsen af arrayet, betyder, at input ikke kan krydse denne størrelse.
Lad os forstå gennem et eksempel.
#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>