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.
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.
Subreddit
Post Details
- Posted
- 4 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/cpp_questio...