943,922 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1268
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 30th, 2009
0

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

Expand Post »
here's my code, don't see why it doesn't print out
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009
Jun 30th, 2009
0

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

so it prints out but is an infinite loop, need to check if they do enter '\n' character how to do that?
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009
Jun 30th, 2009
0

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

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009
Jun 30th, 2009
-2

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

C++ Syntax (Toggle Plain Text)
  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; Mar 10th, 2010 at 5:20 am.
Reputation Points: -10
Solved Threads: 1
Light Poster
nirav99 is offline Offline
28 posts
since Jun 2009
Jun 30th, 2009
0

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

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?.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jun 30th, 2009
0

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

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.
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009
Jun 30th, 2009
0

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

okay...
thanks
Reputation Points: -10
Solved Threads: 1
Light Poster
nirav99 is offline Offline
28 posts
since Jun 2009
Jun 30th, 2009
0

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

C++ Syntax (Toggle Plain Text)
  1. <div> <div class="quote"> <div class="quote_header"> Quote ... </div> <blockquote class="quote_body"> cin>>passage; </blockquote> </div> </div>
cin would stop reading as soon as it encounters a space !
Use getline instead:
C++ Syntax (Toggle Plain Text)
  1. getline (cin , passage)
C++ Syntax (Toggle Plain Text)
  1. <div> <div class="quote"> <div class="quote_header"> Quote ... </div> <blockquote class="quote_body"> 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> </div>
Instead ,
C++ Syntax (Toggle Plain Text)
  1. for (int i = 0 ; i < passage.length() ;i++)
  2. if (isspace(passage[i]) )
  3. space++;
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Jun 30th, 2009
0

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

gives me weird error of includes atleast one deprecated or antiquated header.
And it doesn't seem to capture # of spaces.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009
Jun 30th, 2009
0

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

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 10:52 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: dynamic lib interface problem
Next Thread in C++ Forum Timeline: Seperating char array to seperate chars with a delimiter





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC