Thread: program is case-sensitive

  1. #1
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166

    program is case-sensitive

    I wrote a small program that counts words, and looks for specific words, but the problem is that it's case-sensitive.

    So if the search requested "one" and the file contains "ONE" the count for "one" would register as 0.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    int main(){
        ifstream countwords;
        string word, specificword;
        int counter = 0, checkword = 0;
    
    cout<<"Type the word you want to count in the file.\n";
    cin>>specificword;
    cin.ignore();
    
    countwords.open( "wordcount.txt" );
        while ( countwords>>word ){
              if ( word == specificword ){
                   checkword++;
                   }
                   counter++;
              }
        countwords.close();
        
              cout<<"\n\nThe file contains "<<counter<<" words.\n";
              cout<<checkword<<" of them is "<<specificword<<".\n\n"; 
              cin.get();
              }

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    use toupper or to lower (or write your own function to convert a string to all uppercase or all lowercase).
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    no, use stricmp


    work on indention....you're close, but no cigar

    Code:
    while(1){
    	int x = 12;
    	x = x * x / x;
    
    	if(x){
    		x = 0;
    	}  // <-- put this brace under the first character of the keyword it "belongs" to
    }           // read that ^^^^^
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    lol. thanks. I use Dev-C++ and i just let it do whatever it wants with the indetation.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Except stricmp() isn't a standard function.
    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.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    What's the deal with kernel indentation anyway. How can people think this is more readable:
    Code:
    if(x){
       ...
    }
    than this:
    Code:
    if(x)
    {
      ...
    }
    The block structure is so much clearer in the latter.

    Anyway, that's my opinion. Like it or hate it!
    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.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by Salem
    Except stricmp() isn't a standard function.
    oops

    Quote Originally Posted by Magos
    The block structure is so much clearer in the latter.

    Anyway, that's my opinion. Like it or hate it!
    i agree, but since that's how he had it, i figured i'd stick with it.


    i've been looking at java source lately and it really bugs me....
    perhaps i will write a program to fix the code
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    24
    I wrote a small program that counts words, and looks for specific words, but the problem is that it's case-sensitive.
    you can also write a custom char_traits object and pass it as the second template parameter of basic_string to override the default comparison semantics.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> ....you can also write a custom char_traits object
    This aught to be in a FAQ somewhere....
    http://www.josuttis.com/libbook/stri...tring.hpp.html

    Or for case insensitive operations on any character sequence:
    http://www.josuttis.com/libbook/string/iter2.cpp.html

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Xmas competitions
    By Salem in forum Contests Board
    Replies: 88
    Last Post: 01-03-2004, 02:08 PM
  3. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM
  4. Changing bkgrnd color of Child windows
    By cMADsc in forum Windows Programming
    Replies: 11
    Last Post: 09-10-2002, 11:21 PM