ifstream question

I'm writing the babynames program,

I have a do-while loop over the entire code, so you can re-search for a name from the database. I have the babynamesFile.open() within, at the beginning of the loop, and a babynamesFile.close() and babynamesFile.clear() at the end, also within the loop.

The issue is that I cannot search a second time, it keeps the first search's information when it prints.

Is there something I'm missing?

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
 #include <iostream>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <cctype>
#include <locale>
#include <string.h>
#include <sstream>

using namespace std;

const int RANK = 1000;

void searchBabyName(ifstream& searchStream, string searchName, int& boyRank, int& girlRank);
//Precondition;  searchName contains the name we want to seach for.
//Postcondition:  boyRank and girlRank have been set to the rank
//for boys and girls for searchName.
void new_line();
//discards input remaining on the input line.


int main()

{
	string searchName;
	int boyRank, girlRank;
	char answer; // for re-searching database
	ifstream searchStream;  // variable for babyFile
	ifstream babyFile;  // the text file; 
	babyFile.open("babynames2012.txt");

	do{
	cin.clear();
	
	babyFile.open("babynames2012.txt");
	cout << "Enter name:\n>";

	getline(cin, searchName);
	char firstLetter = searchName.at(0);
	//putchar (toupper(firstLetter)); // this reverts when reinserted for some reason;
	searchName.erase(0,1);
	searchName.insert(0,1,toupper(firstLetter));






	searchBabyName(babyFile, searchName, boyRank, girlRank);
	

	cout << "For " << searchName << ", ";
	cout << "boyRank = " << boyRank << " ";

	cout << "girlRank = " << girlRank << endl;

	if (boyRank <= 0)
		cout << searchName << " is not ranked in the top 1000 boy names.\n" ;
	if (girlRank <= 0)
		cout << searchName << " is not ranked in the top 1000 girl names.\n" ;

	babyFile.close();
	babyFile.clear();
	cout << "Would you like to start another search?" << "  Type [Y]es or [N]o at the prompt: \n>" ;
	
	cin >> answer;
	new_line();

	
	
	}while (answer != 'N' && answer != 'n');

	
	
	cin.get();
	//return 0;

}



void searchBabyName(ifstream& searchStream, string searchName, int& boyRank, int& girlRank)

{
	int rankNum;
	string boyName, girlName;
	while (searchStream >> rankNum)
	{
		searchStream >> boyName;
		searchStream >> girlName;

		if (boyName == searchName)
			boyRank = rankNum;
	
		 if (girlName == searchName)
			girlRank = rankNum;
		// 
	
		
	}

	if (boyRank < 0)
		boyRank = 0;
	if (girlRank < 0)
		girlRank = 0;
			
	
	
}

void new_line()
{
char symbol;
do
	{

		cin.get(symbol);

	} while (symbol != '\n');

}
Please show the contents of your input file.

You should also always be checking that the file opened properly. And why are you opening the file before the loop, and inside the loop?

But you really don't need to close the file each time through the loop. Instead open the file before the loop and inside the loop just clear() and error flags and seek() to the beginning of the file.
Topic archived. No new replies allowed.