Thread: Problem with character counting program...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    34

    Problem with character counting program...

    I'm having problems with this program that i'm trying to write for one of my classes. I'm probably going to get made fun of like last time I posted, but i'm sure it won't be too difficult for you guys to figure the problem out, but i'm kind of new to programming. thanks!
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void print_header();
    
    int main()
    {
      int vowel, odd, quote, period, rest, total;
      char file;
      print_header();
    do
      {
      switch (file)
        {
        case 'A': case 'a':
        case 'E': case 'e':
        case 'I': case 'i':
        case 'O': case 'o':
        case 'U': case 'u':
        case 'Y': case 'y':
          vowel++;
          break;
        
        case '1': case '3':
        case '5': case '7':
        case '9':
          odd++;
          break;
          
        case '\"': case '\'':
          quote++;
          break;
    
        case '.':
          period++;
          break;
    
        default:
          rest++;
        }
      }
     while (cin.eof()); 
    
     cout << "Category   " << "Count        \n"
          << "========== " << "=============" << endl;
     cout << "quotes     " << quote << endl;
     cout << "vowels     " << vowel << endl;
     cout << "periods    " << period << endl;
     cout << "oddDigits  " << odd << endl;
     cout << "allTheRest " << rest << endl;
     total = (vowel+quote+period+odd+rest);
     cout << "total      " << total << endl;
     return 0;
    }
    
    /*
     * Name: print_header
     * Purpose: Display header
     * Receive: Nothing
     * Return: Header
     */
    
    void print_header()
    {
      cout << "Name: mygly\n"
           << "Course: CPS 180 (45911)\n"
           << "Assignment: Homework 9\n"
           << "Purpose: count all vowels, quotes, periods, odd integers and non-whitespace and display them in a table format to the user\n";
    }
    THis is what I get for output when I run it typing "./hw05 < warworlds.txt"

    Output:
    Name: Mygly
    Course: CPS 180 (45911)
    Assignment: Homework 9
    Purpose: count all vowels, quotes, periods, odd integers and non-whitespace and display them in a table format to the user
    Category Count
    ========== =============
    quotes 0
    vowels 0
    periods -12847528
    oddDigits 0
    allTheRest 68017
    total -12779511


    As you can tell I'm have a problem... Thanks

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You never set "file" to a value, it's uninitialized in the switch-statement.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    34
    I'm confused as to what file should be, should I just use if statements instead? The program is supposed to read from the file warworlds.txt and search for vowels, periods, quotes, odd integers and everything else minus white space, and then print it out in the table.
    Last edited by MyglyMP2; 03-25-2005 at 02:14 PM.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I'd do something like:
    Code:
    while(TRUE)
    {
       file = cin.get();
       if(cin.eof()) break;
    
       switch(file)
       {
          ...
       }
    }
    Another thing, I don't think normal cin will ever reach eof(), it'll just keep on waiting for more input. Perhaps piping a file like you do will work though...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    34
    From what I understand in Unix cin.eof will wait for the user to enter control d, before cin.eof will be true. So does that mean I should have something else be the .eof? What do you mean by while (TRUE), what is TRUE supposed to be?
    Last edited by MyglyMP2; 03-25-2005 at 02:25 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int vowel, odd, quote, period, rest, total;
    Try setting all these to 0 as well, otherwise what do you think vowel++ is going to result in?
    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.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    34
    For some reason the switch statement only executes one time, which is why i am getting allTheRest = 1 and total = 1. Any ideas on how I can make this loop til it gets to the ned of the file?

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    34
    Alright, I've fixed almost everything. The only problem i'm having is that it is counting the white space for my all the rest count. here's my new code.
    Code:
    /*
     * Name : Mygly
     * Course : CPS 180 (45911)
     * Assignment : Homework04
     * Description :  
     */
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void print_header();
    
    int main()
    {
      int vowel, odd, quote, period, rest, total;
      vowel = 0;
      odd = 0;
      quote = 0;
      period = 0;
      rest = 0;
      total = 0;
      char file;
      print_header();
      do
      {
        file = cin.get();
        if (cin.eof()) break;
      switch (file)
        {
        case 'A': case 'a':
        case 'E': case 'e':
        case 'I': case 'i':
        case 'O': case 'o':
        case 'U': case 'u':
        case 'Y': case 'y':
          vowel++;
          break;
        
        case '1': case '3':
        case '5': case '7':
        case '9':
          odd++;
          break;
          
        case '\"': case '\'':
          quote++;
          break;
    
        case '.':
          period++;
          break;
    
        default:
          rest++;
        }
      }
      while (!cin.eof());
    
     cout << "Category   " << "Count        \n"
          << "========== " << "=============" << endl;
     cout << "quotes     " << quote << endl;
     cout << "vowels     " << vowel << endl;
     cout << "periods    " << period << endl;
     cout << "oddDigits  " << odd << endl;
     cout << "allTheRest " << rest << endl;
     total = (vowel+quote+period+odd+rest);
     cout << "total      " << total << endl;
     return 0;
    }
    
    /*
     * Name: print_header
     * Purpose: Display header
     * Receive: Nothing
     * Return: Header
     */
    
    void print_header()
    {
      cout << "Name: Mygly\n"
           << "Course: CPS 180 (45911)\n"
           << "Assignment: Homework 9\n"
           << "Purpose: count all vowels, quotes, periods, odd integers and non-whitespace and display them in a table format to the user\n";
    }

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > The only problem i'm having is that it is counting the white space for my all the rest count
    So add a case for white space which does nothing
    And then the default will be everything else except white space
    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.

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    34
    That's what i did, except i made the case whitespace--, not sure if that makes a difference, but either way, it worked, thanks for the help guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 02:02 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  5. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM