Thread: CIN.get

  1. #1
    Unregistered
    Guest

    Post CIN.get

    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

  2. #2
    Unregistered
    Guest
    post code

  3. #3
    Unregistered
    Guest
    use cin.getline(variable);

    Branden

  4. #4
    Unregistered
    Guest
    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] == ' ')

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    11
    if you're reading from a file you need to make it

    ifstream fin;

    fin.open(filename);

    fin.get(charName);

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.get() Troubles
    By yukapuka in forum C++ Programming
    Replies: 19
    Last Post: 06-04-2008, 01:30 PM
  2. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 05:32 PM
  3. cin.get();
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2005, 07:51 AM
  4. cin.get() aint working.
    By Blips in forum C++ Programming
    Replies: 19
    Last Post: 01-17-2005, 06:55 PM
  5. Problems using while loop with cin.get()
    By Arooj in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2004, 01:58 AM