Kira Sama Kira Sama - 5 months ago 33
C++ Question

Calling Output file function using class

I Have an program that makes an

file and it consists of a text by
string of arrays

the program is working successfully when i put the code in the main.cpp
but i have not tried to do this with

This code not seems to work with my class attempt

The Code is

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
int i;
string FileName; // Our File name variable
FileName = "Output.txt";
//Write Stream
string textArray[] = //Number Of lines to store and [] means all
"Bla ",
"Bla Bla ",
"Bla Bla Bla ",
"Bla Bla Bla Bla ",

cout << "Opening file...";
ofstream outputFile(FileName); //specify the file
if (outputFile) //If the value of outputFile not NULL
cout << "Done" << endl;

cout << endl << "Writing text to the file " << FileName << "...";
int i = 0; //From the first line 'progrmmers count from zero'
while (i <= 3) //Write to Line 4
outputFile << textArray[i] << endl; //Write first string then add '\n'
i++; //repeat to the next line by adding +1
cout << "Done" << endl;
cin >> i;
else //Error: maybe read-only place
cout << "Could not open file." << endl;

Note: I added an
line used to open the text file by name and other functions rather than that simplified code

But that's just got it more complex so i decided to create classes to get it simple like:


class Output{
void OutputTheFile();


#include "MyClass.h"
void OutputTheFile()

The problem that I fail when i do that cause i can't find the way to get my output class to work by just call it with one line in my
(ex. OutputTheFile();)

Any idea to do that?

EDIT: Sorry for my past english i feel like an idiot everytime i look at this question and likely can't understand even what i asked so i completely edited it to get things more clear with my new account.

Answer Source

Hey i found the answer to my question by myself i tried only to make an hpp class called Output and didn't declare anything but just go to Output.cpp directly then put my Code into Output::Output() Loop with no return and put my libraries sure at the first then went to my main.cpp and Type Output::Output(); and my program working successfully as I want :D Thanks For All Your advices guys and hope that's will help you in the future XD

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download