Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,102 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,675 people online right now. Registration is fast and FREE... Join Now!




while loops

 
Reply to this topicStart new topic

while loops

wisheshewasleet
15 Oct, 2007 - 11:10 PM
Post #1

New D.I.C Head
*

Joined: 17 Sep, 2007
Posts: 15


My Contributions
Hey everyone.
I"m writing a program with lots of different functions and things, and it all works except for the last function where I am supposed to have a function that "Opens a file of character data (name supplied by user), and copies the nonblank characters to an output file (name also supplied by user). Place one blank space between the output items on each line. The digit characters („0‟ – „9‟) are to be treated differently. In place of writing out the digit write the digit’s name to the output file; i.e. zero for 0, one for 1, etc. The output file should have the same number of lines as the input file."

This code compiles fine, but why is my output file blank?
I only included this function, if you want the main function or anything just let me know.
Thanks

CODE

void FilePlay()
{
    char fileName[30], ch;

    ifstream infile;
    ofstream outfile;

    cout << "Please enter the name of your input file: " << endl;
    cin >> fileName;

    infile.open(fileName);
    outfile.open("output.txt");
    infile >> ch;

while(infile >> ch)
{
switch(ch)
{
case 0: outfile << "zero ";
        break;
case 1: outfile << "one ";
        break;
case 2: outfile << "two ";
        break;
case 4: outfile << "four ";
        break;
case 5: outfile << "five ";
        break;
case 6: outfile << "six ";
        break;
case 7: outfile << "seven ";
        break;
case 8: outfile << "eight ";
        break;
case 9: outfile << "nine ";
        break;
default: outfile << ch << " ";
}
infile.close();
outfile.close();
}

    cout << "Your filename was: " << fileName << endl;

}

User is offlineProfile CardPM
+Quote Post

jjhaag
RE: While Loops
15 Oct, 2007 - 11:36 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
You've got the file stream method close() called for both of your files...in your while loop. So it goes through the first round, and then closes the file. All other operations on the files will fail, because there is no longer an association of the stream with the file.

Move the infile.close() and outfile.close() statements to the outside of the while loop.

And you may also want to use the get() method instead of the stream extraction operator >>, and use a test of whether or not the end of the file has been reached as your while-loop continuation test:
CODE
while (!infile.eof()) {
        infile.get(ch);
        ...


get(ch) will get a single character from the file stream; the >> operator is generally used to extract formatted data (floats, ints, etc).

You should probably also have a check to ensure that the file was opened successfully, before you try to read from it.

Hope that helps

This post has been edited by jjhaag: 15 Oct, 2007 - 11:37 PM
User is offlineProfile CardPM
+Quote Post

wisheshewasleet
RE: While Loops
16 Oct, 2007 - 08:56 AM
Post #3

New D.I.C Head
*

Joined: 17 Sep, 2007
Posts: 15


My Contributions
Thanks. I tried some of the things you said but I was still not getting the desired output, but the idea of testing to see whether I was opening my input file, reading from my input file, and all that really helped. after some testing I found that I was opening the file, and I was reading characters in, but it wasnt writing them to the output file. It was right hten that I realized I was working with the char data type, so my cases, which had previously been
case 0
case 1
case 2... etc should have had the apostrophes around them like...
case '0'
case '1'
and case '2'

After insterting the apostrophes, the program ran without a flaw. Thanks again for helping out.
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: While Loops
16 Oct, 2007 - 09:53 AM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Nice catch. The value of a character versus the character itself is a pretty common mistake (voice of experience) when dealing with the char data type. But way to figure it out on your own smile.gif

-jjh
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:58PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month