C++/Console/cin ignore

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Using ignore() setting during char array reading

  
#include <iostream>
using namespace std;
int main()
{
   char stringOne[255];
   char stringTwo[255];
   cout << "Enter string one:";
   cin.get(stringOne,255);
   cout << "String one: " << stringOne << endl;
   cout << "Enter string two: ";
   cin.getline(stringTwo,255);
   cout << "String two: " << stringTwo << endl;
   cout << "\n\nNow try again...\n";
   cout << "Enter string one: ";
   cin.get(stringOne,255);
   cout << "String one: " << stringOne<< endl;
   cin.ignore(255,"\n");
   cout << "Enter string two: ";
   cin.getline(stringTwo,255);
   cout << "String Two: " << stringTwo<< endl;
   return 0;
}


Using ignore() with string output

  
#include <iostream>
using namespace std;
int main()
{
   char stringOne[255];
   char stringTwo[255];
   cout << "Enter string one:";
   cin.get(stringOne,255);
   cout << "String one" << stringOne << endl;
   cout << "Enter string two: ";
   cin.getline(stringTwo,255);
   cout << "String two: " << stringTwo << endl;
   cout << "\n\nNow try again...\n";
   cout << "Enter string one: ";
   cin.get(stringOne,255);
   cout << "String one: " << stringOne<< endl;
   cin.ignore(255,"\n");
   cout << "Enter string two: ";
   cin.getline(stringTwo,255);
   cout << "String Two: " << stringTwo<< endl;
   return 0;
}