954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

so i want to find out the number of spaces in a sentence, is this correct?

here's my code, don't see why it doesn't print out

#include <iostream>

using namespace std;

int main()
{
    cout<<"enter a passage into input to find out how difficult it is to read\n";
    string passage;
    cin>>passage;
    char letter;
    int space=0;
    while(letter!='\n')
    {
        if(isspace(cin.peek())&&letter=='\n')
        {
            cout<<"from space";
            space=space++;
        }
    }
    cout<< space;
    return 0;
}
lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

so it prints out but is an infinite loop, need to check if they do enter '\n' character how to do that?

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 
#include <iostream>

using namespace std;

int main()
{
    cout<<"enter a passage into input to find out how difficult it is to read\n";
    string passage;
    cin>>passage;
    char letter;
    int space=0;
    while(letter!='\n')
    {
        cin.get(letter);
        while(letter=" ");
        //while(isspace(cin.peek())&&letter=='\n')
        {
            cin.get(letter);
            cout<<"the letter is"<<letter;
            space++;
        }
    }
    cout<< space;
    return 0;
}
lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

void main()
{
clrscr();
char st[25];
cout<<"Enter String:- ";
gets(st);

int n; 
n = strlen(st);
int i;
int count=0;
for(i=0;i<=n;i++)
{
         while(str[i] == NULL)
         {
                   count=count+1;
          }
}

cout<<endl<<"Space:-"<<count;

getch();
}
nirav99
Light Poster
29 posts since Jun 2009
Reputation Points: -10
Solved Threads: 1
 

nirav99,

Use BB code tag. Source program must be surrounded with BB code tags: See # icon at toolbar and also read How to use bb code tags? .

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

basically i am trying to create a readibility index so i need the number of vowels, words and sentences. I am stuck on how to approach this.

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

okay...
thanks

nirav99
Light Poster
29 posts since Jun 2009
Reputation Points: -10
Solved Threads: 1
 
<blockquote>cin>>passage;</blockquote>

cin would stop reading as soon as it encounters a space !
Use getline instead:

getline (cin  ,  passage)
<blockquote>while(letter!='\n')
    {
        cin.get(letter);
        while(letter=" ");  //<<extra character makes it redundant;) 
        //while(isspace(cin.peek())&&letter=='\n')
        {
            cin.get(letter);
            cout<<"the letter is"<<letter;
            space++;
        }
    }</blockquote>

Instead ,

for (int  i = 0 ; i < passage.length() ;i++)
    if (isspace(passage[i]) )
         space++;
zalezog
Light Poster
47 posts since Oct 2008
Reputation Points: 53
Solved Threads: 13
 

gives me weird error of includes atleast one deprecated or antiquated header.
And it doesn't seem to capture # of spaces.

#include<iostream.h>

using namespace std;
int main()
{
string passage;
int space;
cout<<"enter the passage to find out readability index";
cin>>passage;
getline(cin,passage);
    for (int  i = 0; i < passage.length(); i++)
    {
        if (isspace(passage[i]) )
        {
            space++;
        }
        cout<<"the number of spaces is"<<space;
    }
}
lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

Change to , that's the standard C++ way.

Also delete the line cin >>passage . You're already taking in input with getline(), so no need to do it twice.

And also change int space; to int space = 0; . How can your program ++ if it doesn't know the variable's initial value?

Also put this line: cout<<"the number of spaces is"<<space; outside your for loop. You only want to display this messsage once right?

[edit]

Also ( :) ) , you might want to put a cin.get() at the end of your loop (right after you output your message. This keeps the console open for you to read what the output of your program is. If you don't put it there, your console window will disappear to fast.
You could also run it from a console window you've already opened, but my guess is that you do not yet know how this works.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

now it says comparison between unsigned and signed integer expressions
so do i say one is unsigned type?

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

I've editted my previous post, you might want to read it.

>> now it says comparison between unsigned and signed integer

It's right. This line : for (int i = 0; i < passage.length(); i++) compares an unsigned integer (returned by the function length(), with an signed integer (your int i)
To solve this warning, just change it to: for (unsigned int i = 0; i < passage.length(); i++)

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

thank you for your help. It works.

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

no no..
loop is not going infinite, loop is running equal to the numbers of character in a string which you have entered..
i have stored that character in integer variable " n " using " strlen(input string) " function.
and in case of "/n" i think it may not be consider as a member of string..

nirav99
Light Poster
29 posts since Jun 2009
Reputation Points: -10
Solved Threads: 1
 
no no.. loop is not going infinite, loop is running equal to the numbers of character in a string which you have entered.. i have stored that character in integer variable " n " using " strlen(input string) " function. and in case of "/n" i think it may not be consider as a member of string..

What the hell are you talking about?
Anyway: The problem was already solved, you should read all the replies before you make a reply yourself.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Mr xyz...
for your kind information..
i was solving his confutation and i think he may got it...
its non of your business..
and also mind your language, dont proud or shout to be "nearly a posting maven".
In past when u newly joined "daniweb" as i have joined yet, your situation might was like mine..
okay..

nirav99
Light Poster
29 posts since Jun 2009
Reputation Points: -10
Solved Threads: 1
 
nirav99
Light Poster
29 posts since Jun 2009
Reputation Points: -10
Solved Threads: 1
 
so it prints out but is an infinite loop, need to check if they do enter '\n' character how to do that?


no no..
loop is not going infinite, loop is running equal to the numbers of character in a string which you have entered..
i have stored that character in integer variable " n " using " strlen(input string) " function.
and in case of "/n" i think it may not be consider as a member of string..

nirav99
Light Poster
29 posts since Jun 2009
Reputation Points: -10
Solved Threads: 1
 
Mr xyz... for your kind information.. i was solving his confutation and i think he may got it... its non of your business..

It's niek_e not xyz. Why is this non of my business? I helped the OP to solve the problem?Mr xyz...
and also mind your language, dont proud or shout to be "nearly a posting maven".

"nearly a posting maven" is just a title I got for posting more then 2000 (?) posts. Just as your title is "newbie poster" because you haven't posted very much yet. Can't do anything about it, not my choice.In past when u newly joined "daniweb" as i have joined yet, your situation might was like mine..
okay..

No, my situation wasn't like yours at all. I read the rules for example, which clearly state:
- don't give away homework
- use code-tags
- use proper language/grammar
- don't bump:

s

^ What?

Why did you repost your original post? It still doesn't make much sense, even if you post it twice.
- What strlen() are you talking about?
- "/n" is not the same as '\n'
- a character is not a member of a string

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You