0

I want to enter five strings and push them into a vector. But this doesn't work for me. If I do it like with the bigger code below, I get a vector size of 5 and can only enter 4 strings. But if I change it so it says

for(int n=0; n<=5; n++)

This results in me being able to input 5 strings, but if I call investments.size() it returns a value of 6. And if I print every element it will start by printing an empty string first, then print the 5 that I've entered.

So it seems that when my loop finishes I get an extra input and it pushes it into the vector. Is it because of the getline() line? Will this run and input an empty string into my vector as my loop finishes?

int main(){
    using namespace std;
    vector<string> investments;
    vector<double> andel;
    double procent;
    int kapital;

    cout << "Hur stort kapital ska investeras?" << endl;
    cin >> kapital;
    cout << investments.size() << endl;
    cout << "Skriv in dina fonder som du ska spara i. En i taget." << endl;
    for(int n=0; n<5; n++){
        string bolag;
        getline(cin, bolag);
        investments.push_back(bolag);
    }
    cout << "size of investments: " << investments.size() << endl;
    for(string s : investments) cout << s << endl;

}

The problem isn't that cin is skipping input. It's rather that it's adding input where there is none.

Gurkmeja101
  • 592
  • 2
  • 9
  • 24
  • The first getline gets whatever remains on the line where you input the capital, you first need to finish that line. – Marc Glisse Aug 28 '15 at 07:57
  • possible duplicate of [cin and getline skipping input](http://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input) – Svalorzen Aug 28 '15 at 07:58
  • if you know how many string you need, reserve place in advance and access the element using the index. –  Aug 28 '15 at 08:01
  • @Atomic_alarm Yeah, an array would be better if I knew in advance how many elements I needed. You are correct. But I'm doing it with a vector to be able to expand and add x-amounts of elements for different users :) – Gurkmeja101 Aug 28 '15 at 08:03
  • @Svalorzen The problem isn't that it's skipping input. Rather the opposite, it's adding and pushing input where there is none. – Gurkmeja101 Aug 28 '15 at 08:07
  • @Gurkmeja101 If you read that question you will understand. Your first getline is picking up the '\n' left after you read `kapital`. Thus you will always have a first entry which is an empty string. Inspect the string if you want to be sure. That question explains how to fix the problem. – Svalorzen Aug 28 '15 at 08:23
  • @Gurkmeja101, I do not suggest you use an array. I offer pre-allocate storage for a known number of elements. Slightly changed your code: http://ideone.com/vV5cxb –  Aug 28 '15 at 08:24

3 Answers3

1

for(int n=0; n<=5; n++)

Your first for loop will push 6 strings [0;5] (inclusive borders)

But it will work fine for me.

Hur stort kapital ska investeras?
0
Skriv in dina fonder som du ska spara i. En i taget.
size of investments: 5

is the output, when I compile your code above.

hellow
  • 12,430
  • 7
  • 56
  • 79
  • That's wierd. Because if I input something like you did. This is what I get: `Hur stort kapital ska investeras? 0 Skriv in dina fonder som du ska spara i. En i taget. 1 2 3 4 size of investments: 5 1 2 3 4` – Gurkmeja101 Aug 28 '15 at 07:58
1

If you print something inbetween the investments' names you'll notice that the first one is empty.
This is because after you've read the capital, there's a newline still in the stream after it, and your first getline reads only up to that newline.

You can fix this by inserting

cin.ignore(100, '\n');

immediately after you read the capital.

(BTW: mixing Swedish and English identifiers gets confusing very quickly as a program grows.)

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • Thank you so much! That fixed it :) I know it can get confusing, I should get better at it. Thank you for telling me that aswell! – Gurkmeja101 Aug 28 '15 at 08:11
0

You can fix this issue by changing

getline(cin, bolag);

to

cin >> bolag;

Alternatively, before you first use getline(cin, bolag); you could call cin.ignore(1); in order to flush the last newline.

JHobern
  • 866
  • 1
  • 13
  • 20
  • But if some of the strings are multiple words, won't `cin` just grab the first one? Where should I call `cin.ignore(1)`? – Gurkmeja101 Aug 28 '15 at 08:02