problem with output file

After the program runs it creates the file but the LineUp.dat file just reads "1054C9AC1054C9AC1054C9AC1054C9AC" instead of putting what entered in.

What im trying to get in the output file is all the Names of students I input.

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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{ 
	int numStd; // Number of students in the class
	string	top, // Alphabetically the "first" name on list
			bottom, // Alphabetically the "last" name on list
			name; // Holds a name input by the user 
	fstream outFile;

	outFile.open("LineUp.dat",ios::out|ios::trunc);

	
	cout << "This program will display the student names in alphabetical order"<<endl;
	cout << "How many students are in your class? ";
	cin >> numStd;
	while (numStd < 1 || numStd > 25)
		{
		cout<< "Please re-enter a number of students between 1-25: ";
		cin >> numStd;
		}
		cout<<"Please type your first student's name: "; 
			cin.ignore();
			outFile<<getline (cin, name);
			top = bottom = name; // initialize both ends of the line to first name

		for (int std =2; std <= numStd; std++)
		{ 
			cout<<"Please enter name of the next student: ";
			outFile<<getline (cin, name);

		if (name < top)
		top = name;
		else if (name > bottom)
		bottom = name;
		}
		
		// Display results
		cout<<"The person at the front of the line will be "<<top<<endl;
		cout<<"The person at the end of the line will be "<<bottom<<endl;

	outFile.close();
	cout<<"\nDone.\n";

system("pause");
return 0;
}
Last edited on
I don't think getline(cin,name); will return the string for outfile...you are probably meaning something like:

1
2
getline(cin, name);
outFile<<name<<endl;
Topic archived. No new replies allowed.