My code is not clearing cin (please run it)

if you run this beuatafal prgom you will see that the entered name will not reset, so you run it once, it works, you choose yes at the prompt to re=run the program, it will keep the original (first) information.

y u do dis?

clearing the the file makes no sense to me. it shouldn't be the file, but the cin. and i alrady tried to clear it. halp1 me && thank you.

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
  #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;
	int rankNum;  
	char c;			// for uppercase default name
	char answer; // for re-searching database
	ifstream searchStream;  // variable for babyFile
	ifstream babyFile;  // the text file; 
	babyFile.open("babynames2012.txt");

	do{

	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" ;


	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');

	babyFile.close();
	babyFile.clear();
	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');

}
when a loop is run, getline is only run once, even if the loop is restarted. Try cin>>searchName;
Last edited on
Topic archived. No new replies allowed.