Function for opening a file for user input

Hello everyone. I got a new challenge and I don't even know where to start. I got to go do something else for the rest of the day but I can seriously use some help or advice on how to approach this. Fair warning I came up with a code that has a lot of errors but I don't have time at the moment to sit down. But for the heck of it, I'll throw it out for everyone to see what I'm trying to do.

Heres my challenge:
Write a program that calls two functions; function one opens a file that the user will input the following customer data:
Name
Address
City,State,Zip
Account Balance
Date of Last Payment
The second function should reopen the file and appends more customer data.
Lastly read the contents of the file to display file content on the screen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// Function Prototype
string userInput(fstream & string);
void moreUserInput(fstream &);

// File stream object
fstream dataFile;

int main()
{
	
	return 0;
}

// Definition for function userInput*****************************
//***************************************************************
string userInput(fstream & string &name)
{
	// Checks for errors
	dataFile.open("userInput.txt", ios::in | ios::out);
	if (dataFile.fail())
	{
		cout << "Error! File could not be opened." << endl;
	}

	// writes data to file
	else
	{
		while (dataFile >> input)
		{
			cout << "Enter your name" << endl;
			cin << name << endl;
			cout << "Enter your address" << endl;
			cin << address << endl;
			cout << "Enter your city" << endl;
			cin << city << endl;
			cout << "Enter your state" << endl;
			cin << state << endl;
			cout << "Enter your zipcode" << endl;
			cin << zipecode << endl;
			cout << "Enter your account balance" << endl;
			cin << accountBalance << endl;
			cout << "Enter the date of your last payment" << endl;
			cin << datePayment << endl;
		}

		// closes the file
		dataFile.close();
	}
}




// Definition for function userInput*****************************
//***************************************************************
void moreUserInput(fstream &)
{
	// Checks for errors
	dataFile.open("userInput.txt", ios::in | ios::app);
	if (dataFile.fail())
	{
		cout << "Error! File could not be opened." << endl;
	}
	
	// Writes more data to the file
	else
	{
		while (getline (dataFile))
		{
			cout << "Enter your phone number" << endl;
			cin << phoneNumber << endl;
			cout << "Enter your email" << endl;
			cin << email << endl;
		}

		// Read contents of file and displays it
		cout << "Your name: " << dataFile >> name;
		cout << "Address: " << dataFile >> address << " " >> dataFile >> city >> dataFile >> state >> dataFile >> zipcode << endl;
		cout << "Account Balance: " << dataFile >> accountBalance;
		cout << "Date of Last Payment: " << dataFile >> datePayment;
}



Sorry I know its terrible but I just don't have the time at the moment to figure out what to do and I'm completely lost. Any advice is appreciated and I will get back to it tonight and hopefully learn how to approach this problem.

Thanks everyone!
First don't try to write the whole program without compiling. Write small sections at a time, then compile, fix errors/warnings, repeat till finished.

Second don't use global variables.

Third, for this problem it would probably be better not to pass the stream to and from your functions. Just pass the file name to the function and open the function in the function.

Fourth, good luck.

Ok I had some time to just start from the beginning and take one step at a time. I have to admit, at first I sort of panicked at first this morning but now I about got it. Here is my code so far. I didn't use functions at first and now I'm about to. FIY it does work so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// File stream object
fstream dataFile;

// Function Prototypes
void firstUserInput();
void SecondInputandDisplay();

int main()
{
	// Variables
	string name, address, city, state, zipCode, accountBalance, lastPayment, phoneNu, emailAddress;

	// Opens the file
	fstream dataFile("C:\\Users\\Brittney\\Documents\\College Courses\\C++ 2\\Chapter 12\\Test\\userInput.txt", ios::in | ios::out);

	// Shows error message if the file didn't open correctly.
	if (dataFile.fail())
	{
		cout << "Error! The file did not open correctly." << endl;
		return 1;
	}

	// Prompts user to input data
	cout << "Hello, I will be opening a file and prompting you to enter your information." << endl;
	cout << "Please enter your name" << "\n";
	getline(cin, name);
	cout << "Please enter your street address" << "\n";
	getline(cin, address);
	cout << "Please enter your city" << "\n";
	getline(cin, city);
	cout << "Please enter your state" << "\n";
	getline(cin, state);
	cout << "Please enter your zip code" << "\n";
	getline(cin, zipCode);
	cout << "Please enter your account balance" << "\n";
	getline(cin, accountBalance);
	cout << "Please enter the last date of your payment" << "\n";
	getline(cin, lastPayment);

	// Saves the data.
	dataFile << name;
	dataFile << address;
	dataFile << city;
	dataFile << state;
	dataFile << zipCode;
	dataFile << accountBalance;
	dataFile << lastPayment;

	cout << endl << "Saving your information on the file..." << endl;

	// CLoses the file
	dataFile.close();

	// Reopens the file for more input
	dataFile.open("C:\\Users\\Brittney\\Documents\\College Courses\\C++ 2\\Chapter 12\\Test\\userInput.txt", ios::in | ios::app);

	// Prompts user to add more input
	cout << endl << "Hello, I have reopened the file and will be asking information one more time." << endl;
	cout << "Please enter your phone number" << "\n";
	getline(cin, phoneNu);
	cout << "Please enter your email address" << "\n";
	getline(cin, emailAddress);

	// Saves the data.
	dataFile << phoneNu;
	dataFile << emailAddress;

	cout << endl << "Saving your information again..." << endl;

	// CLoses the file
	dataFile.close();

	// Displays results
	cout << endl << "Here is all your information on the file." << endl;
	cout << endl << "Your name: " << name << "\n";
	cout << "Your address: " << address << "\n";
	cout << "Your city: " << city << "\n";
	cout << "Your state: " << state << "\n";
	cout << "Your zip code: " << zipCode << "\n";
	cout << "Your account balance: " << accountBalance << "\n";
	cout << "Your last payment: " << lastPayment << "\n";
	cout << "Your phone number: " << phoneNu << "\n";
	cout << "Your email address: " << emailAddress << "\n";

	return 0;

}

// Definition for firstUserInput **********************************************
// ****************************************************************************
void firstUserInput()
{

}


// Definition for secondInputandDisplay ***************************************
// ****************************************************************************
void SecondInputandDisplay()
{

}
Here is my final code. Would you change anything about, otherwise it works. :) Yay!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// File stream object
fstream dataFile;

// Function Prototypes
int firstUserInput(fstream &dataFile);
int SecondInputandDisplay(fstream &dataFile);

// Variables
string name, address, city, state, zipCode, accountBalance, lastPayment, phoneNu, emailAddress;

int main()
{
	// Calling functions
	firstUserInput(dataFile);
	SecondInputandDisplay(dataFile);

	return 0;

}

// Definition for firstUserInput **********************************************
// ****************************************************************************
int firstUserInput(fstream &dataFile)
{
	// Opens the file
	dataFile.open("C:\\Users\\Brittney\\Documents\\College Courses\\C++ 2\\Chapter 12\\Test\\userInput.txt", ios::in | ios::out);

	// Shows error message if the file didn't open correctly.
	if (dataFile.fail())
	{
		cout << "Error! The file did not open correctly." << endl;
		return 1;
	}

	// Prompts user to input data
	cout << "Hello, I will be opening a file and prompting you to enter your information." << endl;
	cout << "Please enter your name" << "\n";
	getline(cin, name);
	cout << "Please enter your street address" << "\n";
	getline(cin, address);
	cout << "Please enter your city" << "\n";
	getline(cin, city);
	cout << "Please enter your state" << "\n";
	getline(cin, state);
	cout << "Please enter your zip code" << "\n";
	getline(cin, zipCode);
	cout << "Please enter your account balance" << "\n";
	getline(cin, accountBalance);
	cout << "Please enter the last date of your payment" << "\n";
	getline(cin, lastPayment);

	// Saves the data.
	dataFile << name;
	dataFile << address;
	dataFile << city;
	dataFile << state;
	dataFile << zipCode;
	dataFile << accountBalance;
	dataFile << lastPayment;

	cout << endl << "Saving your information on the file..." << endl;

	// CLoses the file
	dataFile.close();
}


// Definition for secondInputandDisplay ***************************************
// ****************************************************************************
int SecondInputandDisplay(fstream &dataFile)
{
	// Reopens the file for more input
	dataFile.open("C:\\Users\\Brittney\\Documents\\College Courses\\C++ 2\\Chapter 12\\Test\\userInput.txt", ios::in | ios::app);
	// Shows error message if the file didn't open correctly.
	if (dataFile.fail())
	{
		cout << "Error! The file did not open correctly." << endl;
		return 1;
	}

	// Prompts user to add more input
	cout << endl << "Hello, I have reopened the file and will be asking information one more time." << endl;
	cout << "Please enter your phone number" << "\n";
	getline(cin, phoneNu);
	cout << "Please enter your email address" << "\n";
	getline(cin, emailAddress);

	// Saves the data.
	dataFile << phoneNu;
	dataFile << emailAddress;

	cout << endl << "Saving your information again..." << endl;

	// CLoses the file
	dataFile.close();

	// Displays results
	cout << "-------------------------------------------------" << endl;
	cout << endl << "Here is all your information on the file." << endl;
	cout << endl << "Your name: " << name << "\n";
	cout << "Your address: " << address << "\n";
	cout << "Your city: " << city << "\n";
	cout << "Your state: " << state << "\n";
	cout << "Your zip code: " << zipCode << "\n";
	cout << "Your account balance: " << accountBalance << "\n";
	cout << "Your last payment: " << lastPayment << "\n";
	cout << "Your phone number: " << phoneNu << "\n";
	cout << "Your email address: " << emailAddress << "\n";
}
Would you change anything about, otherwise it works.

Yes I would get rid of the global variables. Use variables local to the functions that require them.

You should also return a value from all functions you promised you would.

There is no need to pass the stream into your functions, just make the instance local to those functions.
Ok can you give me an example? I haven't quite learned all the logical terms in c++. I know what global variables are but I don't know how to display everything in the last function without using them.
You can declare the variables in main(), then for the first function pass these variables into the function as reference parameters. For the second function you would then pass these variables by value.

See this link for more information:
http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.