zoukankan      html  css  js  c++  java
  • 输入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对象的箭头流向是向左的,为什么要向左呢?这是因为左边已经有值存在,要把这个值传输给屏幕显示,人的思维符合这种流向。

      

  • 相关阅读:
    (考研)散列表和hashcode和hashmap
    java基本数据类型和引用类型
    Java 中的语法糖(7/15整个周六上午总结)
    数据库第一天-数据库索引
    MonoBehaviour生命周期
    Unity基础知识
    SQLite3笔记
    cocos_js写文件
    UGUI_屏幕适配
    KMS算法
  • 原文地址:https://www.cnblogs.com/carlber/p/9828310.html
Copyright © 2011-2022 走看看