Kira Sama Kira Sama - 2 months ago 11
C++ Question

Calling Output file function using class

I Have an program that makes an

Output.txt
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
classes


This code not seems to work with my class attempt


The Code is


//Libraries
#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;
}
return EXIT_SUCCESS;
}


Note: I added an
cin
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:


MyClass.h


class Output{
public:
void OutputTheFile();
};



MyClass.cpp


#include "MyClass.h"
void OutputTheFile()
{
//stuff...
}


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
main.cpp
(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