Facing Problems In c++. Please Solve This Code...!

ankit.kumar010203

Journeyman
Hello,
I am facing problem with my c++ coding:cry::cry:. How can i add these following sentences to my text file of notepad? I know that it is wrong. Please Help me...! How can I fix it? The code are as follow:-


#include <iostream>
#include <fstream>

using namespace std;

class cars{
public:
void introcar(){
cout << "Cars was invented by Nicolas Joseph Cugnot.\n";
cout << "Cars have 4 wheels and more than 5 doors.\n";
}
};

class maruti : public cars{
public:
void marutiinfo(){
cout << "Maruti Suzuki was founded by Sanjay Gandhi.\n";
cout << "Maruti Suzuki is one of the top Car company in the world.\n";
}
};

class swift : public maruti{
public:
void swiftinfo(){
cout << "Swift is one of the best model of Maruti Suzuki Cars.\n";
}
};
int main()
{
ofstream filename("Cars.txt");
swift ob1;
filename << ob1.introcar();
filename << ob1.marutiinfo();
filename << ob1.swiftinfo();

return 0;
}
 

krishnandu.sarkar

Simply a DIGITian
Staff member
Well, just replace the

Code:
ofstream filename("Cars.txt");

with

Code:
ofstream filename;
filename.open("Cars.txt");
 
you haven't mantioned the file opening mode. If you are appending, thein it should be:

ofstream filename;
filename.open("e:\cars.txt", ios::app);

the functions you are calling write their output to consol, they do not return anything. What you can do is that instead of using cou in those functions, you can make those functions return a string containg whatever you want to print. Then you can use "filename << <string name>" to do the task.
 

krishnandu.sarkar

Simply a DIGITian
Staff member
^^Right. Sorry I didn't noticed that.

@OP It won't work that way. Maybe you can try returning the string from the function.

The thing needs to be

Code:
filename << "Hello World";
 
Top Bottom