输入cin对象的用法

#include<iostream>
using namespace std;
int main()
{
    int carrots ;
    cout << "How many carrots do you have? " << endl;
    cout << "please enter number:" ;
    cin >> carrots;
    cin.get();
    cout << "Here are two more.";
    carrots = carrots + 2;
    cout << "Now you have " << carrots << " carrots." << endl;
    cin.get();
    return 0;
}   

  程序中使用到了两个cin.get()函数,这样才能在屏幕上看到输出。为什么要用到两个呢?

  第一个cin.get()语句在您输入数字并按enter键时读取输入,而第二条cin.get()语句让程序暂停,直到您按下按键才会关闭屏幕

  cin对象的箭头流向是向右的>>,为什么会向右呢?这是因为从键盘输入的值会赋给carrots,这符合人的思维流程。如果不加第一个cin.get()函数的话,它会在输入字符后立马关闭屏幕。

  cout对象的箭头流向是向左的,为什么要向左呢?这是因为左边已经有值存在,要把这个值传输给屏幕显示,人的思维符合这种流向。

  

猜你喜欢

转载自www.cnblogs.com/carlber/p/9828310.html
今日推荐