代码之家  ›  专栏  ›  技术社区  ›  jkeys

STD的规则是什么:C++中的CIN对象?

  •  0
  • jkeys  · 技术社区  · 17 年前

    我正在编写一个小程序,供我个人使用,以学习C++和它的功能,一个MLA引文生成器(我写一篇大论文,有几十个引文)。

    由于缺乏更好的方法(我不理解类或在main中使用其他.cpp文件,所以不必麻烦告诉我,我会在有更多时间时处理),我正在为每种类型的引用编写一个函数。如果我有更多的时间,我可能会将其分解为每个重用代码的函数。

    我的问题是:std::cin对象是如何工作的?我目前正在阅读std::cin>&燃气轮机;对于字符串,我希望是单个单词,而getline(std::cin,string)则希望是带有空格的字符串。但是,我没有得到正确的输出。我只是想知道std::cin是如何工作的,以及为什么我总是意外地跳过一些输入(例如,它跳过网页,而不是给我一个输入的机会)。

    void webCit() {
        std::cout << "Leave any unknowns blank.\n";
    
        std::cout << "Author last name: ";
        std::string lastName;
        std::cin >> lastName;
    
        if (lastName.size() != 0) {
            lastName = lastName + ", ";
        }
    
    
        std::cout << "Author first name: ";
        std::string firstName;
        std::cin >> firstName;
    
        if (firstName.size() != 0) {
            firstName = firstName + ". ";
        }
    
    
        std::cout << "Article title: ";
        std::string articleTitle;
        getline(std::cin, articleTitle);
    
        if (articleTitle.size() != 0) {
            articleTitle = "\"" + articleTitle + ".\" ";
        }
    
        std::cout << "Title of web page: ";
        std::string pageTitle;
        std::cin >> pageTitle;
    
        if (pageTitle.size() != 0) {
            pageTitle = pageTitle + ". ";
        }
    
        std::cout << "Publication date: ";
        std::string publicationDate;
        getline(std::cin, publicationDate);
    
        if (publicationDate.size() != 0) {
            publicationDate = publicationDate + ". ";
    
        }
    
        std::cout << "Web address: ";
        std::string webAddress;
        getline(std::cin, webAddress);
    
        webAddress = "<" + webAddress + ">. ";
    
        std::cout << "Date accessed: ";
        std::string dateAccessed;
        getline(std::cin, dateAccessed);
    
        if (dateAccessed.size() != 0) {
            dateAccessed = dateAccessed + ". ";
        }
    
        std::string citation =
            lastName + firstName + articleTitle + pageTitle + publicationDate + webAddress + dateAccessed;
    
        std::cout << citation; //TEST; remove after
    }
    

    编辑:I/O

    Leave any unknowns blank.
    Author last name: Hooked
    Author first name: Jerbear
    Article title: Title of web page: title
    Publication date: Web address: www.win.com
    Date accessed: 4/29/09
    Hooked, Jerbear. Title. <www.win.com>. 4/29/09. 
    

    2 回复  |  直到 6 年前
        1
  •  7
  •   Simon Buchan    17 年前

    这里发生的事情是 std::cin >> firstName; 只读到 但不包括 第一个空白字符,包括换行符(或 '\n' )当你按enter键时,当它到达 getline(std::cin, articleTitle); “\n” std::cin getline() 马上回来。

    // cin = "Bloggs\nJoe\nMan of Steel, Woman of Kleenex\n"
    std::cin >> lastName;
    std::cin >> firstName;
    // firstName = "Joe", lastName = "Bloggs", cin = "\nMan of Steel, Woman of Kleenex\n"
    getline(std::cin, articleTitle);
    // articleTitle = "", cin = "Man of Steel, Woman of Kleenex\n"
    

    添加' std::cin >> std::ws ' ( ws w 白 s

    std::cin >> firstName >> std::ws;
    getline(std::cin, articleTitle);
    

    std::cin >> firstName;
    getline(std::cin >> std::ws, articleTitle);
    
        2
  •  4
  •   Jesse Beder    17 年前

    当你使用 >> cin 一直读取到下一个空格字符,但它不处理空格。所以当你有

    std::cin >> str1;
    std::getline(std::cin, str2);
    

    相反,如果您计划使用 getline 一次之后 operator >> std::cin.ignore() 在你打电话之前先吃了这条新线 getline .

    std::cin >> str1;
    std::cin >> str2;
    

    因为第二个调用将忽略所有前导空格。