1

I'm trying to read integers from the console into my vector of ints. I want to keep reading in integers from a single line until the user clicks enter. I've been trying to use getline and stringstream, but it keeps looking for input after I press enter. Any solutions?

A high-level description: This program reads in numbers from the console and pushes them to the back of a vector. The vector is then sorted and two pointers are created to point to the back and front. The user can then enter a sum that the program will then search for in linear time by taking the sum of the two pointers. The pointers will then keep moving in one direction until they either find such a sum or determine that no such sum exists.

#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;

int findSum(vector<int> tempVec, int sum)
{
    int i;
    cout << "Sorted sequence is:";
    for ( i = 0; i < tempVec.size(); i++ )
        cout << " " << tempVec[i];
    cout << endl;

    int *ptr1 = &tempVec[0];
    cout << "ptr1 points to: " << *ptr1 << endl;
    int *ptr2 = &tempVec[tempVec.size() - 1];
    cout << "ptr2 points to: " << *ptr2 << endl;

    int count = 0;
    while ( ptr1 != ptr2 )
    {

        if ( (*(ptr1) + *(ptr2)) == sum )
        {
            cout << *(ptr1) << " + " << *(ptr2) << " = " << sum;
            cout << "!" << endl;
            return count;
        }
        if ( (*(ptr1) + *(ptr2)) < sum)
        {
            cout << *(ptr1) << " + " << *(ptr2) << " != " << sum;
            ptr1 = ptr1 + 1;
            cout << ". ptr1 moved to: " << *ptr1 << endl;
            count++;
        }
        else 
        {
            cout << *(ptr1) << " + " << *(ptr2) << " != " << sum;
            ptr2 = ptr2 - 1;
            cout << ". ptr2 moved to: " << *ptr2 << endl;
            count++;
        }
    }
    return -1;
}

int main()
{
    int ValSum;
    cout << "Choose a sum to search for: ";
    cin >> ValSum;


    vector<int> sumVector;
    int input;
    cout << "Choose a sequence to search from: ";
    while ( cin >> input != "\n" )
    {
        //getline(cin, input);
        if ( cin == '\0' )
            break;
        sumVector.push_back(input);
    }
    sort(sumVector.begin(), sumVector.end());


    int count = findSum(sumVector,ValSum);
    if ( count == -1 )
        cout << "\nThe sum " << ValSum << " was NOT found!" << endl;
    else 
    {
        cout << "\nThe sum " << ValSum << " was found!" << endl;
        cout << count + 1 << " comparisons were made." << endl;
    }
    sumVector.clear();
} 
Grigor
  • 11
  • 1
  • 3
  • 1
    Give the following answer's option 2 a gander. The only difference is you are using cin rather than a file: https://stackoverflow.com/a/7868998/4581301 – user4581301 Aug 03 '17 at 18:33
  • Where is `getline` and `stringstream` in your code? – Barmar Aug 03 '17 at 18:34
  • `getline` and `stringstream` is the right way to solve this, so you must have been doing it wrong. `getline` shouldn't be in the loop -- you do it once, then read from the `stringstream` in the loop. – Barmar Aug 03 '17 at 18:37
  • If you post that code we can help you fix it. – Barmar Aug 03 '17 at 18:38

2 Answers2

2

cin with the input operator >> eats all the whitespace before it gets to you, so input will never be \n.

But that's not even the biggest problem. cin >> input doesn't return what was just read, but rather a reference to the stream itself (see here). This means your code while ( cin >> input != "\n" ) isn't doing quite what you think (honestly that shouldn't even compile).

To read a line of integers from stdin into a vector, you would so something like this:

string line;
int num;
vector<int> v;

getline(cin, line);
istringstream iss(line);

while(istringstream >> num) {
    v.push_back(num);
}
scohe001
  • 15,110
  • 2
  • 31
  • 51
  • With this I am getting a Segmentation fault once I reach getline( cin, line ); – Grigor Aug 03 '17 at 19:05
  • @Grigor can you give an ideone (ideone.com) link with the code in question? It's probably something from another line of your code just before. – scohe001 Aug 03 '17 at 20:28
0

Use

std::vector<int> v;
std::string line;

// while (!v.empty()) { // optional check to make sure we have some input
std::getline(std::cin, line); // gets numbers until enter is pressed
std::stringstream sstream(line); // puts input into a stringstream
int i;

while (sstream >> i) { // uses the stringstream to turn formatted input into integers, returns false when done
    v.push_back(i); // fills vector
}
// }
joshwilsonvu
  • 2,569
  • 9
  • 20
  • You can get rid of the `while` loop by using `std::copy()` with `std::istream_iterator` and `std::back_inserter`, eg: `std::copy(std::istream_iterator(sstream), std::istream_iterator(), std::back_inserter(v));` – Remy Lebeau Aug 03 '17 at 19:49
  • While (get it?) that is an impressive use of STL, I have to say that it is much more difficult to understand for less experienced programmers. Sometimes those utilities lead to shorter and more readable code, sometimes they do not. – joshwilsonvu Aug 03 '17 at 20:01