-
September 24th, 2011, 11:40 PM
#1
Miscalculating words/spaces
I was suppose to create a programs that reads out all characters, spaces, and words in a txt file, also a word would be anything followed by a space and also includes punctuation marks as words.
So far I have done everything that I am suppose to, but running the txt file under certain circumstances such as "space.space" in creates double words this is my code so far.
Code:
#include <fstream>
#include <iostream>
#include <conio.h>
#include <cctype>
using namespace std;
int main()
{
char ch,oldch;
int cnt_ch=0, cnt_words=0, spacenum=0;
ifstream poemfile;
poemfile.open("F:\\Super Computer programming\\Roses.txt");
if( poemfile.fail() )
{
cout<<"The File could not be found. Terminating\n";
getch();
exit(1);
}
ch = poemfile.get();
while( !poemfile.eof() )
{
cout<<ch;
cnt_ch++;
ch=poemfile.get();
if(ch == ' ')
{
spacenum++;
}
if ((ch == ' ') && (oldch != ' ') ) cnt_words ++;
if ((ch == '?') || (ch == '.') || (ch == '!'))
{
cnt_words++;
}
}
poemfile.close();
cout<<"\nThe file had "<<cnt_ch
<<" characters, "<<spacenum
<<" spaces, and "<<cnt_words+1
<<" words\n";
cout<<"\n\nNormal termination.\n";
getch();
return 0;
}
-
September 26th, 2011, 10:03 AM
#2
Re: Miscalculating words/spaces
This code
Originally Posted by raulian
Code:
if ((ch == '?') || (ch == '.') || (ch == '!'))
{
cnt_words++;
}
contradicts the statement
Originally Posted by raulian
...a word would be anything followed by a space and also includes punctuation marks as words.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
September 26th, 2011, 10:24 AM
#3
Re: Miscalculating words/spaces
Indentation shouldn't be random. Your code would be a lot easier to read and debug if it were formatted better.
You're checking for the value in oldch, but you're never setting it anywhere.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|