2

In a problem were i have to take n number of strings as input and count the ones containing a given substring(case insensitive).

Here's my code:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include<string>
using namespace std;
int main()
{
    std::string str2 = "hello";
    std::string str3 = "HELLO";
    short int n,count=0,i;
    cin>>n;
    std::string str1[n];
    for(i=0;i<n;i++)
    {
        std::getline(std::cin,str1[i]);   //here getline is taking only n-1  inputs
        std::size_t found1=str1[i].find(str2);
        std::size_t found2=str1[i].find(str3);
        if(found1!=std::string::npos || found2!=std::string::npos)
            count++;
    }
    cout<<count;
    return 0;
}

Since i cant use cin as string includes spaces or cin.getline() as have to use string type in place of char[].
Problem with my code is std::getline() is only taking n-1 inputs.Cant figure out why?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
wrangler
  • 3,454
  • 1
  • 19
  • 28
  • 2
    Perhaps the last line of input is not terminated with a newline. Different standard library implementations handle this in their own way. I recall an issue with the IBM compiler many years ago where you could never get the last line in a file unless it ended in newline. – paddy Aug 24 '15 at 00:30
  • So much code for what's essentially a single `generate()` call. Getting paid by the character? – Blindy Aug 24 '15 at 00:53
  • You forget about the Enter key you pressed when entering `n` – M.M Aug 24 '15 at 01:54
  • Does this answer your question? [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – user202729 Jan 31 '21 at 03:16

2 Answers2

4

The first getline after cin reads the remainder of the line, which is probably empty. This is why when reading user input, it is usually better to use getline and process input using code.

After cin >> n the input stream is positioned just after the number n. You can use a getline just to read the newline and then throw it away to position to the start of the next line.

stark
  • 12,615
  • 3
  • 33
  • 50
  • the question dosnt demands a newline after cin>>n. And is there nothing else i could do using STL?? – wrangler Aug 24 '15 at 00:41
0

This code should work

#include <iostream>
#include <string>

using namespace std;

int main() {
    int n = 0, count = 0;
    cin >> n;

    do {
        string str;
        getline(cin, str);

        if (str.find("HELLO") != string::npos || str.find("hello") != string::npos) {
            count++;
        }
    } while (n-- && n >= 0);

    cout << count << endl;
    return 0;
}
CodeBeginner
  • 329
  • 1
  • 7
  • Your code doesn't fix the problem stark's answer explains (and it leaves other nasty problems - no check for success of `cin >> n` nor `getline`), and introduces new issues: it doesn't cope properly with `0` or negative `n` as the test happens after a loop iteration, and for `INT_MIN` it will do a lot of iterations. – Tony Delroy Aug 24 '15 at 03:27