logo

Funktionen Cin.ignore() i C++

I C++ är cin.ignore() funktion är avgörande för att lösa ingångsrelaterade problem , speciellt när du använder äter och getline funktioner tillsammans. Genom att rensa indatabufferten och ta bort onödiga tecken kan utvecklare säkerställa att inmatningsprocesser beter sig som förväntat och korrekt. I den här artikeln kommer vi att undersöka cin.ignore()-funktionens syntax, användning, exempel , och förväntade resultat .

De ström klass cin.ignore() funktion kan användas för att ignorera text upp till ett givet antal tecken eller tills en specifik avgränsare hittas. Dess syntax är följande:

cin.ignore(n, avgränsare);

Parametrar för funktionen Cin.ignore() Syntax:

n (valfritt): Den anger hur många tecken som ska vara ignoreras .

Avgränsare (valfritt): Den specificerar a avgränsningstecken , varefter inmatningen kommer att ignoreras. Om inte specificerad , den är som standard 1 . Om ingenting är specificerad n ewline-tecken ('n') används av standard .

ram tkinter

Användning och användning av Cin.ignore()-funktionen:

Huvudsyftet med cin.ignore() funktion är att ta bort oönskade karaktärer från ingångsbuffert . Ny ingång kan nu läsas eftersom ingångsbufferten har tömts. Den kan användas i en mängd olika omständigheter, inklusive efter läsa numerisk inmatning med äter , innan lässträngar med getline , och när man kombinerar separata inmatningsprocedurer.

Tills något av följande villkor är träffade, cin.ignore() lyder tecken från inmatningsbufferten och kasserar dem:

  1. Om 'n' tecken specificerades, ignorerades de.
  2. Fram tills avgränsaren (om angiven) hittades, ignorerade den tecken.
  3. När det gör det ingångsbuffert är full.

Utelämnar en karaktär

Låt oss tänka på ett enkelt scenario där vi behöver läsa två tecken från användaren. Men vi behöver inte första karaktären ; vi behöver bara andra . Som visas nedan kan vi åstadkomma detta med hjälp av 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;>

Förklaring:

I exemplet ovan använder vi std::noskipws till stopptecken från läsning med blanksteg överhoppad. För att ta bort det oönskade tecknet efter att ha läst det första tecknet ringer vi cin.ignore() utan några argument. Som ett resultat av detta 'andra tecken' variabeln innehåller endast andra tecken .

Tills en avgränsare

Låt oss säga att vi bara vill läsa det första ordet från en textrad som användaren tillhandahåller. Vi kan åstadkomma detta med hjälp av cin.ignore() och avgränsaren specificerad enligt följande:

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

Förklaring:

I exemplet ovan, ledande blanksteg hoppas över att använda std::ws innan ingången läses med getline() . När avgränsare är inställd på a mellanslag (' '), cin.ignore() kommer bara att extrahera det första ordet och bortse från alla andra tecken fram till den punkten.

Slutsats:

För att adressera ingångsrelaterade problem och ge exakt kontroll över ingångsbufferten, C++ cin.ignore() funktion är ett användbart verktyg. Utvecklare kan effektivt hantera oönskade karaktärer och uppnå det nödvändiga beteendet i sina program genom att förstå dess syntax, användning och funktionsprincip.

Utvecklare kan säkerställa exakta och förväntade inmatningsprocedurer genom att använda cin.ignore() funktion för att hoppa över till en angiven avgränsare eller bortse från en uppsättning tecken. När du arbetar med blandade inmatningstyper, numerisk inmatning som följs av stränginmatning, eller när du läser strängar med getline() , den här funktionen är till stor hjälp.

Utvecklare kan undvika oväntat beteende som orsakas av kvardröjande tecken i inmatningsbufferten genom att använda korrekt cin.ignore() . Genom att rensa bufferten och tillåta läsning av ny ingång, hjälper denna funktion till att upprätthålla integriteten för följande inmatningsoperationer.

För korrekt hantering av olika inmatningsförhållanden är det absolut nödvändigt att förstå parametrarna och beteendet hos cin.ignore() . Med hjälp av cin.ignore() , kan programmerare skapa kraftfull och pålitlig ingångshanteringssystem för deras C++-program , om de vill ignorera ett enstaka tecken eller hoppa över till en avgränsare.

Sammanfattningsvis cin.ignore() funktion är en avgörande del av C++-indatabehandling eftersom den gör det möjligt för programmerare att ta bort onödiga tecken och garantera korrekta och sömlösa inmatningsoperationer. Att förstå hur man använder det effektivt kan avsevärt förbättra stabiliteten och användbarheten för C++-applikationer.

format sträng java