Upper case output

This is a discussion on Upper case output within the C++ Programming forums, part of the General Programming Boards category; Hi I am new to C++ programming and I am in some need of help. I need to write a ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    23

    Upper case output

    Hi I am new to C++ programming and I am in some need of help. I need to write a program that opens a file called "in.dat", reads it character by character and converts all lower case characters to upper case. The program then needs to write this output to a file called "out.dat" and outputs its contents to the screen. This is what I have so far:

    Code:
    #include <iostream>
    #include <cctype>
    #include <fstream>
       using namespace std;
    
    int main()
    {
       ifstream inFile("in.dat");
       ofstream outFile("out.dat");
       if (!inFile)
       {
          cout << "Input file not open" << endl;
          return -1;
       }
    
       if (!outFile)
       {
          cout << "Output file not open" << endl;
          return -1;
       }
    
       char ch;
    
       while (ch = inFile.get())
       {
          cout.put(ch);
          outFile.put(ch);
       }
    
       inFile.close();
       outFile.close();
       return 0;
    }
    I already have some text for "in.dat" but this code only loops a character infinitely to the screen. Any help would be appreciated. Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,805
    inFile.get() returns an int, not a char. When it attempts to read and finds that the end of file has been reached it returns EOF, which is a negative value. Since it is non-zero, it evaluates to true, hence your loop keeps on going even when the end of file has been reached.

    I suggest using while (inFile.get(ch)) 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
    Mar 2007
    Posts
    23
    It's OK people, I've worked it out. Thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  2. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  3. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM
  4. Printing weekday
    By sworc66 in forum C Programming
    Replies: 12
    Last Post: 09-13-2002, 07:03 AM
  5. Changing bkgrnd color of Child windows
    By cMADsc in forum Windows Programming
    Replies: 11
    Last Post: 09-10-2002, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21