Thread: Capitalize first letter of every word in .txt file

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    25

    Capitalize first letter of every word in .txt file

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
        FILE* fin;
        FILE* fout;
        char inputfname[256];
        char outputfname[256];
        char currentchar;
        char previouschar = ' ';
    
        printf("Enter the input filename\n");
        scanf("%s", inputfname);
        printf("Enter the output filename\n");
        scanf("%s", outputfname);
    
        fin = fopen(inputfname, "r");
        fout = fopen(outputfname, "w");
    
        while(!feof(fin))
        {
            currentchar = fgetc(fin);
            if(!isalpha(previouschar) && isalpha(currentchar))
            {
                currentchar = toupper(currentchar);
                fputc(currentchar, fout);           
            }
            currentchar = previouschar; 
        }
    
        system("PAUSE");
        return 0;
    }
    As the title says. I can't get this to work. I know I'm making a stupid mistake somewhere and I can't find it. And yea I know system("PAUSE"); shouldn't be used but it's what my prof. insists on.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?

    Incidentally, you should #include <stdlib.h> for system() and feof() should not be used to control a loop - use the result of fgetc() instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    It capitalizes every letter in the .txt file.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably want to use fputc() on every char that is read, not just those that are capitalised.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    And you also want
    Code:
    previouschar = currentchar;
    instead of
    Code:
    currentchar = previouschar;
    I hate real numbers.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    Well I forgot to close the files ;/ but that was never the problem anyway. *Sigh* I know what you mean about fputc for every char, I just can't figure out how to implement it.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I know what you mean about fputc for every char, I just can't figure out how to implement it.
    Move that line to after the if block.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Additionally, read the FAQ on how to use feof() correctly.
    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.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    Thanks guys I figured it out now. Sometimes the simplest problems can be a real pain lol.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Last week a "hello world" question ended up being a 100+ post thread. So don't feel too bad.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 10:08 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM