Thread: Code logic error

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    Code logic error

    Hey guys I am just wondering why the following code gives the wrong number of characters from a file? Thanks.


    Code:
    void main()
    {
        int blank_count = 0;
        int char_count = 0;
        int sentence_count = 0;
        char ch;
    
        ifstream iFile("c:\test.txt");
    
        if (! iFile)
        {
            cout << "Error opening input file" << endl;
           
        }
    
        while (iFile.get(ch))
        {
            switch (ch) {
                case ' ':
                    blank_count++;
                    break;
                case '\n':
                case '\t':
                    break;
                case '.':
                    sentence_count++;
                    break;
                default:
                    char_count++;
                    break;
            }
        }
    
        cout << "There are " << blank_count << " blanks" << endl;
        cout << "There are " << char_count << " characters" << endl;
        cout << "There are " << sentence_count << " sentences" << endl;
    }

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    > "c:\test.txt"

    To be able to include the quotation mark and other special characters in strings, the \ character is treated specially. For example "a \"quoted\" string" would result in a string with teh value: a "quoted" string. The sequence \t results in a tab character. To include a back-slash in a string you need to write two, like here:

    ifstream iFile("c:\\test.txt");

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    I see thanks, the problem I am having is though that the program says there are more characters that are really there.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void main()
    main returns int

    > cout << "Error opening input file" << endl;
    If you're not getting this, then it means you opened a different file to the one you expected to open.

    Do as joni suggested and use \\ to get the real "test.txt", not a filename which begins with a tab character.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    Could you then post the code that you are actually working with? I doubt you have files with names that include tab characters.

  6. #6
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    I think what Salem meant was that the code you wrote above is looking for a file that begins with a tab character. The escape sequence (that's what characters like \\ and \' are called) for a tab is \t. So the file your program is looking for is something like:
    Code:
     c:{tab}est.txt
    since \t is interpreted as a tab.

    I don't know if that's a 100% accurate representation, but you get the idea.

    If, inside a string literal, you want the backslash character, you need to use the escape sequence '\\' instead of \ . A file's path, within a string literal, looks like this:
    Code:
    "c:\\folder\\subfolder\\filename.ext"
    Google escape sequences for a complete list.
    There is a difference between tedious and difficult.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM