Baby names assignment using structs strings and files

Pages: 1234
Hi all. My teacher is having us do this final project before we do the final exam assignment and questions and while it is technically due the last day the 19th, she suggests turning it in the 16th because thats the day the final opens. She gives us hints but honestly I find the directions kind of confusing.

Anyway, I have a file I need to save as a txt file that has 4429 entries of baby names and their popularity for 11 decades. Each of the 4429 lines in this file starts with a baby's first name and is followed by eleven (11) integers representing the popularity ranks for that name for the 11 decades from 1900 through 2005.

Print histogram for a name: The histogram line should be longer the more popular that name is. So, a rank of 1 gets the most stars (100) and a rank of 999 gets only one star. Names whose ranks are zero (0) should show no stars since the zero implies a popularity of 1000 or more. You should aim to get 100 stars for rank 1 and 1 star for rank 999. This can be done by accessing a name's rank for a specific decade and plugging it into the expression: (1000 - rank) / 10 + 1. If the entered name is not found in the list of names, then an error is printed and the menu is redisplayed for another user selection.

Compare two names in a decade: The user should be prompted to enter each of the two names and the decade value (1 through 11). Handling of errors (a name is not found or an invalid entry for decade) is shown in the example output.

Print top ten names for a decade: List the top 10 names for a given decade in order of decreasing popularity (or increasing rank: first 1, then 2, then 3, etc.). The user should be prompted for the decade in which to search. NOTE: This will display 20 names since for each popularity rank there are two name. See the example output.

Quit (write anomalies): This menu choice causes the application to terminate. However, before terminating, the application must print all anomalies found in names.txt to an output file named anomalies.txt. The file's format is defined to have two names for every decade/rank pair. For example, if examining decade 7 (1960-1969) and the rank, 9, this would yield the names, Michelle and Thomas. Any pair of a decade and a rank should correspond to two names as in the previous example. However, there are anomalies in the file such that some decade/rank pairs have only one name and a few of these pairs have no name at all.

Here is what I have so far. I am having trouble opening the txt file although I dont have it saved as one yet so that could be a problem. I am not allowed to use pointers.

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

const int SIZE=4429;
const int NAME_LEN=21;
const int NUM_RANKS=11;

struct Name{
    char name[21];
    int rank[11];
 };
void loadNames(Name []);

int main(){
    Name list[SIZE];
    char choice;
    loadNames(list);

    return 0;
}


void loadNames( Name list[]){
    ifstream nameList;
    int count=0;
    char line[4430];
    int ch;
    nameList.open("names.txt");
    while((ch=nameList.peek())!=EOF){
        nameList.getline(line,SIZE);

    };
   nameList.close();
 }

@Dmtg93

I would add an #include <string>
and change char name[21] to string name.

Under void loadNames(...) you need more to actually load your list of data into the struct. Something, but probably NOT exactly, like..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void loadNames( Name list[])
{
    ifstream nameList;
    int count=0;
     int ch;
    nameList.open("names.txt");
   do
{
  nameList >> list[count].name;
 for(ch=0;ch<11;ch++)
    nameList >> list[count].rank[ch];
count++;
}while ( names.txt);

 nameList.close();
 }


This does seem like an enjoyable, and fascinating, project to work on.
Last edited on
The most important part of this project is to load the data. Instead of reading the whole file I would start to read only one line into the struct and display it to check if it works. Once this works you can load the whole file.
1
2
3
4
5
6
7
const int NAME_LEN=21;
const int NUM_RANKS=11;
struct Name
{
    char name[21];
    int rank[11];
 };

It's good to have constants and avoid magic numbers but then it's better to use them.

I would add an #include <string>
and change char name[21] to string name.

Yes that would be the best idea but I am afraid it's one of this old, silly, stupid exercises where students should have a harder time.
I would add an #include <string>
and change char name[21] to string name.


I would but she advised against that unfortunately. I can't use strings in the struct, I have to outline it in a similar fashion for all 4429 I believe.

My teacher said:

The function, loadNames, should be called to read the entire names.txt file and store the data from each line into a struct of type Name. The main function should pass an array of Name structs to loadNames so that it can simply read a line's data into the struct at the first element of this array, then read the second line's data into the second element, etc. This should be done for all 4429 lines of the names.txt file.


So I have to do it 4429 times? Am I reading that correctly? I'm not the greatest with syntax understanding, so I don't really understand what this sentence means and what it's asking.

Thomas,

The most important part of this project is to load the data. Instead of reading the whole file I would start to read only one line into the struct and display it to check if it works. Once this works you can load the whole file.


My teacher suggested this
Create the array in main, pass it to the loadNames function and let that function populate the contents of each Name struct in the array using the data from successive lines of the text file, names.txt


So I need an array named list? The wording is throwing me off. As it is now my code isn't opening anything
So I have to do it 4429 times?
Yes, if the file has 4429 lines.

void loadNames( Name list[])
This is the correct approach. However reading the whole into the array is not sooo easy. That's the reason why I recommend to read first just one line into a Name struct and print it. Once you know how to do you can do it for the whole file.
Last edited on
Thomas,

Sounds good. That's going to be annoying to do it that many times. Jesus.

Right now I am having trouble with Eclipse reading the text file. I have it set up as above and have the text file saved in Debug because that's where its run from and have tried messing with the run configurations but I still am having issues..ugh
@Dmtg93, you will get a better help if you you show us how the file looks like. Just a line or two of the file is enough for people to get a better.

Eg. your file can be any of the following format and it will entail a different kind of code to read

Betrand 11 8 3 4 8 2 9 0 1 3
Thomas 4 6 7 3 8 2 4 6 2 2 5 

OR
Betrand 11,8,3,4,8,2,9,0,1,3
Thomas 4,8,7,3,8,2,4,6,2,2,5 


So a line or two will help
Last edited on
blongho,

C:\Users\xxx\workspace\nameApp\Debug\nameApp.exe

${workspace_loc:nameApp/Debug}

Is what it is right now. When I run the program nothing will print to the screen. Not even a cout..

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

const int SIZE=4429;
const int NAME_LEN=21;
const int NUM_RANKS=11;
//Structure used to store the data contained on one line of the file.
//name is an array of strings.
//rank is an array of int storing
struct Name{
    char name[21];
    int rank[11];
 };

void loadNames(Name []);

int main(){
    Name list[SIZE];

    loadNames(list);

    cout << "Element[21] = " << list[21].name << " and its rank[10] = " << list[21].rank[11] << endl;

    return 0;
}


void loadNames( Name list[])
{
    ifstream nameList;
    int count=0;
     int ch;
    nameList.open("names.txt");
   do
{
  nameList >> list[count].name;
 for(ch=0;ch<11;ch++)
    nameList >> list[count].rank[ch];
count++;
}while ("names.txt");

 nameList.close();
 }
I mean that you should go to the txt file and copy one or two lines and paste here. Eg the sample names you see there are from this txt file https://unsee.cc/biguteza/
Last edited on
1
2
3
4
5
6
A 83 140 228 286 426 612 486 577 836 0 0
Aaliyah 0 0 0 0 0 0 0 0 0 380 215
Aaron 193 208 218 274 279 232 132 36 32 31 41
Abagail 0 0 0 0 0 0 0 0 0 0 958
Abbey 0 0 0 0 0 0 0 0 537 451 428
Abbie 431 552 742 924 0 0 0 0 752 644 601
This is what the first part is supposed to look like..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 Welcome to nameApp.
    Use menu options to look at various statistics for names
    popular between 1900 and 2005.
    
    Select a menu option:
       a - Print histogram for a name
       b - Compare two names in a decade
       c - Print top ten names for a decade
       d - Quit (write anomalies)
    Your selection: a
    Enter a name: waLTer
    
    Histogram for name, Walter
      11: ***************************************************************************************************
      10: ****************************************************************************************************
      15: ***************************************************************************************************
      22: **************************************************************************************************
      27: **************************************************************************************************
      52: ***********************************************************************************************
      80: *********************************************************************************************
     107: ******************************************************************************************
     158: *************************************************************************************
     245: ****************************************************************************
     310: **********************************************************************


In the txt file this is the entry for Walter: Walter 11 10 15 22 27 52 80 107 158 245 310
Last edited on
I don't know why your lecturer have "refused that you should not use available tools for C++.
It would have been way easier if you were to use std::string, std::vector

EDIT

To get you started,

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>

struct Name 
{
	std::string name;
	std::vector<int> ranks;
	
	Name() {}
	Name(std::string n, std::vector<int> r): name{n}, ranks{r}{}
};

void readFiles(std::vector<Name>& nameVec);

void printHistogram(std::vector<Name> names);

void usage();

void doMenu(std::vector<Name> &names);

int main()
{
	std::vector<Name> names;
	readFiles(names);
	doMenu(names);	
	return 0;
}

// read names from file
void readFiles(std::vector<Name>& nameVec)
{
	std::ifstream infile("nameFile.txt");
	
	if (infile)
	{
		//Name struct variables
		std::string n; // to hold name
		std::vector<int> rankVec; // vector to hold the ranks

		// stuff to read from file
		int score = 0; // a rank score
		std::string line; // a line to be read from the file

		while (getline(infile, line)) // read a line, hold it and do stuff 
		{
			// process read line
			std::istringstream iss(line);
			iss >> n; // read name

			while (iss >> score) 
			{
				rankVec.push_back(score);
			}
			nameVec.emplace_back(Name(n,rankVec));
			rankVec.clear(); // clear this vector and let it ready for the next ranks
		}
	}
	else
	{
		std::cout << "\nOops! Error reading file" << std::endl;
	}
}

void printHistogram(std::vector<Name> names)
{
	std::string searchName;
	std::cout << "\n\tEnter a name : ";
	std::getline(std::cin, searchName);

	bool nameFound = false;
	for (auto& n : names)
	{
		if (n.name == searchName) // if name is on this list, do stuff
		{
			std::cout << "\t" << n.name << std::endl;

			for (auto &rank : n.ranks)
			{
				std::cout << std::setw(5) << std::right << rank << ": ";
				int rankStars = (1000 - rank) / (10 + 1);
				for (int i = 0; i < rankStars; i++)
				{
					if (rank == 0) // if rank is zero, print empty spaces
					{
						std::cout << ' ';
					}
					else // otherwise, print the number of rank stars
					{
						std::cout << "*";
					}
				}
				std::cout << std::endl; // new line after each line starts
			}
			std::cout << std::endl; // new line after each name
			nameFound = true;
			break; // if not, you will continue searching to the next match(if duplicates exist)
		}
	}
	if (!nameFound)
	{
		std::cout << "\tNo name like " << searchName << " is found in the name list\n";
	}
}

void usage()
{
	std::cout << "\n\n\tWelcome to nameApp.\n";
	std::cout << "\tUse menu options to look at various statistics for names\n\t"
		<< "popular between 1900 and 2005.\n\n";
}

void doMenu(std::vector<Name>& names)
{
	if (!names.empty()) // only show menu if names have been read
	{
		bool again = true;
		do
		{
			usage();
			std::cout << "\ta - Print histogram for a name\n";
			std::cout << "\tb - Compare two names in a decade\n";
			std::cout << "\tc - Print top ten names for a decade\n";
			std::cout << "\td - Quit(write anomalities\n";

			char userChoice = ' ';
			std::cout << "\n\tYour selection : ";
			std::cin >> userChoice;
			std::cin.get();

			switch (userChoice)
			{
			case 'a':
				printHistogram(names);
				again = true;
				break;
			case 'b':
				//do b stuff
				again = true;
				break;
			case 'c':
				// do c stuff
				again = true;
				break;
			case 'd':
				// do d stuff
				again = false;
				break;
			default:
				std::cout << "Wrong choice of character. Must be a, b, c or d\n";
				break;
			}
		} while (again);
	}
}
// nameFile.txt
A 83 140 228 286 426 612 486 577 836 0 0
Aaliyah 0 0 0 0 0 0 0 0 0 380 215
Aaron 193 208 218 274 279 232 132 36 32 31 41
Abagail 0 0 0 0 0 0 0 0 0 0 958
Abbey 0 0 0 0 0 0 0 0 537 451 428
Abbie 431 552 742 924 0 0 0 0 752 644 601
Walter 11 10 15 22 27 52 80 107 158 245 310


Results

   

        Welcome to nameApp.
        Use menu options to look at various statistics for names
        popular between 1900 and 2005.

        a - Print histogram for a name
        b - Compare two names in a decade
        c - Print top ten names for a decade
        d - Quit(write anomalities

        Your selection : a

        Enter a name : Walter
        Walter
   11: *****************************************************************************************
   10: ******************************************************************************************
   15: *****************************************************************************************
   22: ****************************************************************************************
   27: ****************************************************************************************
   52: **************************************************************************************
   80: ***********************************************************************************
  107: *********************************************************************************
  158: ****************************************************************************
  245: ********************************************************************
  310: **************************************************************



        Welcome to nameApp.
        Use menu options to look at various statistics for names
        popular between 1900 and 2005.

        a - Print histogram for a name
        b - Compare two names in a decade
        c - Print top ten names for a decade
        d - Quit(write anomalities

        Your selection : d
Press any key to continue . . .


Hope this helps
Last edited on
closed account (48T7M4Gy)
@OP You were on the right track and like Thomas said you need to get the data in and be able to take control of it - display it as a start. This is one way of doing it.

Where this gets you is the names (up to 5000 of them maximum) are in the array of names ready for processing. Leave programming the menu to last and just use it as a TODO list I suggest.

There are no vectors, strings or pointers given previously known constraints.

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>

using namespace std;

const int SIZE = 5000;
const int NAME_LEN = 21;
const int NUM_RANKS = 11;

struct Name{
    char name[NAME_LEN];
    int rank[NUM_RANKS];
};

int loadNames(Name [], int);
void displayOneHistogramBar(int);
void displayPersonHistogram(Name [], int, int);
void displayPersonBar(Name [], int, int, int);

int findName(Name [], int);
void convertToUpperCase(char []);

void displayDecade(int);
void sort(Name aList[], int actual_no, int aDecade);
int getDecadeNo();

void menu(Name [], int);

int main()
{
    int number_of_names = 0;
    Name list[SIZE];
    number_of_names = loadNames(list, SIZE);
    
    menu(list, number_of_names);
    
    return 0;
}

// READ TEXT FILE AND PREPARE NAME ARRAY
int loadNames( Name aList[], int aSize )
{
    ifstream nameList;
    nameList.open("baby_names.txt");
    
    int count = 0;
    
    if(nameList.is_open())
    {
        while(nameList >> aList[count].name )
        {
            for(int i = 0; i < NUM_RANKS; ++i)
            {
                nameList >> aList[count].rank[i];
            }
            ++count;
        }
        nameList.close();
    }
    else
        cout << "Unable to open file\n";
    
    return count;
}

// DISPLAY A SINGLE HISTOGRAM BAR GIVEN THE RANK VALUE
void displayOneHistogramBar(int aRankValue)
{
    char bar[200]{'\0'};
    int no_stars = 0;
    
    if(aRankValue > 0)
    {
        no_stars = (1000 - aRankValue) / 10 + 1;
        
        for(int i = 0; i < no_stars; ++i)
            bar[i] = '*';
    }
    cout << setw(4) << aRankValue << ": " << bar << '\n';
}

// DISPLAY ALL DECADES HISTOGRAM BARS FOR A SINGLE NAME
void displayPersonHistogram( Name aList[], int actual_no, int index)
{
    for(int i = 0; i < NUM_RANKS; ++i)
    {
        displayOneHistogramBar(aList[index].rank[i]);
    }
}

// DISPLAY A SELECTED DECADE HISTOGRAM BAR FOR A SINGLE NAME
void displayPersonBar(Name aList[], int actual_no, int index, int aDecade)
{
    int rank_no = aDecade - 1;
    cout << setw(8) << aList[index].name;
    displayOneHistogramBar(aList[index].rank[rank_no]);
}

// FIND THE INDEX OF A NAME IN THE LIST
int findName(Name aList[], int actual_no)
{
    char search_name[NAME_LEN];
    char list_name[NAME_LEN];
    
    int index = -1;
    
    while( index == -1)
    {
        cout << "Please enter a name: ";
        cin >> search_name;
        convertToUpperCase(search_name);
        
        for(int i = 0; i < actual_no; ++i)
        {
            strcpy(list_name,aList[i].name);
            convertToUpperCase(list_name);
            
            if(!strcmp(search_name, list_name))
            {
                index = i;
                return index;
            }
        }
    }
    return index;
}

// CONVERT A NAME TO ALL UPPERCASE
void convertToUpperCase(char aName[])
{
    for(int i = 0; i < strlen(aName); i++)
        aName[i] = toupper(aName[i]);
}

// DISPLAY THE DECADE START AND END YEARS GIVEN A DECADE NO. (1-11 INCLUSIVE)
void displayDecade(int aDecade)
{
    int delta = 0;
    int start = 1900 + (aDecade - 1) * 10;
    
    if(aDecade == 11)
        delta = 5;
    else
        delta = 9;
    
    cout
    << "Decade " << aDecade << ": " << start << '-' << start + delta << '\n';
}

// SORT NAMES TO GET TOP NAMES FOR A DECADE
void sort(Name aList[], int actual_no, int aDecade, int aTop)
{
    int aRank = aDecade - 1;
    Name temp;
    
    for(int j = 0; j < actual_no; j++)
    {
        for(int i = 0; i < actual_no-j; i++)
        {
            if( aList[i].rank[aRank] > aList[actual_no-1-j].rank[aRank])
            {
                temp = aList[actual_no-1-j];
                aList[actual_no-1-j] = aList[i];
                aList[i] = temp;
            }
        }
    }
    
    int count = 0, index = 0;
    while(count < aTop && index < actual_no)
    {
        if(aList[index].rank[aRank] != 0)
        {
            std::cout
            << setw(12) << aList[index].name
            << setw(5) << aList[index].rank[aRank] << '\n';
            count++;
        }
        index++;
    }
}

// GET A DECADE NUMBER (1-11 INCLUSIVE)
int getDecadeNo()
{
    int decade = 1;
    while(
          cout << "Enter decade: "
          && cin >> decade
          && (decade < 1 or decade > 11)
          )
    {
        cout << "Invalid decade\n";
    }
    return decade;
}

//MENU
void menu(Name aList[], int actual_no)
{
    char option = '0';
    while(tolower(option) != 'd')
    {
        char line[] =
        "\n+-------------------------------------------------------+\n";
        cout
        << line
        << "Welcome to nameApp.\n"
        << "Use menu options to look at various statistics for names\n"
        << "popular between 1900 and 2005.\n\n"
        << "a - Print histogram for a name\n"
        << "b - Compare two names in a decade\n"
        << "c - Print top ten names for a decade\n"
        << "d - Quit(write anomalies"
        << line
        << "Your selection: ";
        
        cin >> option;
        switch(option)
        {
            case 'a':
            {
                int index = findName(aList, actual_no);
                displayPersonHistogram(aList, actual_no, index);
                break;
            }
                
            case 'b':
            {
                int index_1 = findName(aList, actual_no);
                cout << "Name 1: " << aList[index_1].name << " found\n";
                
                int index_2 = findName(aList, actual_no);
                cout << "Name 2: " << aList[index_2].name << " found\n";
                
                int decade = getDecadeNo();
                displayDecade(decade);
                
                displayPersonBar(aList, actual_no, index_1, decade);
                displayPersonBar(aList, actual_no, index_2, decade);
                
                break;
            }
                
            case 'c':
            {
                int decade = getDecadeNo();
                displayDecade(decade);
                sort( aList, actual_no, decade, 10);
                break;
            }
                
            case 'd':
                cout << "Option d selected\n";
                break;
            default:
                cout << "*** Incorrect option ... please try again ***\n";
        }
    }
    cout << "Here is a list of anomalies encountered ...\n";
}
Last edited on
blongho,
I don't know why your lecturer have "refused that you should not use available tools for C++.
It would have been way easier if you were to use std::string, std::vector


Im not sure. This class has been a struggle all semester for me because we are so limited on what we can actually do. I don't even think we get to learn about vectors in this class. It's an intro class but man it does not feel like it at all. She said this when I asked her about it:

You should notice that the Name struct uses a C-String to store the name found on a line of the text file. You must use this struct and the c-string field, name, exactly as defined above. Using a c-strings requires library functions like, strcpy, strcmp, etc. (see textbook). If you want to use other strings in the application, you are welcome to use the data type, string, for these -- but not for the Name stuct's name field. The string type is found in many examples in the textbook.


So now Im confused. Does this mean I can use them? For your input for Walter, will you get an error if you typed waLTer or something? The program should be able to account for something like that.

I have tabbed in my textbook to be able to account for something like that. I would use toupper and tolower right? Thank you for your help.

kemort,

Thank you. I have this so far for my menu.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
do {
        displayMenu();
        choice = getUsersChoice();
        switch( choice ) {
                case 'A':
                        displayHistogram( list );
                break;
                case 'B':
                        compareTwoNames( list );
                break;  
                case 'C':
                        displayTopTenNames( list );
                break;
                case 'D':
                        writeAnomaliesToFile( list );
                        done = true;
                break;          }
} while ( !done );


The good news is is that the file I have actually opens now. It wasn't yesterday. When it loads it starts at Jamie. How do I edit to test for one line? I believe I need named constants for all 4429 names? That involves changing up
1
2
const int NAME_LEN=21;
const int NUM_RANKS=11;

right?

This is the example output for selection b
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
Select a menu option:
       a - Print histogram for a name
       b - Compare two names in a decade
       c - Print top ten names for a decade
       d - Quit (write anomalies)
    Your selection: b
    Enter a name: cAthY
    Enter a name: toDD
    Enter number corresponding to your decade:
        1 - 1900-1909
        2 - 1910-1919
        3 - 1920-1929
        4 - 1930-1939
        5 - 1940-1949
        6 - 1950-1959
        7 - 1960-1969
        8 - 1970-1979
        9 - 1980-1989
       10 - 1990-1999
       11 - 2000-2005
    Enter a decade: 8
    
    Data for Cathy
     215: *******************************************************************************
    
    Data for Todd
      43: ************************************************************************************************
@Dmtg93,
from
If you want to use other strings in the application, you are welcome to use the data type, string, for these -- but not for the Name stuct's name field. The string type is found in many examples in the textbook.
, it means one can use a std::strings.

for
So now Im confused. Does this mean I can use them? For your input for Walter, will you get an error if you typed waLTer or something? The program should be able to account for something like that.
, You will need this
http://www.cplusplus.com/forum/beginner/211832/#msg991386

How do I edit to test for one line?
Do like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if (nameList.is_open())
    {
	 while (nameList >> list[count].name) // you read a name 
	     {
		 cout << "Name read: " << list[count].name << endl;
		 cout << "Ranks for " << list[count].name << ": ";
		 for (int i = 0; i < NUM_RANKS; ++i) // read the ranks for this name
		  {
			nameList >> list[count].rank[i];
			cout << list[count].rank[i] << " ";
		   }
		++count;
		 cout << endl; // new line for new entry to be read
			
		}
		nameList.close();

	}
Last edited on
1
2
3
4
std::string to_lower( std::string str ) // return string with all alpha characters in lower case
{
    for( char& c : str ) c = std::tolower(c) ;
    return str ;

So I would put something similar to this when I want the displayed name to return like it normally should? Like could I put something like this where a displaynames function is?

I guess what is throwing me off is the deviation. I'm not familiar on how to read std::. Like does std::strings mean <strings> ?
this idea of std:: is debated widely here
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

It is not long that i started using it too. And all my course projects have worked well.

for your type of program(as i have read from that link), you can just do as you are doing i.e
1
2
3
4
5
6
7
8
9
 
#include <string>
using namespace std; 

string to_lower(string str)
{
     for(char&c: str) c = tolower(c);
     return str;
}


This will work too. Read that link to get the technicallities
Last edited on
Oh ok so it's just a better way to do it. Makes sense. You would think that teaching someone to use using namespace std would kinda be bad practice. Dunno why someone would wanna teach someone an inefficient way of doing something. Sigh.

I'll add that string thank you.

Also what does for (auto& n : names) auto mean?
closed account (48T7M4Gy)
@OP using namespace std is used particularly for beginners in C++ to avoid obscuring the key programming concepts. Don't sweat it.

I've added a few things to the earlier post to incorporate name selection. If your name list is the same format as the sample above then all you do is run the program with your data. (Make sure the .txt file name is compatible.)

Menu comes later :)
Pages: 1234