Help with Example in C++ without fear book.

This is a discussion on Help with Example in C++ without fear book. within the C++ Programming forums, part of the General Programming Boards category; They gave two examples: The 1st which they taught me how to create a file and put strings into the ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    5

    Help with Example in C++ without fear book.

    They gave two examples:

    The 1st which they taught me how to create a file and put strings into the file:


    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main() {
        char filename[81];
    
        cout << "Enter a file name and press ENTER: ";
        cin.getline(filename, 80);
        ofstream file_out(filename);
        if (! file_out) {
            cout << "File " << filename << " could not be opened.";
            return -1;
        }
        cout << "File " << filename << " was opened.";
        file_out << "I am Blaxxon," << endl;
        file_out << "the cosmic computer." << endl;
        file_out << "Fear me.";
        file_out.close();
        return 0;
    }

    ...in the next example they gave me, (according to the book) I am suppose to be able to open that same file with whatever name/directory i gave it and, if i choose, put more strings into the file:
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main() {
        int c;   // input character
        int i;   // loop counter
        char filename[81];
        char input_line[81];
    
        cout << "Enter a file name and press ENTER: ";
        cin.getline(filename, 80);
    
        ifstream file_in(filename);
    
        if (! file_in) {
            cout << "File " << filename << " could not be opened.";
            return -1;
        }
    
        while (1) {
            for (i = 1; i <= 24 && ! file_in.eof(); i++) {
                file_in.getline(input_line, 80);
                cout << input_line << endl;
            }
            if (file_in.eof())
                break;
            cout << "More? (Press 'Q' and ENTER to quit.)";
            cin.getline(input_line, 80);
            c = input_line[0];
            if (c == 'Q' || c == 'q')
                break;
        }
        return 0;
    }
    However, all it does is opens the file and tells me to press any key to exit.


    Is their a problem with my compiler? I am currently using a trial version of Visual C++ 2008 express.

    Any help would be great thank you!

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,171
    Is their a problem with my compiler? I am currently using a trial version of Visual C++ 2008 express.
    The chance of you being the first one to find a bug with that kind of trivial code in a compiler used by millions of people is like the chance of winning a lottery.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,171
    However, all it does is opens the file and tells me to press any key to exit.
    Is your file empty by any chance?

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    No the file says:

    I am Blaxxon,
    the cosmic computer.
    Fear me!

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Your posted code works fine for me. It displays all the required data. Are you sure you're entering the correct path to the text file?

    Assuming that you're a jarhead, Semper fi, Marine.

  6. #6
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Quote Originally Posted by Marine4Jesus View Post
    ...in the next example they gave me, (according to the book) I am suppose to be able to open that same file with whatever name/directory i gave it and, if i choose, put more strings into the file:
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main() {
        int c;   // input character
        int i;   // loop counter
        char filename[81];
        char input_line[81];
    
        cout << "Enter a file name and press ENTER: ";
        cin.getline(filename, 80);
    
        ifstream file_in(filename);
    
        if (! file_in) {
            cout << "File " << filename << " could not be opened.";
            return -1;
        }
    
        while (1) {
            for (i = 1; i <= 24 && ! file_in.eof(); i++) {
                file_in.getline(input_line, 80);
                cout << input_line << endl;
            }
            if (file_in.eof())
                break;
            cout << "More? (Press 'Q' and ENTER to quit.)";
            cin.getline(input_line, 80);
            c = input_line[0];
            if (c == 'Q' || c == 'q')
                break;
        }
        return 0;
    }

    I believe the for loop is running to the end of the file. So, when the if(file_in.eof()) statement executes, it will always be at the end of the file, which breaks from the while loop. Thus, you are never able to input more strings. However, note that there are no file output statements to write the new strings to the file anyways.
    IDE - Visual Studio 2005
    Windows XP Pro

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    Quote Originally Posted by BobS0327 View Post
    Your posted code works fine for me. It displays all the required data. Are you sure you're entering the correct path to the text file?

    Assuming that you're a jarhead, Semper fi, Marine.
    Ooorahh!

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    Quote Originally Posted by Cpro View Post
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main() {
        int c;   // input character
        int i;   // loop counter
        char filename[81];
        char input_line[81];
    
        cout << "Enter a file name and press ENTER: ";
        cin.getline(filename, 80);
    
        ifstream file_in(filename);
    
        if (! file_in) {
            cout << "File " << filename << " could not be opened.";
            return -1;
        }
    
        while (1) {
            for (i = 1; i <= 24 && ! file_in.eof(); i++) {
                file_in.getline(input_line, 80);
                cout << input_line << endl;
            }
            if (file_in.eof())
                break;
            cout << "More? (Press 'Q' and ENTER to quit.)";
            cin.getline(input_line, 80);
            c = input_line[0];
            if (c == 'Q' || c == 'q')
                break;
        }
        return 0;
    }

    I believe the for loop is running to the end of the file. So, when the if(file_in.eof()) statement executes, it will always be at the end of the file, which breaks from the while loop. Thus, you are never able to input more strings. However, note that there are no file output statements to write the new strings to the file anyways.
    Okay it's working for me now. I saved the file at c:\borland\text.txt (I know weird place to save if I am using Microsoft Visual.) When using this source code it opens the file at location c:\borland\text.txt and prints the following:

    I am Blaxxon,
    the cosmic computer.
    Fear me.
    Press any key to continue...

    Now according to the book I should be prompted to input more information. (Forgive me I am new to c++ hence why I am using this book.) or am I wrong?

  9. #9
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Now according to the book I should be prompted to input more information.
    Well, you are already prompting the user for more information; you just need to write it to the end of the file.
    Here is how I wold go about it:
    Move the for loop out of the while loop (if you haven't already).
    Close the file after you output the contents of the file (the for loop).
    Re-open the file with ofstream.
    Add an else statement to the end of the while loop to output the string to the file, as long as the first character isn't Q.

    So, you are really only adding about 3 lines of code this way.
    IDE - Visual Studio 2005
    Windows XP Pro

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    Quote Originally Posted by Cpro View Post
    Well, you are already prompting the user for more information; you just need to write it to the end of the file.
    Here is how I wold go about it:
    Move the for loop out of the while loop (if you haven't already).
    Close the file after you output the contents of the file (the for loop).
    Re-open the file with ofstream.
    Add an else statement to the end of the while loop to output the string to the file, as long as the first character isn't Q.

    So, you are really only adding about 3 lines of code this way.
    Thanks for the help!

    so does this mean the author made an error?

  11. #11
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Quote Originally Posted by Marine4Jesus View Post
    so does this mean the author made an error?
    Perhaps, but maybe it was wanting the reader to actually implement that part themselves; who knows.
    IDE - Visual Studio 2005
    Windows XP Pro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. URGENT: Help wanted...in C
    By iamjimjohn in forum C Programming
    Replies: 16
    Last Post: 05-18-2007, 05:46 AM
  2. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 03:18 PM
  3. Newbie - MFC code from a book in VC++.Net
    By Guardian in forum Windows Programming
    Replies: 2
    Last Post: 04-27-2002, 07:17 PM
  4. C++: Reference Book, GUI, Networking & Beyond
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2001, 07:03 PM

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