Thread: Help with strings

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    13

    Help with strings

    I am trying to write a program that reads in a line of text and counts the number of words and the number of instances of each letter. After you type in a line of text you hit return and it should give your output. Sometimes if there are some small words, three or less letters, you will need to hit return a second time. It doesn't count all small words. Also the letter count is always the same:

    The number of each letter you typed in is:
    b 134520536
    f 4
    h 134520536
    l 134520536
    p 134520536
    q 10
    r 4
    x 134520536


    I had it print out 'c' right after the cin.get statement and it seems to be reading in only every other letter. Any help will be greatly appreciated. Thank you in advance.

    Code:
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    void introduction();
    //Explains what the program does.
    
    void input_count_output(int& num_words);
    //Asks user to input text from the keyboard and then counts
    //and outputs to the screen the number of words and the number
    //of each letter typed in.
    
    int main()
    {
    	int num_words = 0;
    	introduction();
    	input_count_output(num_words);
    	return 0;
    }
    
    //uses iostream
    void introduction()
    {
    	cout << "This program will ask the user to input a line of text\n"
    	     << "and then will output the number of words and the number of\n"
    	     << "each type of letter.\n"
    	     << endl;
    }
    
    //uses iostrem
    //uses string
    //uses cctype
    void input_count_output(int& num_words)
    {
    	char c;
    	int letter_count[26];
    	cout << "Please enter a line of text, then press return:\n";
    	do
    	{
    	 	cin.get(c);
    		if (!cin.get(c))  //breaks if character is not able to be read in
    			break;
      		if (isalpha(c))
    		{
       			if (isupper(c))
         	 			c = tolower(c);
    		}
    		++letter_count[c - 'a'];
    		if (c == '\n' || c == '\t' || c == '"' || c == ',' || c == ';' || c == ':' || c == '.' || c == '?' || c == '!' || c == ' ')
    			++num_words;
    	}while(c != '\n');
    	
    	//now for output
    	
    	cout << "The number of words you typed in is "
    	     << num_words
    	     << endl
    	     << "The number of each letter you typed in is:\n";
    	for (int i = 0; i < 26; i++)
    	{
    		if (letter_count[i] > 0)
    		{
    			c = 'a' + i;
    			cout << c
    			     << "   "
    			     << letter_count[i]
    			     << endl;
    		}
    	}
    }
    Last edited by jaw24; 04-06-2007 at 08:55 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe
    Code:
    int letter_count[26] = { 0 };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    Thank you, worked perfectly. I'm a beginner(as you can tell), much to learn.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (c == '\n' ||
    Lookup isspace() and ispunct()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM