Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
// emtehani5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int jomleTokalame(string s)
{
    string::iterator i;
    int sentence;
    int count = 0;
    for (i = s.begin(); i != s.end(); i++)
    {
        if(*i != ' ' && *i != '.')
        {
            count++;
        }
    }
    return count;
}
int main()
{
    int n;
    string a[50];
    string s;
    int i=1,j,k=0,g;
    cout<<"plz enter the number of lines:"<<endl;
    cin>>n;
    for(int i=0 ; i<n-1 ; i++)
    {
        cin>>a[i];
        g=jomleTokalame(a[i]);
        cout<<g<<endl;
    }
    return 0;
}


I need a program that at the firs ask the number of lines that i want and then count the characters that are in the sentences.
but in my program when i ask the lines it take the number of lines for counting the words
for example when i write 2 lines it goes to sentence and read 2 words of that.:confused:
Posted
Updated 14-Feb-11 0:38am
v2

Reading a string with istream operator>> will terminate on the first whitespace, null character of end of line[^]. The way to read a complete line is to use std::getline(cin, a[i]).

This gets the lines correctly, but there is an issue with using cin >> n. The input terminates on the first non-digit. The next digit is not consumed. So when you go to read the first line it actually reads the an empty line. The way to correct this is to use cin.ignore(), which extracts and ignores characters from the input stream. When used with no parameters ignore will read a single character (expecting this to be the enter key).

The final problem with your code is the line reading loop, you need to use i<n rather than <code>i<n-1 to read in the correct number of lines.

My version of your code is:

C++
#include <iostream>
#include <string>
using namespace std;
int jomleTokalame(string s)
{
    string::iterator i;
    int sentence;
    int count = 0;
    for (i = s.begin(); i != s.end(); i++)
    {
        if(*i != ' ' && *i != '.')
        {
            count++;
        }
    }
    return count;
}
int main()
{
    int n;
    string a[50];
    string s;
    int i=1,j,k=0,g;
    cout<<"plz enter the number of lines:"<<endl;
    cin>>n;
    cin.ignore();
    for(int i=0 ; i<n ; i++)
    {
        getline(cin, a[i]);
        g=jomleTokalame(a[i]);
        cout<<g<<endl;
    }
    return 0;
}
 
Share this answer
 
v3
Comments
#realJSOP 14-Feb-11 7:59am    
You had nested PRE tags, so I deleted the outer-most one.
#realJSOP 14-Feb-11 8:00am    
Proposed as answer.
Hey let us talk, shall we. This is the 5th time you are asking the same exact question. Look here http://www.codeproject.com/script/Answers/List.aspx?kw=jomleTokalame[^]

I have no problem if you keep coming and asking question. In fact from my point of view it is very good to be insist on trying to get it. But

1. You have been given many answers, what have you done with them?
2. You were given some alternatives in some of the answers, what have you done with it?

If you keep throwing the the question while ignoring the answers provided to you, you want be solve it. Remember we are here to help you not do your work. Don't expect someone to give your the complete answer, but we will direct you to the right side. The rest is up to you.
 
Share this answer
 
Comments
ShilpiP 14-Feb-11 7:47am    
Nice catch :)
Nish Nishant 14-Feb-11 8:49am    
The questions are slightly different. It's the same program but he keeps running into new issues.
Yusuf 14-Feb-11 8:58am    
I noticed that too. But it makes more sense to continue the discussion the in same thread than start over IMHO
Nish Nishant 14-Feb-11 9:10am    
Yes, and no. His questions are on different issues really. The reason that's not obvious is that he copy/pastes his entire program each time instead of posting just the problem code.
Yusuf 14-Feb-11 10:00am    

Well, well. I am not sure about that. I may be reading it differently, but
1. The code he posted was suggested by Graham Shanks in http://www.codeproject.com/Answers/157437/counting-characteres-in-the-sentences.aspx#answer2
2. There is only 2 lines of code that is different from what is suggested to him.
3. In his Answer Graham pointed out which the OP is asking.

IMHO, this question should have been a follow up to Graham's suggestion. In addition, given a working code, it should not have been that difficult to figure it out. I bet, I was in his shoes, but at least I strive to understand what was spoon fed to me.

There is nothing wrong coming back and asking "I don't get it". I know there is a language barrier, but I am not taking about broken language, rather taken someone's suggestion, presenting it as self and then asking for more help. That is way I pointed out that he has been asking similar questions repeatedly.

just my 2cents.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900