School recruit knowledge summary C ++ (a) processing input

For all strongly condemned not explicitly given input and output, why? ? ?

  • For some arithmetic problems, there is no clear inputs and outputs. We can only guess is based on a blank line , or terminator file , this is too difficult.

First, the first clear case c ++ how to process input

The first is the C language scanf in pritf and functions. Because it is c ++, do not do too much expansion.

Then enter in c ++. I suggest two master like, I just mastered cin and string getline.

China

  cin receive data from the buffer until it encounters a space, TAB, Enter to stop, but need to be clear that these spaces, TAB, Enter will still remain in the buffer.

From this program, we can verify this. And continue to think, you can come up with, cin will be at the start, hit a space, a carriage return and TAB are automatically extracted, knowing encountered several non-top characters.

#include<iostream>
#include<string>
#include<vector>
#include <sstream>
using namespace std;
int main()
{
  string a;
  cin>>a;
  cout<<a<<endl;
    if(getchar() == '\n')
    {
        cout<<a.size()<<endl;

        cout<<" can not deal '\\n'"<<endl;
        IF    }
    carriage return is still there still buffer//
(getchar () == '  ' ) 
    { 
        COUT << a.size () << endl; 
        COUT << " CAN Not Deal '' " << endl;
         // spaces whether the buffer is still 
    }       
}

getline

This is the string getline, you need to include #include <string>. There is also a cin.getline (char *), the need to use an array of characters, I prefer to use string, so not too concerned about this, but it should be about.

getline (cin, str, '\ n'). There are three parameters, CIN is the first, the second is an array, a third terminator, the default is the transport, may set their own.

As can be seen from the above terminator, the surface is not meant to take a row to be changed terminator ',' this encounter, '' terminates.

Characteristics: getline function will stop only sensitive to terminator, terminator encountered, but will terminator removed from the buffer.

Here's the difference:

  cin: encounter spaces, carriage returns, TAB will stop, but this is not the beginning, but after reading the valid data, otherwise it would have been filtered to eliminate these characters from the buffer until the valid data to be read.

  getline: not the same, the data will be extracted before the termination symbol, with or without means, it is possible to extract the null value. This is no termination condition is determined to terminate the case.

From the following program to test.

  string a;
//    cin>>a;
    string b;
//    cin>>b;
    getline(cin,a);
    getline(cin,b);
    cout<<a<<b<<endl;

Second, determine the termination condition

  1. As already mentioned case reading is determined null.

  2. File terminator, the follow-up to see the supplement, I did it my IDE emulation terminator can not get out, it is embarrassing. Examples of the C ++ primer subsequent look under test.

Guess you like

Origin www.cnblogs.com/meikon/p/11518940.html