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

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 265
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

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

 
0
  #1
Jun 30th, 2009
here's my code, don't see why it doesn't print out
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. cout<<"enter a passage into input to find out how difficult it is to read\n";
  8. string passage;
  9. cin>>passage;
  10. char letter;
  11. int space=0;
  12. while(letter!='\n')
  13. {
  14. if(isspace(cin.peek())&&letter=='\n')
  15. {
  16. cout<<"from space";
  17. space=space++;
  18. }
  19. }
  20. cout<< space;
  21. return 0;
  22. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 265
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

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

 
0
  #2
Jun 30th, 2009
so it prints out but is an infinite loop, need to check if they do enter '\n' character how to do that?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 265
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

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

 
0
  #3
Jun 30th, 2009
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. cout<<"enter a passage into input to find out how difficult it is to read\n";
  8. string passage;
  9. cin>>passage;
  10. char letter;
  11. int space=0;
  12. while(letter!='\n')
  13. {
  14. cin.get(letter);
  15. while(letter=" ");
  16. //while(isspace(cin.peek())&&letter=='\n')
  17. {
  18. cin.get(letter);
  19. cout<<"the letter is"<<letter;
  20. space++;
  21. }
  22. }
  23. cout<< space;
  24. return 0;
  25. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 26
Reputation: nirav99 has a little shameless behaviour in the past 
Solved Threads: 1
nirav99 nirav99 is offline Offline
Light Poster

It may have minor errors as i have written directly..that you can solve easly

 
-2
  #4
Jun 30th, 2009
#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();
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,638
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 472
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: It may have minor errors as i have written directly..that you can solve easly

 
0
  #5
Jun 30th, 2009
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?.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 265
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

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

 
0
  #6
Jun 30th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 26
Reputation: nirav99 has a little shameless behaviour in the past 
Solved Threads: 1
nirav99 nirav99 is offline Offline
Light Poster

Re: It may have minor errors as i have written directly..that you can solve easly

 
0
  #7
Jun 30th, 2009
okay...
thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 44
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

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

 
0
  #8
Jun 30th, 2009
  1. <div class="bbquote"> <div class="tlc"><div class="tli">&bull;</div></div> <div class="blc"><div class="bli">&bull;</div></div> <div class="trc"><div class="tri">&bull;</div></div> <div class="brc"><div class="bri">&bull;</div></div> <blockquote style="font-size:11px">cin>>passage;</blockquote> </div>
cin would stop reading as soon as it encounters a space !
Use getline instead:
  1. getline (cin , passage)
  1. <div class="bbquote"> <div class="tlc"><div class="tli">&bull;</div></div> <div class="blc"><div class="bli">&bull;</div></div> <div class="trc"><div class="tri">&bull;</div></div> <div class="brc"><div class="bri">&bull;</div></div> <blockquote style="font-size:11px">while(letter!='\n')
  2. {
  3. cin.get(letter);
  4. while(letter=" "); //<<extra character makes it redundant;)
  5. //while(isspace(cin.peek())&&letter=='\n')
  6. {
  7. cin.get(letter);
  8. cout<<"the letter is"<<letter;
  9. space++;
  10. }
  11. }</blockquote> </div>
Instead ,
  1. for (int i = 0 ; i < passage.length() ;i++)
  2. if (isspace(passage[i]) )
  3. space++;
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 265
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

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

 
0
  #9
Jun 30th, 2009
gives me weird error of includes atleast one deprecated or antiquated header.
And it doesn't seem to capture # of spaces.
  1. #include<iostream.h>
  2.  
  3. using namespace std;
  4. int main()
  5. {
  6. string passage;
  7. int space;
  8. cout<<"enter the passage to find out readability index";
  9. cin>>passage;
  10. getline(cin,passage);
  11. for (int i = 0; i < passage.length(); i++)
  12. {
  13. if (isspace(passage[i]) )
  14. {
  15. space++;
  16. }
  17. cout<<"the number of spaces is"<<space;
  18. }
  19. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,879
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

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

 
0
  #10
Jun 30th, 2009
Change <iostream.h> to <iostream>, 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.
Last edited by niek_e; Jun 30th, 2009 at 10:52 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC