Sly Trash Panda
Sly Trash Panda

Reputation: 75

C++ cin not returning after hitting enter

So I want to be able to input something like: '6, -15, 12, 44, ...' etc. And have those integers be added into a vector. However when I enter said input and press enter it does nothing. If I then enter a letter and press enter, then enter another letter and press enter, it finally returns the desired result. If I enter more digits and press enter it will just continue to return nothing. Can someone please point me in the right direction of what is going wrong here? Thanks guys. I hope my explanation of the issue makes sense.

int main()
{

// The user inputs a string of numbers (e.g. "6, 4, -2, 88, ..etc") and those integers are then put into a vector named 'vec'.
std::vector<int> vec;

int value;
std::cin >> value;

if ( std::cin ) 
{

    vec.push_back( value );
    char separator;

    while ( std::cin >> separator >> value ) 
    {
        vec.push_back( value );
    }

}

std::cout << vec.size() << std::endl;
for ( int i = 0; i < vec.size(); i++ )
{
    std::cout << vec.at(i) << ' ';
}
std::cout << std::endl;
}

Upvotes: 1

Views: 2707

Answers (2)

R Sahu
R Sahu

Reputation: 206707

However when I enter said input and press enter it does nothing.

That is an incorrect interpretation of what the program does. Add some debugging output to your program and you will notice that the program is processing your input.

while ( std::cin >> separator >> value ) 
{
    std::cout << "Read separator: " << separator << std::endl;
    std::cout << "Read value: " << value << std::endl;

    vec.push_back( value );
}

If I then enter a letter and press enter, then enter another letter and press enter, it finally returns the desired result.

It seems that you want the program to stop reading input after you enter a line of text.

The you have written the program, the while loop does not stop when you press Enter. It waits for additional input in the next line.

By entering a letter, you have provided an input for separator. By entering another letter, you have put std::cin in a state of error. That's when the while loop breaks.

It seems to me that what you are really looking for is:

  1. Read a line of text. You can use std::getline for that.
  2. Read the numbers from the line of text. You can use std::istringstream for that.
  3. Output the data.

int main()
{
   // The user inputs a string of numbers (e.g. "6, 4, -2, 88, ..etc") and those integers are then put into a vector named 'vec'.
   std::vector<int> vec;

   std::string line;
   if ( getline(std::cin, line) )
   {
      std::istringstream str(line);

      int value;
      str >> value;
      vec.push_back( value );
      char separator;
      while ( str >> separator >> value ) 
      {
         vec.push_back( value );
      }
   }

   std::cout << vec.size() << std::endl;
   for ( int i = 0; i < vec.size(); i++ )
   {
      std::cout << vec.at(i) << ' ';
   }
   std::cout << std::endl;
}

Upvotes: 2

Jean-Baptiste Yun&#232;s
Jean-Baptiste Yun&#232;s

Reputation: 36431

Think the way your program parses your input. First you always require a comma after each number, do you need it at the end of the input? Second, how do you think your reading fails to end the loop?

This is why you need to enter two chars, one as a separator at the end, the second to make integer reading fail.

Upvotes: 0

Related Questions