I want to count how many times a quoted string appears in a string.
Today "is" a new day "because" it is "12" am.
A string like this should evaluate to 3. I don't know why my code prints 0.

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

bool isQstr(string str);
int main()
{
    string strn;
    int numdQuotes = 0;
    int total = numdQuotes/2;
    cout <<"enter any str "<<endl;
    getline(cin,strn);
    cout<<isQstr(strn)<<endl;
    int len = strn.length();

    for (unsigned int i=0; i<len; i++) {
        if(isQstr(strn) == true) {
        numdQuotes++;
        }
    }
    cout<<"Total number of double quotes is "<<total<<endl;
    return 0;
}

bool isQstr(string str){
    int len = str.length();
    for (unsigned int i=0;i<len;i++){
        if(str.at(i)=='"')
            return true;
    }
    return false;
}

Here, you set the variable total:

int numdQuotes = 0;
int total = numdQuotes/2;

This sets it to the value 0, because at this moment, numdQuotes/2 is zero.

Your code never touches the variable total again, so it remains zero forever, so when you output it to screen, it's zero.

To add to what Moschops mentions, you can either assign total = numdQuotes or just output numdQuotes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.