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

Thread Solved

Join Date: Jun 2009
Posts: 333
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 2
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz

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: 333
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 2
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz

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: 333
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 2
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz

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: 28
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
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4.  
  5. void main()
  6. {
  7. clrscr();
  8. char st[25];
  9. cout<<"Enter String:- ";
  10. gets(st);
  11.  
  12. int n;
  13. n = strlen(st);
  14. int i;
  15. int count=0;
  16. for(i=0;i<=n;i++)
  17. {
  18. while(str[i] == NULL)
  19. {
  20. count=count+1;
  21. }
  22. }
  23.  
  24. cout<<endl<<"Space:-"<<count;
  25.  
  26. getch();
  27. }
Last edited by Nick Evan; 9 Days Ago at 4:20 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 3,922
Reputation: adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future adatapost has a brilliant future 
Solved Threads: 704
Moderator
adatapost adatapost is offline Offline
Senior Poster

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?.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 333
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 2
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz

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: 28
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: 47
Reputation: zalezog is on a distinguished road 
Solved Threads: 12
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: 333
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 2
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz

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: 3,314
Reputation: Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute Nick Evan has a reputation beyond repute 
Solved Threads: 337
Moderator
Featured Poster
Nick Evan's Avatar
Nick Evan Nick Evan 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 Nick Evan; Jun 30th, 2009 at 9:52 am.
Reply With Quote Quick reply to this message  
Reply

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




Views: 984 | Replies: 18
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC