logo

Cin.ignore() funkcija C++

Programmā C++, cin.ignore() funkcija ir būtiska, lai atrisinātu ar ievadi saistītas problēmas , īpaši lietojot ēšana un getline funkcijas kopā. Notīrot ievades buferi un noņemot nevajadzīgās rakstzīmes, izstrādātāji var nodrošināt ievades procesu darbību, kā paredzēts un precīzi. Šajā rakstā mēs apskatīsim cin.ignore() funkcijas sintakse, lietojums, piemēri , un sagaidāmie rezultāti .

The straume klases cin.ignorēt() funkciju var izmantot, lai ignorētu tekstu līdz noteiktam rakstzīmju skaitam vai līdz tiek atrasts konkrēts norobežotājs. Tās sintakse ir šāda:

cin.ignorēt(n, norobežotājs);

Funkcijas Cin.ignore() parametri Sintakse:

n (neobligāti): Tas norāda, cik rakstzīmēm jābūt ignorēts .

Atdalītājs (pēc izvēles): Tajā norādīts a norobežotāja raksturs , pēc kura ievade tiks ignorēta. Ja nē norādīts , pēc noklusējuma tas ir 1 . Ja nekas nav norādīts , n ewline rakstzīme ('n') izmanto noklusējuma .

lejupielādēt autocad 2019 angļu mediafire

Funkcijas Cin.ignore() lietošana un darbība:

Galvenais mērķis cin.ignorēt() funkciju ir noņemt nevēlami varoņi no ievades buferis . Jauno ievadi tagad var nolasīt, jo ievades buferis ir notīrīts. To var izmantot dažādos apstākļos, tostarp pēc ciparu ievades lasīšana ar ēšana , pirms tam stīgu lasīšana ar getline , un kombinējot atsevišķas ievades procedūras.

Līdz brīdim, kad tiek izpildīts kāds no tālāk norādītajiem nosacījumiem met, cin.ignore() skan rakstzīmes no ievades bufera un atmet tās:

  1. Ja 'n' rakstzīmes tika precizēti, tie tika ignorēti.
  2. Līdz brīdim, kad tika atrasts norobežotājs (ja norādīts), tas neņēma vērā rakstzīmes.
  3. Kad tas notiek, ievades buferis ir pilns.

Izlaižot vienu varoni

Padomāsim par vienkāršu scenāriju, kurā mums ir jānolasa divas rakstzīmes no lietotāja. Bet mums tas nav vajadzīgs pirmais varonis ; mums vajag tikai otrais . Kā parādīts tālāk, mēs varam to paveikt, izmantojot cin.ignore() .

 #include int main() { char secondCharacter; std::cout&lt;&gt;std::noskipws&gt;&gt;secondCharacter; std::cin.ignore(); std::cout&lt;&lt; &apos;The second character is: &apos; &lt;<secondcharacter<<std::endl; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Enter two characters: AB The second character is: B </pre> <p> <strong>Explanation:</strong> </p> <p>In the above example, we use <strong> <em>std::noskipws</em> </strong> to <strong> <em>stop characters</em> </strong> from reading with whitespace skipped. In order to remove the undesirable character after reading the first character, we call <strong> <em>cin.ignore()</em> </strong> without any arguments. As a result, the <strong> <em>&apos;secondCharacter&apos;</em> </strong> variable only contains the <strong> <em>second character</em> </strong> .</p> <h3>Until a Delimiter</h3> <p>Let&apos;s say we simply want to <strong> <em>read</em> </strong> the first word from a user-provided line of text. We can accomplish this with the help of <strong> <em>cin.ignore()</em> </strong> and the delimiter specified as follows:</p> <pre> #include #include int main() { std::string firstWord; std::cout&lt;&gt;std::ws, firstWord, &apos; &apos;); std::cout&lt;&lt; &apos;The first word is: &apos; &lt;<firstword<<std::endl; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Enter a sentence: Hello, World! How are you? The first word is: Hello, </pre> <p> <strong>Explanation:</strong> </p> <p>In the above example, leading <strong> <em>whitespace</em> </strong> is skipped using <strong> <em>std::ws</em> </strong> before the input is read using <strong> <em>getline()</em> </strong> . When the <strong> <em>delimiter</em> </strong> is set to a <strong> <em>space (&apos; &apos;), cin.ignore()</em> </strong> will only extract the first word and disregard all other characters up to that point.</p> <h2>Conclusion:</h2> <p>For addressing input-related concerns and providing exact control over the input buffer, the C++ <strong> <em>cin.ignore() function</em> </strong> is a useful tool. Developers can efficiently handle undesired characters and accomplish the required behavior in their programs by understanding its syntax, usage, and functioning principle.</p> <p>Developers can ensure precise and anticipated input procedures by using the <strong> <em>cin.ignore() function</em> </strong> to skip until a designated delimiter or disregard a set of characters. When working with mixed input types, numeric input that is followed by string input, or when reading strings using <strong> <em>getline()</em> </strong> , this function is quite helpful.</p> <p>Developers can avoid unexpected behavior brought on by lingering characters in the input buffer by properly using <strong> <em>cin.ignore()</em> </strong> . By clearing the buffer and allowing for the reading of new input, this function aids in maintaining the integrity of following input operations.</p> <p>For proper handling of various input conditions, it is imperative to comprehend the parameters and behavior of <strong> <em>cin.ignore()</em> </strong> . With the help of <strong> <em>cin.ignore()</em> </strong> , programmers can create <strong> <em>powerful</em> </strong> and <strong> <em>dependable</em> </strong> input handling systems for their <strong> <em>C++ programs</em> </strong> , whether they want to ignore a single character or skip till a delimiter.</p> <p>In conclusion, the <strong> <em>cin.ignore() function</em> </strong> is a crucial part of C++ input processing since it enables programmers to remove unnecessary characters and guarantee accurate and seamless input operations. Understanding how to use it effectively can considerably improve the stability and usability of C++ applications.</p> <hr></firstword<<std::endl;></pre></secondcharacter<<std::endl;>

Paskaidrojums:

Iepriekš minētajā piemērā mēs izmantojam std::noskipws uz stop rakstzīmes no lasīšanas, izlaižot atstarpes. Lai pēc pirmās rakstzīmes izlasīšanas noņemtu nevēlamo rakstzīmi, mēs zvanām cin.ignore() bez jebkādiem argumentiem. Tā rezultātā, 'otrais raksturs' mainīgais satur tikai otrais raksturs .

Līdz norobežotājam

Pieņemsim, ka mēs vienkārši vēlamies lasīt pirmais vārds no lietotāja nodrošinātas teksta rindiņas. Mēs to varam paveikt ar palīdzību cin.ignore() un norobežotājs, kas norādīts šādi:

 #include #include int main() { std::string firstWord; std::cout&lt;&gt;std::ws, firstWord, &apos; &apos;); std::cout&lt;&lt; &apos;The first word is: &apos; &lt;<firstword<<std::endl; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Enter a sentence: Hello, World! How are you? The first word is: Hello, </pre> <p> <strong>Explanation:</strong> </p> <p>In the above example, leading <strong> <em>whitespace</em> </strong> is skipped using <strong> <em>std::ws</em> </strong> before the input is read using <strong> <em>getline()</em> </strong> . When the <strong> <em>delimiter</em> </strong> is set to a <strong> <em>space (&apos; &apos;), cin.ignore()</em> </strong> will only extract the first word and disregard all other characters up to that point.</p> <h2>Conclusion:</h2> <p>For addressing input-related concerns and providing exact control over the input buffer, the C++ <strong> <em>cin.ignore() function</em> </strong> is a useful tool. Developers can efficiently handle undesired characters and accomplish the required behavior in their programs by understanding its syntax, usage, and functioning principle.</p> <p>Developers can ensure precise and anticipated input procedures by using the <strong> <em>cin.ignore() function</em> </strong> to skip until a designated delimiter or disregard a set of characters. When working with mixed input types, numeric input that is followed by string input, or when reading strings using <strong> <em>getline()</em> </strong> , this function is quite helpful.</p> <p>Developers can avoid unexpected behavior brought on by lingering characters in the input buffer by properly using <strong> <em>cin.ignore()</em> </strong> . By clearing the buffer and allowing for the reading of new input, this function aids in maintaining the integrity of following input operations.</p> <p>For proper handling of various input conditions, it is imperative to comprehend the parameters and behavior of <strong> <em>cin.ignore()</em> </strong> . With the help of <strong> <em>cin.ignore()</em> </strong> , programmers can create <strong> <em>powerful</em> </strong> and <strong> <em>dependable</em> </strong> input handling systems for their <strong> <em>C++ programs</em> </strong> , whether they want to ignore a single character or skip till a delimiter.</p> <p>In conclusion, the <strong> <em>cin.ignore() function</em> </strong> is a crucial part of C++ input processing since it enables programmers to remove unnecessary characters and guarantee accurate and seamless input operations. Understanding how to use it effectively can considerably improve the stability and usability of C++ applications.</p> <hr></firstword<<std::endl;>

Paskaidrojums:

Iepriekš minētajā piemērā vadošais atstarpe tiek izlaists, izmantojot std::ws pirms ievades lasīšanas, izmantojot getline() . Kad norobežotājs ir iestatīts uz a atstarpe (''), cin.ignore() izvilks tikai pirmo vārdu un neņems vērā visas pārējās rakstzīmes līdz šim brīdim.

Secinājums:

Lai novērstu ar ievadi saistītas problēmas un nodrošinātu precīzu ievades bufera kontroli, C++ cin.ignorēt() funkciju ir noderīgs rīks. Izstrādātāji var efektīvi rīkoties ar nevēlamām rakstzīmēm un veikt nepieciešamo darbību savās programmās, izprotot to sintaksi, lietojumu un darbības principu.

Izstrādātāji var nodrošināt precīzas un paredzamas ievades procedūras, izmantojot cin.ignorēt() funkciju lai izlaistu līdz noteiktajam norobežotājam vai neņemtu vērā rakstzīmju kopu. Strādājot ar jauktiem ievades veidiem, ciparu ievadi, kam seko virknes ievade, vai lasot virknes, izmantojot getline() , šī funkcija ir ļoti noderīga.

Pareizi izmantojot izstrādātājus, var izvairīties no neparedzētas rīcības, ko izraisa ieilgušas rakstzīmes ievades buferī cin.ignore() . Notīrot buferi un ļaujot nolasīt jaunu ievadi, šī funkcija palīdz saglabāt turpmāko ievades darbību integritāti.

ja vēl bash

Lai pareizi apstrādātu dažādus ievades apstākļus, ir obligāti jāsaprot parametri un darbība cin.ignore() . Ar palīdzību cin.ignore() , programmētāji var izveidot spēcīgs un uzticams ievades apstrādes sistēmas viņu C++ programmas , neatkarīgi no tā, vai viņi vēlas ignorēt vienu rakstzīmi vai izlaist līdz atdalītājam.

Noslēgumā jāsaka, cin.ignorēt() funkciju ir būtiska C++ ievades apstrādes sastāvdaļa, jo tā ļauj programmētājiem noņemt nevajadzīgās rakstzīmes un garantēt precīzas un netraucētas ievades darbības. Izpratne par to, kā to efektīvi izmantot, var ievērojami uzlabot C++ lietojumprogrammu stabilitāti un lietojamību.