The cin objekt v C++ je objekt triedy iostream . Používa sa na prijatie vstupu zo štandardného vstupného zariadenia, t.j. klávesnice. Je spojený so štandardným štandardným vstupným tokom C. The operátor extrakcie(>>) sa používa spolu s objektom cin na čítanie vstupov. Operátor extrakcie extrahuje údaje z objektu cin, ktorý sa zadáva pomocou klávesnice.
Program 1:
Nižšie je uvedený program C++ na implementáciu objektu cin:
C++ // C++ program to demonstrate the // cin object #include using namespace std; // Driver Code int main() { string s; // Take input using cin cin>> s; // Tlač výstupu cout<< s; return 0; }>
Vstup:
Výkon:
analyzovať reťazec na int
Program 2:
Viacnásobné vstupy pomocou operátorov extrakcie (>>) s cin. Nižšie je uvedený program C++, ktorý prevezme viacero používateľských vstupov:
// C++ program to illustrate the take // multiple input #include using namespace std; // Driver Code int main() { string name; int age; // Take multiple input using cin cin>> meno>> vek; // Tlač výstupu cout<< 'Name : ' << name << endl; cout << 'Age : ' << age << endl; return 0; }>
Vstup:
Výkon:
The jedenie možno použiť aj s niektorými členskými funkciami, ktoré sú nasledovné:
cin.getline(char *buffer, int N) :
Číta prúd znakov dĺžky N do string buffer , Po prečítaní sa zastaví (N – 1) znakov alebo nájde koniec súboru alebo znak nového riadku (
). Nižšie je uvedený program C++ na implementáciu cin.getline() :
mamta kulkarni herecC++
// C++ program to illustrate the use // of cin.getline #include using namespace std; // Driver Code int main() { char name[5]; // Reads stream of 3 // characters cin.getline(name, 3); // Print output cout << name << endl; return 0; }>
Vstup:
Výkon:
cin.get(char& var):
Prečíta vstupný znak a uloží ho do a premenlivý . Nižšie je uvedený program C++ na implementáciu cin.get() :
C++ // C++ program to illustrate the use // of cin.get() #include using namespace std; // Driver Code int main() { char ch[30]; cin.get(ch, 25); // Print ch cout << ch; }>
Vstup:
Výkon:
cin.read(char *buffer, int N):
Číta prúd znakov dĺžky N . Nižšie je uvedený program C++ na implementáciu cin.read() :
C++ // C++ program to illustrate the use // of cin.read() #include using namespace std; // Driver Code int main() { char gfg[20]; // Reads stream of characters cin.read(gfg, 10); // Print output cout << gfg << endl; return 0; }>
Vstup:
stream filtra java
Výkon:
cin.ignore():
Ignoruje alebo vymaže jeden alebo viac znakov zo vstupnej vyrovnávacej pamäte. Nižšie je uvedený program C++ na implementáciu cin.ignore() :
C++ // C++ program to illustrate the use // of cin.ignore() #include // used to get stream size #include // used to get numeric limits #include using namespace std; // Driver Code int main() { int x; char str[80]; cout << 'Enter a number andstring:
'; cin>> x; // vyčisti vyrovnávaciu pamäť pred prijatím // nový riadok cin.ignore(numeric_limits::max(), '
'); // Zadajte reťazec cin.getline(str, 80); cout<< 'You have entered:
'; cout << x << endl; cout << str << endl; return 0; }>
Vstup:
definovať počítač
Výkon:
Vysvetlenie: Vo vyššie uvedenom programe ak cin.ignore() nebolo použité, potom po zadaní čísla, keď užívateľ stlačí enter pre zadanie reťazca, bude výstupom iba zadané číslo. Program nezoberie zadanie reťazca. Aby ste sa vyhli tomuto problému cin.ignore() sa použije, znak nového riadku sa bude ignorovať.