| | |
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:
Solved Threads: 2
here's my code, don't see why it doesn't print out
C++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Jun 2009
Posts: 333
Reputation:
Solved Threads: 2
C++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Jun 2009
Posts: 28
Reputation:
Solved Threads: 1
C++ Syntax (Toggle Plain Text)
#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(); }
Last edited by Nick Evan; 9 Days Ago at 4:20 am.
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?.
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?.
•
•
Join Date: Jun 2009
Posts: 28
Reputation:
Solved Threads: 1
Re: It may have minor errors as i have written directly..that you can solve easly
0
#7 Jun 30th, 2009
•
•
Join Date: Oct 2008
Posts: 47
Reputation:
Solved Threads: 12
C++ Syntax (Toggle Plain Text)
<div class="bbquote"> <div class="tlc"><div class="tli">•</div></div> <div class="blc"><div class="bli">•</div></div> <div class="trc"><div class="tri">•</div></div> <div class="brc"><div class="bri">•</div></div> <blockquote style="font-size:11px">cin>>passage;</blockquote> </div>
Use getline instead:
C++ Syntax (Toggle Plain Text)
getline (cin , passage)
C++ Syntax (Toggle Plain Text)
<div class="bbquote"> <div class="tlc"><div class="tli">•</div></div> <div class="blc"><div class="bli">•</div></div> <div class="trc"><div class="tri">•</div></div> <div class="brc"><div class="bri">•</div></div> <blockquote style="font-size:11px">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> </div>
C++ Syntax (Toggle Plain Text)
for (int i = 0 ; i < passage.length() ;i++) if (isspace(passage[i]) ) space++;
•
•
Join Date: Jun 2009
Posts: 333
Reputation:
Solved Threads: 2
gives me weird error of includes atleast one deprecated or antiquated header.
And it doesn't seem to capture # of spaces.
And it doesn't seem to capture # of spaces.
C++ Syntax (Toggle Plain Text)
#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; } }
Change <iostream.h> to <iostream>, that's the standard C++ way.
Also delete the line
And also change
Also put this line:
[edit]
Also (
) , you might want to put a
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.
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.
![]() |
Similar Threads
- how to find number of integers in a number (C++)
- Function to find number of files in a directory (C)
- Trouble deleting the correct number of spaces from a stream (C++)
- project plz who can help me (C++)
- Word break and line break in Rich Edit Control (C++)
- the number of times a word appears in a sentence(c prog) (C)
- find the number of incoming requests to a apache server (Linux Servers and Apache)
- Counting Spaces in a string (C++)
- Array without twice the same number? (C)
Other Threads in the C++ Forum
- Previous Thread: dynamic lib interface problem
- Next Thread: Seperating char array to seperate chars with a delimiter
Views: 984 | Replies: 18
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary c++ c++borland c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error file files filestream forms fstream function functions game givemetehcodez graph graphics gui homework iamthwee input int integer lazy linker list loop loops map math matrix member memory network newbie news number object objects opengl operator output parameter pointer pointers problem program programming project qt random read reading recursion recursive reference return server sort spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual win32 window windows winsock wxwidgets






