why cant it work properly???

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

int main(){
    ofstream out;//create output stream
           int acc_num[10];
           for(int x=0;x<10;x++)
           acc_num[x] = rand()%10; //generate account number
           
           char acc_file[600];
           for(int x=0;x<10;x++)
           acc_file[x] = acc_num[x] + '0';
           out.open(acc_file, ios::out);//create & open output file
           
           out << "PACKAGE ID: ";
           for(int x=0;x<10;x++)
           out << acc_num[x];
           out << endl;
             
//------------------------------ENTER DETAILS------------------------- 
           string sname, saddress, scity, sstate, weight;
           int szipcode;
           
           cout << "ENTER WEIGHT OF PACKAGE(Kg): ";
           cin >> weight;
           out << "WEIGHT OF PACKAGE: " << weight << endl;
           
           cin.ignore();
           cout << "ENTER SENDER'S NAME: ";
           getline(cin, sname);
           out << "SENDER'S NAME: " << sname << endl;
           
           cin.ignore();
           cout << "ENTER SENDER'S ADDRESS: ";
           getline(cin, saddress);
           out << "SENDER'S ADDRESS: " << saddress << endl;
           
           cin.ignore();
           cout << "ENTER SENDER'S CITY: ";
           getline(cin, scity);
           out << "SENDER'S CITY: " << scity << endl;
           
           cin.ignore();
           cout << "ENTER SENDER'S STATE: ";
           getline(cin, sstate);
           out << "SENDER'S STATE: " << sstate << endl;
           
           cin.ignore();
           cout << "ENTER SENDER'S ZIP CODE: ";
           cin >> szipcode;
           out << "SENDER'S ZIP CODE: " << szipcode << endl;
           system("pause");
}

Why dont you say whats not working first..

What is it that doesnt work properly, what does the program suppose to do?

Because your account file variable (used in the open function) is not a valid string?

wen u enter the name n all it dosent output to the txt file properly...try compiling it n have a look....

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

int main() {
	int acc_num[10];
	for (int x=0; x<10; x++)
		acc_num[x] = rand()%10; //generate account number

	char acc_file[11];
	for (int x=0; x<10; x++)
		acc_file[x] = acc_num[x] + '0';
	acc_file[10] = '\0'; 
	ofstream out;//create output stream
	out.open(acc_file);//create & open output file
	if (out.is_open()) {
		out << "PACKAGE ID: ";
		for (int x=0; x<10; x++)
			out << acc_num[x];
		out << endl;

		//------------------------------ENTER DETAILS------------------------- 
		string sname, saddress, scity, sstate, weight;
		int szipcode;

		cout << "ENTER WEIGHT OF PACKAGE(Kg): ";
		cin >> weight;
		out << "WEIGHT OF PACKAGE: " << weight << endl;

		cin.ignore();
		cout << "ENTER SENDER'S NAME: ";
		getline(cin, sname);
		out << "SENDER'S NAME: " << sname << endl;

		cin.ignore();
		cout << "ENTER SENDER'S ADDRESS: ";
		getline(cin, saddress);
		out << "SENDER'S ADDRESS: " << saddress << endl;

		cin.ignore();
		cout << "ENTER SENDER'S CITY: ";
		getline(cin, scity);
		out << "SENDER'S CITY: " << scity << endl;

		cin.ignore();
		cout << "ENTER SENDER'S STATE: ";
		getline(cin, sstate);
		out << "SENDER'S STATE: " << sstate << endl;

		cin.ignore();
		cout << "ENTER SENDER'S ZIP CODE: ";
		cin >> szipcode;
		out << "SENDER'S ZIP CODE: " << szipcode << endl;
	} else
		cout << "Unable to open file";
	return 0;
}

read my previous post.

After you attempt to open a file, you should always test for success, and handle the error in some fashion. You're not doing that, so your program goes on blindly thinking it's got a file to write to.

And, get rid of those ignore( ) statements between the keyboard inputs. Using getline( ) with the string type makes this pretty much pointless. There's nothing to ignore.

ok...I've done wat u told me to do VMANES...now it skip's the enter sender's name part...

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

int main(){
    ofstream out;//create output stream
           int acc_num[10];
           for(int x=0;x<10;x++)
           acc_num[x] = rand()%10; //generate account number
           
           char acc_file[600];
           for(int x=0;x<10;x++)
           acc_file[x] = acc_num[x] + '0';
           out.open(acc_file, ios::out);//create & open output file
           
           if (out.is_open()) {
           out << "PACKAGE ID: ";
           for(int x=0;x<10;x++)
           out << acc_num[x];
           out << endl;
             
//------------------------------ENTER DETAILS------------------------- 
           string sname, saddress, scity, sstate, weight;
           string rname, raddress, rcity, rstate;
           int szipcode, rzipcode;
           
           cout << "ENTER WEIGHT OF PACKAGE(Kg): ";
           cin >> weight;
           out << "WEIGHT OF PACKAGE: " << weight << endl;
           

           cout << "ENTER SENDER'S NAME: ";
           getline(cin, sname);
           out << "SENDER'S NAME: " << sname << endl;
           

           cout << "ENTER SENDER'S ADDRESS: ";
           getline(cin, saddress);
           out << "SENDER'S ADDRESS: " << saddress << endl;
           

           cout << "ENTER SENDER'S CITY: ";
           getline(cin, scity);
           out << "SENDER'S CITY: " << scity << endl;
           

           cout << "ENTER SENDER'S STATE: ";
           getline(cin, sstate);
           out << "SENDER'S STATE: " << sstate << endl;
           

           cout << "ENTER SENDER'S ZIP CODE: ";
           cin >> szipcode;
           out << "SENDER'S ZIP CODE: " << szipcode << endl;
           
           } else
             cout << "Unable to open file";
             
           return 0;
           system("pause");
}

Mixing cin >> var; and cin.getline( var, len ); is looking for trouble. The first statement, as you used to get the weight, stops at the newline entered by the user, leaving that newline in the input stream. When your getline executes, it sees the newline first thing, and, since newline is the default delimiter for getline ( the third argument that we often omit), your getline did exactly what you told it to. So while you typed a Sender's name, it ends up in the next variable, the address.

Either use getline for the weight as well, or follow that line with a cin.ignore( 10, '\n' ); to throw away the newline. You can use any number for the first argument, allowing enough for whatever any bad user of your program might do after the weight value.

(in other words, put it back the way you had it originally :) )

What Vmanes said and:

This line: acc_num[x] = rand()%10; //generate account number Will always generate the same sequence of numbers because you haven't seeded the function rand() with srand() Most people use srand((unsigned)time(0)); for this.

return 0;
           system("pause");

The line system("pause"); will never be reached because you return 0 (end the program) before system is called.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.