Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

2
Confused about terminating std::cin
Post Flair (click to view more posts with a particular flair)
Post Body

I can tell to terminate the std::cin an invalid character like feeding string when int expected or ctrl d or crtl z depending on the platform but why not just a simple enter won't do the job? Is asking user to enter an invalid or a special character to end the input plausible in real world applications? How is this situation handled in professional applications? An example from c primer below:

#include <iostream>

int main()
{
    int currVal = 0;
    int val = 0;

    if (std::cin >> currVal)
    {
        int cnt = 1;
        while (std::cin >> val)
        {

            if (currVal == val)
            {
                  cnt;
            }
            else
            {
                std::cout << "the number " << currVal << " occurs " << cnt << " times " << std::endl;
                currVal = val;
                cnt = 1;
            }
        }
        std::cout << "the number " << currVal << " occurs " << cnt << " times " << std::endl;
    }
    return 0;
}

I have to do ctrl d twice to program to finish for the input of 11 11 22 22 if I don't press enter first. Any suggestion is welcome, thank you for your time.

Author
User Disabled
Account Strength
0%
Disabled 1 month ago
Account Age
4 years
Verified Email
Yes
Verified Flair
No
Total Karma
187
Link Karma
103
Comment Karma
84
Profile updated: 7 hours ago
Posts updated: 1 year ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
4 years ago