i am trying to write input() function into file.....
please help....
i am using visual studio 2013

 // N Copy.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
#include "fstream"

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{

    fstream file;
    file.open("NCopy.txt");
    file << input();
    file.close();
    system("pause");

    return 0;

    }

void input()
{
    cout << "Enter how many copies you want: ";
    int num;
    cin >> num;
    cout << "Enter text you want to copy: " << endl;
    char in[256];
    cin.getline(in, 256);
    for (int n = 1; n <= num; n++)
    {
        cout << n << ": " << in << endl;
    }
}

There are a few problems.

#include "stdafx.h"

This is uneeded.

#include "iostream"

We inlcude system headers with < and >. This should be #include <iostream>.

#include "fstream"

Same problem here. Use < and >.

int _tmain(int argc, _TCHAR* argv[])

This is not C++, and is only usable from Visual Studio. You should use int main() in this case instead.

fstream file;

It's better to use ofstream here instead.

file.open("NCopy.txt");

You didn't specify a mode (you don't need a mode if your using ofstream).

file << input();

You didn't declair input() yet! The compiler must see input() defined somewhere before you can use it. Your two option would be to define input before main(), or you can declair a prototype before main().

system("pause");

Don't do this. It's not portable at all (it only works on windows sometimes, usually does not work on other operating systems). Over top of that, it's not proper C++. I usually reccomend that you do not pause a program before closing it (only students who don't know how the command line works usually do this). But if you insist on pausing your program before exiting, use something like:

std::cout << "Press <enter> to exit... " << flush;
std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );

void input()

The problem here is void. Esentially void means that the function isn't going to return anything productive. Go back to this line: file << input();. The operator << is expecting something (presumably a string in this case), but your function doesn't return anything. You should either be returning a string or a char *.

char in[256];

Why not use a string instead?

Your input() function also needs to return a value (ie, return in;).

Comments
Thanku so much (brother || friend), whatever you like....

Hey "Hiroshe"! can u tell me how to read a .dat file in C++?Plz..

Edited 2 Years Ago by rehan_5

Hey "Hiroshe"! can u tell me how to read a .dat file in C++?Plz..

Don't hijack someone elses thread (otherwise conversations will get confusing). Read the rules, make your own thread and then we'll help you.

This article has been dead for over six months. Start a new discussion instead.