How to Signify No More Input for String Ss in the Loop While (Cin >> Ss)

How to signify no more input for string ss in the loop while (cin ss)

Your code is correct. If you were interactively inputting, you would need to send a EOF character, such as CTRL-D.

This EOF character isn't needed when you are reading in a file. This is because once you hit the end of your input stream, there is nothing left to "cin"(because the stream is now closed), thus the while loop exits.

c++ when will while(cin s) stop

while (cin >> s) { ... } will loop as long as the input is valid. It will exit the loop when the attempted input fails.

There are two possible reasons for failure:

  1. Invalid input
  2. End of file

Assuming that the input itself is valid, in order to terminate the loop the input stream has to reach the end.

When the input is actually a file, recognizing the end is easy: when it runs out of characters it's at the end of the file. When it's the console, it's not so easy: you have to do something artificial to indicate the end of the input.

Do that, you have to tell the terminal application (which controls the console) that there is no more input, and the terminal application, in turn, will tell your program that it's at the end of the input.

The way you do that depends on the terminal application, which is typically part of the operating system.

  • On Windows, ctrl-Z tells the terminal application that you're at the end of your input.
  • On Unix systems, it's ctrl-D.

While loop to continue taking user input from stringstream after calling print function

stringstream.clear() is used to reset error flags and not clear the stream, although it might be giving you results but it's meaning shouldn't be changed.

#include<cstdlib>

char ch;
string line;
string buf;
bool check=true;

cout<<"Enter input\n";

while(1)
{
if(check == true)
{
cin>>line;
}
else break;
istringstream is(line);

// This will split the line based on whitespace
while(std::getline(is, buf, ' '))
{
cmd.push_back(buf);

// Checking if vector has 5 elements

if(cmd.size() == 5)
{
// Call function summary
summary(cmd[0], cmd[1], atoi(cmd[2].c_str()), atoi(cmd[3].c_str()),
atoi(cmd[4].c_str()), TeamData);

// Clear the vector list now
cmd.clear();

cout<<"Do you want to continue, enter y then"<<endl;
cin>>ch;
if(ch == 'y')
{
cout<<"Enter input"<<endl;
}
else
{
check=false;
break;
}
}
}
}

atoi function would convert your string to integer type.

Cin in a while loop

Hitting Enter produces either a CR or LF or both, depending on your platform. This is a valid input so satisfies the condition to continue the while loop. You will need to either explicitly test for these characters at the beginning of your input or use Ctrl-C to break out of the loop.

As a readability issue, I would include braces around the code that you want in the loop. What you have there is valid C++ since without braces the while will loop on the next statement and the whole if conditional is a single statement. Practice putting them in now even on single line loops and you'll save yourself some headache debugging in the future.

How can I include '\n' into the while (cin x) idiom?

Using >> operator directly won't work, because as you say it skips whitespace including newlines.

What you can do is to read a single line using std::getline, and then you can read all input from the line with std::stringstream.

std::string line;
if (std::getline(std::cin, line)) {
std::istringstream ss(line);
int x;
while (ss >> x) {
//....
}
}

What is the best way to take an input line and add the words in the line to a vector in C++?

A nice way to split each word of the string and store them into a vector would be taking the help of std::istringstream (from sstream library):

#include <iostream>
#include <vector>
#include <sstream>

int main(void) {
std::string input;
std::string temp;
std::vector<std::string> words;

std::getline(std::cin, input);

// Using input string stream here
std::istringstream iss(input);

// Pushing each word sep. by space into the vector
while (iss >> temp)
words.push_back(temp);

for(const auto& i : words)
std::cout << i << std::endl;

return 0;
}

As a sample test case, you can see:

$ g++ -o main main.cpp && ./main
Hello world, how are you?
Hello
world,
how
are
you?


Related Topics



Leave a reply



Submit