-1

I have a weird problem, so my instructor told us to create a simple program using vectors that will first ask the user how many integers they want to enter, and after the numbers were entered via a space as a delimiter the output should add 2 to each vector element.

Ex -

How many numbers are you going to enter = 4 (Lets assume the user enters 4)

     Enter the numbers = 11 45 71 24 (Lets assume the user entered these 4 numbers)

THE OUTPUT SHOULD BE THIS = 13 47 73 26 (with spaces added 2)

Since using vectors is a must, i know how to add add 2 with a normal integer vector output but when it comes with spaces i have no idea, so far the program i wrote will accept everything before the space into the vector and will add 2 to it.

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

int main()
{
    vector <int> userInput;
    int input;
    int number;
    int num1;
    cout << "How many numbers are you going to enter ? :";
    cin >> number;
    cout << "Enter the Numbers : " << endl;
    cin >> input;

    userInput.push_back(input);
    cout << "Vector Contains : ";
    for (unsigned int i = 0; i < userInput.size(); i++)
    {
        cout << userInput[i] + 2;
    }   
    system("pause>nul");
    return 0;
}
Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
high 0n C
  • 57
  • 1
  • 11
  • 2
    You currently input a single integer (`cin >> input`). You need to do this `number` times instead of just once. Then, try to understand how extracting an integer from a stream behaves when it encounters whitespace in the stream. – Useless Oct 19 '14 at 16:07
  • this is not splitting a string ?????? – high 0n C Oct 19 '14 at 16:07
  • By the way, adding 2 to every element is a one-liner in C++11: `std::for_each(begin(userInput), end(userInput), [](int &element) { element += 2; });` – Christian Hackl Oct 19 '14 at 16:53
  • There's actually no point in using a vector for this. – Ben Voigt Oct 19 '14 at 16:54
  • @ChristianHackl: Or he can just add two to each value before putting it into the vector. Or add two when printing. There's absolutely no reason to iterate the vector a third time to add two, or use a lambda callback. – Ben Voigt Oct 19 '14 at 16:55
  • @BenVoigt: That's true, at least for this small example code. OTOH, beginners should learn early on the distinction between contents and presentation; you could argue that in this case, the numbers gained from the user are the contents and should be stored in their original form, whereas printing them in some special way is a presentational concern. – Christian Hackl Oct 19 '14 at 16:59

3 Answers3

1

The reason you're only taking in one input is because you're only taking in one int from std::cin. The other ints are still remaining in the stream. You should do this:

while(/*we still have stuff to extract*/)
{
  std::cin >> input;
  userInput.push_back(input);
}

However, we still need to know when to stop. Since we're only expecting one line of input from the user, we can do std::cin.peek() != '\n' to check if we've reached the point where the user pressed the Enter key earlier.

Thank you for reading.

Nard
  • 1,006
  • 7
  • 8
1

The standard extraction operator for int expects whitespace as the separator between items so you don't need to do anything special there. Just read ints, put them in your vector, do the addition, and print out the results. Assuming you've already read n for the count, the real work could look something like this:

std::copy_n (std::istream_iterator<int>(std::cin),
             n, 
             std::back_inserter(your_vector));

std::transform(your_vector.begin(), your_vector.end(),
               std::ostream_iterator<int>(std::cout, " "),
               [](int i){ return i + 2; });
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

Try the following

#include <iostream>
#include <vector>
#include <cstdlib>

int main() 
{
    std::cout << "How many numbers are you going to enter ? :";

    size_t n = 0;
    std::cin >> n;

    std::vector<int> v( n );

    std::cout << "Enter " << n << " numbers : " << std::endl;

    int x;
    for ( size_t i = 0; i < n && std::cin >> x; i++ ) v[i] = x;

    for ( int x : v  ) std::cout << x + 2 << ' ';
    std::cout << std::endl;

    std::system( "pause>nul" );

    return 0;
}

If to input

4
11 45 71 24

the output will be

13 47 73 26 

Other approach is to use standard function std::getline and std::istringstream instead of the operator >>

For example

#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <sstream>
#include <limits>

int main() 
{
    std::cout << "How many numbers are you going to enter ? :";

    size_t n = 0;
    std::cin >> n;

    std::vector<int> v( n );

    std::cout << "Enter " << n << " numbers : " << std::endl;

    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

    std::string s;
    std::getline( std::cin, s );

    std::istringstream is( s );

    int x;
    for ( size_t i = 0; i < n && is >> x; i++ ) v[i] = x;

    for ( int x : v  ) std::cout << x + 2 << ' ';
    std::cout << std::endl;

    std::system( "pause>nul" );

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335