-1
//header  
#include <bits/stdc++.h>
using namespace std;
    
int main(int argc, char** argv)
{
    string s,s1=" ";
    int i,j,flag=0,flag1=0,count=0;
    getline(cin,s);
    for(i=0;i<s.length();i++){
        if(s[i]==' '){
            flag=i;
            for(j=flag1;j<flag;j++){
               s1=s1+s[j];
               flag1=flag+1;
            }
            //cout<<s1<<" ";--uncommenting this works but below if statement not working
            if(s1=="a"||s1=="A"||s1=="an"||s1=="An"){
                count++;
            }
        }
        s1=" ";
    }
    cout<<count;
}

If statement taking nothing and just giving count value as 0

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
  • 1
    Some recommended reading: [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – ChrisMM Jul 23 '20 at 12:58
  • can you add the input sample – shankar upadhyay Jul 23 '20 at 14:49
  • it is showing 0 in every output – Anurag Nema Jul 26 '20 at 17:28

2 Answers2

0

Your s1 is initialized to a whitespace character and it is preventing it from matching with the strings in the if statement, none of which don't have whitespace characters.

Remove the extra two whitespaces and initialize s1 to empty strings.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

In a better way and to make the life easier, you could do the same stuff with the help of std::stringstream and std::vector:

#include <iostream>
#include <sstream>
#include <vector>

int main(void) {
    std::string string;
    std::string temp;
    std::vector<std::string> words;
    int counter = 0;

    std::cout << "Enter a string: ";
    std::getline(std::cin, string);

    // a string stream to separate each word
    std::stringstream ss(string);

    // pushing the separated words (by space) into a dynamic array
    while (ss >> temp)
        words.push_back(temp);

    // comparing each array of 'words' (where each separated words are stored
    // as a type of 'std::string'
    for (size_t i = 0, len = words.size(); i < len; i++)
        if (words.at(i) == "a" || words.at(i) == "an" || \
                words.at(i) == "A" || words.at(i) == "An")
            counter++;

    // if no articles found, i.e. the counter is zero
    if (counter == 0) {
        std::cout << "Sorry, there were no articles found.\n";
        return 0;
    }

    // otherwise...
    std::cout << counter << " number of articles were found!\n";

    return 0;
}

A sample output is as follows:

Enter a string: A great scientist, Newton experimented something on an apple.
//             _^___________________________________________________^^_______
2 number of articles were found!

Also, avoid the usage of bits/stdc++.h, it's neither the part of Standard C++ nor a good convention to use as directed in @ChrisMM's comment in this post.

Using using namespace std for smaller programs where the chance of naming ambiguity is negligible, is good, but better avoid in large programs.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • Thankyou for your advice and giving time for answering me, I will do it as you said but I am still wondering why my if statement not working. – Anurag Nema Jul 26 '20 at 17:17