C++/Console/cin

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

A Closer Look at the I/O Operators

 
#include <iostream>
using namespace std;
int main()
{
  float f;
  char str[80];
  double d;
  cout << "Enter two floating point numbers: ";
  cin >> f >> d;
  cout << "Enter a string: ";
  cin >> str;
  cout << f << " " << d << " " << str;
  return 0;
}


cin and cout work with char array

 
#include <iostream>
using namespace std;
int main ()
{
   char name[ 80] = {"J", "e", "f", "f", "/0" };
   cout << "Enter your name: ";
   cin >> name;
   cout << "Your name is " << name;
   return 0;
}


cin Object"s getline Function

 
#include <iostream>
using namespace std;
int main ()
{
   char name[ 80] = {"J", "e", "f", "f", "/0" };
   cout << "Enter your name: ";
   cin >> name;
   cout << "Your name is " << name;
   return 0;
}


cin to read

 
#include <iostream>
using namespace std;
int main ()
{
   int testScore1, testScore2, testScore3;
   cout << "Enter test score #1: ";
   cin >> testScore1;
   cout << "Enter test score #2: ";
   cin >> testScore2;
   cout << "Enter test score #3: ";
   cin >> testScore3;
   cout << "Test score #1: " << testScore1 << endl;
   cout << "Test score #2: " << testScore2 << endl;
   cout << "Test score #3: " << testScore3 << endl;
   return 0;
}


cin to read float in C++

 
#include <iostream>
using namespace std;
int main()
{
   double x, y;
   cout << "Enter two floating-point values: ";
   cin >> x ;
   cin >> y;
   cout << "The average of the two numbers is: " << (x + y)/2.0 << endl;
   return 0;
}


Filling an Array with a Maximum Number of Characters

  
#include <iostream>
using namespace std;
int main()
{
    char buffer[80] = {"\0"};
    cout << "Enter the string: ";
    cin.get(buffer, 79);       // get up to 79 or newline
    cout << "Here"s the buffer:  " << buffer << endl;
    return 0;
}


Unformattwd Input/Output

 
#include <iostream>
#include <string>
using namespace std;
string header ="Unformatted Input";
int main()
{
   string word, rest;
   cout << header << "Press <return> to go on" << endl;
   cin.get();                  // Read the new line without saving.
                               
   cout << "\nEnter a sentence! End with <!> and <return>." << endl;
   cin >> word;                // Read the first word
   getline( cin, rest, "!");   // read a line up to the character !
   cout << "The first word:   " << word  << endl
        << "Remaining text: " << rest << endl;
   return 0;
}


Use the getline function to read the user"s input and assign that input to the character array

 
#include <iostream>
using namespace std;
int main ()
{
   char name[80] = {"J", "e", "f", "f", "/0" };
   cout << "Enter your name: ";
   cin.getline(name, 80);
   cout << "Your name is " << name;
   return 0;
}


Using the cin and cout Objects with Arrays

 
#include <iostream>
using namespace std;
const int MAX = 3;
int main ()
{
   char grades[MAX];
   for (int i = 0; i < MAX; i++)
   {
      cout << "Enter grade for test #" << i + 1 << ":";
      cin >> grades[i];
   }
   return 0;
}