NEED HELP!

Create a text data file called lastname.dat which contains your lastname on the first line.

Create a text data file called firstname.dat which contains your firstname on the first line.

Write a program which reads your firstname and lastname from the respective files and displays your name in the format

lastname, firstname

in an output file named myname.dat

I've been doing this for hours. and I can't seem to figure out the erros.

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
// NameGame.cpp : Defines the entry point for the console application.
// Program to spell out my name in the order of lastname, firstname.
// Joshua Wortman
// 3/14/17

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

void Pause()
{ string junk;

  cout << "Press Enter to continue...";
  cin >> junk;
}


int main()
{
	// declare file variables
	ifstream Infile;
	ofstream Outfile;
	ofstream Logfile;

	// declare variables
	string myname;
	int lastname, firstname;
	char junk;

	// open files
	Logfile.open("f:\\myname.log");
	if (Logfile)
	{
		cout << "Log file created.\n\n";
	}
	else
	{ cout << "FAILED to open myname.log\n\n";
	Pause();
	}

	Infile.open("f:\\lastname.dat");
	if (Infile)
	{
		Logfile << "lastname.dat opened successfully.\n\n";
	}
	else
	{ Logfile << "FAILED to open lastname.dat\n\n";
	}

	Infile.open("f:\\firstname.dat");
	if (Infile)
	{
		Logfile << "firstname.dat opened successfully.\n\n";
	}
	else
	{ Logfile << "FAILED to open firstname.dat\n\n";
	}

	Outfile.open("f:\\myname.dat");
	if (Outfile)
	{
		Logfile << "myname.dat opened successfully.\n\n";
	}
	else
	{ Logfile << "FAILED to open myname.dat\n\n";
	}

	// get input
	                            // cout << "What is your last name? ";
	                            // cin >> firstname;
	getline(Infile, myname);
	if (Infile)
	{
		Logfile << "last name was read successfully= " << myname << "\n\n";
	}
	else
	{ Logfile << "FAILED to read last name\n\n";
	}
	                             // cout << "What is your first name? ";
	                             // cin >> lastname;
	getline(Infile, myname);
	if (Infile)
	{
		Logfile << "first name was read successfully= " << myname << "\n\n";
	}
	else
	{ Logfile << "FAILED to read first name\n\n";
	}

	// display results
	cout << endl << "Your name is " << lastname << ", " << firstname << endl << endl;

	Outfile << endl << "Your name is " << lastname << ", " << firstname << endl << endl;

	// close files
	Infile.close();
	Outfile.close();
	Logfile << "Program ended.\n";
	Logfile.close();


	// freeze screen
	Pause();
	return (0);
}
Well let me make a few comments.
You need to open 2 files and write to one, so you need 2 ifstream's and 1 ofstream.

Next, look up int and string, and think about which a name would be.
int lastname, firstname;
Last edited on
Topic archived. No new replies allowed.