0

In below code snippet, getline function executes one less than desired number of times.

For eg: n = 2; string 1:- stack string 2:- overflow

but only stack is stored and processing is done on "stack".

Any leads, what is wrong going on ?

'''

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

int main() {

    int n= 0;
    vector<string> istr{};
    cin >> n;
    string strin;
    while(n > 0)
    {
        //strin = "";
        getline(cin, strin);
        istr.push_back(strin);
        n--;
    }
    
    
    
    for(int i = 0; i < istr.size();i++)
    {
        string istreven = "";
        string istrodd = "";
        int nlen = istr[i].length();
        for(int j = 0; j <= nlen-1; j = j + 2)
            istreven += istr[i][j];
    
        for(int j = 1; j <= nlen-1; j = j + 2)
            istrodd += istr[i][j];
    
        string strret = istreven + "  " + istrodd;
        cout << strret << endl;
    }
    return 0;
}

'''

Thanks in advance for explaining the root cause or bug in getline function or what am i doing wrong ?.

pawopawan
  • 1
  • 1

0 Answers0