Numbering concepts into a list

I've gotten my text file to read alphabetically in the output window and am now trying to list them into a numbered list. Like so:
1.Alabama
2.Alaska

I have looked online for ways to do this but have found nothing that lists concepts in numbers. Any help would be appreciated. Thank you for your time.

This is my txt file:

Alabama
Alaska
Arizona
Arkansas
California
Colorado
Connecticut
Delaware
District Of Columbia
Florida
Georgia
Hawaii
Idaho
Illinois
Indiana
Iowa
Kansas
Kentucky
Louisiana
Maine
Maryland
Massachusetts
Michigan
Minnesota
Mississippi
Missouri
Montana
Nebraska
Nevada
New Hampshire
New Jersey
New Mexico
New York
North Carolina
North Dakota
Ohio
Oklahoma
Oregon
Pennsylvania
Rhode Island
South Carolina
South Dakota
Tennessee
Texas
Utah
Vermont
Virginia
Washington
West Virginia
Wisconsin
Wyoming
American Samoa
District of Columbia
Guam
Northern Mariana Islands
Puerto Rico
United States Virgin Islands


And my code:

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
  
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	// Tell the user what the program does
	cout << "Read Concepts, number and alphabetize.\n\n";
	
	std::string line_;
	ifstream file_("LIST.txt");
	
	/*std::string number;*/
	if (file_.is_open())

	{

		{
			
			std::cout << line_ << '\n';

		}
		file_.close();
	}
	else
		std::cout << "File is not open" << '\n';
	std::cin.get();
	/////
	//////////////////////////////////////
	std::string;
	string temp;

	vector<string>names;

	//Read names from file
	ifstream in("LIST.txt");
	if (!in.is_open())
		cout << "Unable to open file \n";

	//Sort vector names
	string word;
	while (getline(in, word))
		names.push_back(word);

	sort(names.begin(), names.end());

	for (size_t i = 0; i < names.size(); i++)
		cout << names[i] << '\n';


		//Prevent concolse from closing
		cout << "\n\n\nPress any key to continue...";
		_getch();
		
    return 0;
}
Last edited on
It's actually much easier than you think.
1
2
	for (size_t i = 0; i < names.size(); i++)
		cout << i + 1 << ". " << names[i] << '\n';


Also, when posting code, keep it short and simple. Post a small working sample of your code, as people will tend to help when said code is smaller.

I just changed it and it worked perfectly! Thank you so much for your help. I've taken out the commented code and will be more mindful of posting smaller bits of code in the future.
Last edited on
Topic archived. No new replies allowed.