What are the rules of a std :: cin object in C ++?

I am writing a small program for personal use for the practice of teaching C ++ and for its functionality, an MLA citation generator (I am writing a large article with dozens of links).

Due to the lack of a better way to do this (I don’t understand the classes or use other .cpp files inside your main system, so don’t tell me, I will work on it when I have more time), I write a function for each type of citation. I could break this down into a function for each reusable code if I have more time.

My question is: how does the std :: cin object work? I am currently reading with std :: cin >> for strings that I expect to be separate words, and getline (std :: cin, string) for strings with spaces. I am not getting the correct conclusion, though. I just want to know how std :: cin works and why I unexpectedly miss some input (for example, it skips a web page instead of giving me a chance to enter it).

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
}

UPDATE: 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. 

As you can see, something is going wrong because my data is being skipped.

0
source share
2 answers

, , std::cin >> firstName; , , ( '\n'), , , 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 hite s ) getline() :

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

, , :

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

>>, cin , . ,

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

, - .

, getline operator >>, std::cin.ignore(), getline.

: , ,

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

.

+3

All Articles