Hi guys,
I just started using this website recently to help study for my college C++ class.
Today I was trying to write a program that reads input data from a .txt file and prints it to another .txt file named by the user.
I wrote the code to do this, but when I tried to run it, I got a message that the iostream objects are protected. I actually don't even understand what this means really, is there any way to have a program in Online GDB read or print data files?
Just wondering, as it would be nice actually test my code and see if I did the exercise right.
Thanks,
EDIT: Here is data file I created as well as the portion of the code where I open the files and initialize the istream and ostream objects. The program reads from a txt file (I will show below) and prints to an output txt file. The purpose of the assignment is to write a code that will read a file containing only integers and whitespace, and print the number of integers and the number of individual lines. It doesn't have to print to an output file per se, getting it to print to standard output would be just fine as well.
Main Program (not complete, just the pertinent part):
/******************************************************************************
Problem 5-25
*******************************************************************************/
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
// Declare and initialize objects
int count, entries, data, integers{0};
char space;
istream fin;
ostream fout;
string filename;
// Collect name of file
cout << "Enter name of input file:\n";
cin >> filename;
// Open Input File and Check for Errors
fin.open(filename);
if(fin.fail())
{
cerr << "Error opening input file " << filename << endl;
exit (1);
}
// Open Report File
fout.open("Report.txt");
Data File (input.txt):
10
5 3 4
1 2
6
7
8 9
3 2 1
2 9
3 9
46 24
82 100 99
In the data file I created, I currently am using a counter-controlled loop, where the number of entries is read as the first data point.
Anybody who knows why I am getting the error message when I try to initialize the istream and ostream and call these objects, it would be greatly appreciated. Here is the error log I get:
main.cpp:18:13: error: ‘std::basic_istream<_CharT, _Traits>::basic_istream() [with _CharT = char; _Traits = std::char_traits]’ is protected within this context
18 | istream fin;
| ^~~
In file included from /usr/include/c++/9/iostream:40,
from main.cpp:6:
/usr/include/c++/9/istream:606:7: note: declared protected here
606 | basic_istream()
| ^~~~~~~~~~~~~
main.cpp:19:13: error: ‘std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char; _Traits = std::char_traits]’ is protected within this context
19 | ostream fout;
| ^~~~
In file included from /usr/include/c++/9/iostream:39,
from main.cpp:6:
/usr/include/c++/9/ostream:390:7: note: declared protected here
390 | basic_ostream()
| ^~~~~~~~~~~~~
main.cpp:27:9: error: ‘std::istream’ {aka ‘class std::basic_istream’} has no member named ‘open’
27 | fin.open(filename);
| ^~~~
main.cpp:35:10: error: ‘std::ostream’ {aka ‘class std::basic_ostream’} has no member named ‘open’
35 | fout.open("Report.txt");
| ^~~~