# 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. ## 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. 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. 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. No the file says:

I am Blaxxon,
the cosmic computer.
Fear me!

5. 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. Originally Posted by Marine4Jesus
...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.

7. Originally Posted by BobS0327
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. Originally Posted by Cpro
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. 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.

10. Originally Posted by Cpro
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. Originally Posted by Marine4Jesus
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.