| | |
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: 282
Reputation:
Solved Threads: 1
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: 282
Reputation:
Solved Threads: 1
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; }
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: 26
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: 44
Reputation:
Solved Threads: 11
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: 282
Reputation:
Solved Threads: 1
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 niek_e; Jun 30th, 2009 at 10: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
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






