0

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;
}
3
Contributors
2
Replies
9
Views
3 Years
Discussion Span
Last Post by L7Sqr
0

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.

Edited by Moschops

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.