-
Coding Troubles
Okay I have code that is supposed to get a number of words in a text file....for some reason it will half of the number of words in the text file....There are actually 6338 words in this text file...I only come up with exactly half which is 3169...I don't see anything wrong with the code so maybe you guys can figure it out
Code:
void CountNumberOfWordsAndNumberOfCharacter()
//Pre:Get Number of Words and Number of characters in Text file
//Post:Output Number of Words and Number of Characters
{
int Choice = 0;
apstring Words;
long int TotalWords = 0;
char Characters;
long int TotalCharacters = 0;
cout << endl;
cout << "Enter your choice count number of words and characters" << endl;
cout << endl;
cout << "[1] Words.txt" << endl << endl;
cout << "[2] melville.txt" << endl << endl;
cout << "[3] caesar.txt" << endl << endl;
cin >> Choice;
while(Choice != 1 && Choice != 2 && Choice != 3)
{
cout << "Input Error, please choose again." << endl;
cin >> Choice;
}
switch(Choice)
{
case 1:
{
ifstream instream("C:\\STR\\words.txt");
while(instream>>Words)
{
TotalWords++;
}
while(instream>>Characters)
{
TotalCharacters++;
}
cout << "Total Amount Of Words : " << TotalWords << endl;
cout << "Total Amount of Characters : " << TotalCharacters << endl;
break;
}
}
}
-
Well the characters are a problem just one I am not dealing with right now...but the words for some reason is only half of what it should be and I can't find any logic problems with the code....yeah help with the characters if you want cause I'll do that afterwards..right now words are my main concern
-
Okay I figured out the word thing...I used MS word to count the words and yeah that was wrong...yeah so I used someone elses code to see how many words were there and I was right...now on to the characters....can someone tell me how I can read the file again from the begining cause it seems that the program is not reading the characters...I am assuming it is at the end of the file so how can I bring it back to the begining of the file?
-
BTW I am a newbie C++ programmer if you can see....so I don't know too mcuh about the language.
-
Never mind I think I figured out the character issue by just declaring another thing called
Code:
ifstream instreamTwo("C:\\STR\\melville.txt");
right before the charcter loop so it should restart