logo

Cin.ignore()-funksjonen i C++

I C++ er cin.ignore() funksjon er avgjørende for å løse input-relaterte problemer , spesielt når du bruker spiser og getline-funksjoner sammen. Ved å tømme inngangsbufferen og fjerne unødvendige tegn, kan utviklere sikre at inndataprosesser oppfører seg som forventet og nøyaktig. I denne artikkelen vil vi undersøke cin.ignore() funksjonens syntaks, bruk, eksempler , og forventede utganger .

De strøm klasses cin.ignore() funksjon kan brukes til å ignorere tekst opp til et gitt antall tegn eller til et spesifikt skilletegn er funnet. Syntaksen er som følger:

cin.ignore(n, skilletegn);

Parametre for funksjonen Cin.ignore() Syntaks:

n (valgfritt): Den angir hvor mange tegn som skal være ignorert .

Skilletegn (valgfritt): Den spesifiserer en skilletegn , hvoretter innspillet vil bli ignorert. Hvis ikke spesifisert , er det standard 1 . Hvis ingenting er spesifisert , deretter ewline-tegn ('n') brukes av misligholde .

ins nøkkel

Bruk og drift av Cin.ignore()-funksjonen:

Hovedformålet med cin.ignore() funksjon er å fjerne uønskede karakterer fra inngangsbuffer . Ny inngang kan nå leses fordi inngangsbufferen er tømt. Den kan brukes i en rekke omstendigheter, inkludert etter lese numerisk inndata med spiser , før lesestrenger med getline , og når du kombinerer separate inndataprosedyrer.

Inntil en av følgende betingelser er møtt, cin.ignore() leser tegn fra inndatabufferen og forkaster dem:

  1. Hvis 'n' tegn ble spesifisert, ble de ignorert.
  2. Inntil skilletegnet (hvis spesifisert) ble funnet, ignorerte det tegn.
  3. Når den gjør det inngangsbuffer er full.

Utelater én karakter

La oss tenke på et enkelt scenario der vi må lese to tegn fra brukeren. Men vi trenger ikke første tegn ; vi trenger bare sekund . Som vist nedenfor, kan vi oppnå dette ved å bruke 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;>

Forklaring:

I eksemplet ovenfor bruker vi std::noskipws til stopptegn fra lesing med mellomrom hoppet over. For å fjerne det uønskede tegnet etter å ha lest det første tegnet, ringer vi cin.ignore() uten noen argumenter. Som et resultat har 'andre tegn' variabelen inneholder bare andre tegn .

Inntil en skilletegn

La oss si at vi bare vil lese det første ordet fra en tekstlinje fra brukeren. Dette kan vi få til ved hjelp av cin.ignore() og skilletegnet spesifisert som følger:

 #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;>

Forklaring:

I eksemplet ovenfor, ledende mellomrom hoppes over å bruke std::ws før inngangen leses ved hjelp av getline() . Når skilletegn er satt til a mellomrom (' '), cin.ignore() vil bare trekke ut det første ordet og se bort fra alle andre tegn frem til det punktet.

Konklusjon:

For å adressere input-relaterte bekymringer og gi nøyaktig kontroll over inngangsbufferen, C++ cin.ignore() funksjon er et nyttig verktøy. Utviklere kan effektivt håndtere uønskede karakterer og oppnå den nødvendige oppførselen i programmene deres ved å forstå dets syntaks, bruk og funksjonsprinsipp.

Utviklere kan sikre presise og forventede inndataprosedyrer ved å bruke cin.ignore() funksjon for å hoppe til et angitt skilletegn eller se bort fra et sett med tegn. Når du arbeider med blandede inndatatyper, numerisk inndata som etterfølges av strenginndata, eller når du leser strenger ved hjelp av getline() , denne funksjonen er ganske nyttig.

fibonacci-sekvens java

Utviklere kan unngå uventet oppførsel forårsaket av dvelende tegn i inndatabufferen ved å bruke riktig cin.ignore() . Ved å tømme bufferen og tillate lesing av nye input, hjelper denne funksjonen med å opprettholde integriteten til følgende inndataoperasjoner.

For riktig håndtering av ulike inngangsforhold er det viktig å forstå parameterne og oppførselen til cin.ignore() . Med hjelp av cin.ignore() , kan programmerere lage kraftig og pålitelig inndatahåndteringssystemer for deres C++ programmer , enten de vil ignorere et enkelt tegn eller hoppe til et skilletegn.

Avslutningsvis cin.ignore() funksjon er en avgjørende del av C++-inndatabehandling siden den gjør det mulig for programmerere å fjerne unødvendige tegn og garantere nøyaktige og sømløse inndataoperasjoner. Å forstå hvordan du bruker det effektivt kan forbedre stabiliteten og brukervennligheten til C++-applikasjoner betraktelig.