trying to read in from a file and check each character from the file to tell how many words are in each setence in the file.
told to use cin.get
but can't get it to work right
trying to read in from a file and check each character from the file to tell how many words are in each setence in the file.
told to use cin.get
but can't get it to work right
post code
use cin.getline(variable);
Branden
declare an array of strings. (what type of array depends on what type of string you use)
declare an ifstream and associate it with your file
while not end of file
use getiline with the stream to read line into file one line at a time. You need to know the longest potential line in order to do this safely.
declare an int to use in loop
declare an int to act as counter and initialize to zero.
use a loop to cycle through each string in the array of strings
if using cstyle strings look at each char to see if it is a space
if using STL string you could try using something like find()
each time you find a space increment the counter
example of call to getline()
fin.getline(string, 81, '\n');
example of evaluating char stored in array of cstyle strings
if(string[i][j] == ' ')
if you're reading from a file you need to make it
ifstream fin;
fin.open(filename);
fin.get(charName);
Assuming sentences end with a full stop and each sentence is less than 255 characters this will count the number of words in one sentence (from cin) -
Code:#include <iostream> using namespace std; int main() { char buff[256]; int words=0; cin.get(buff,256,'.'); for(int i=0;i<strlen(buff);i++) { if (buff[i]==' ') words++; } //add final word words++; cout << "Number of words: " << words << endl; return 0; }
zen