#1. I have to Write a program that reads the contents of both files to two separate vectors. Prompt the user to enter a boy’s name, a girl’s name or both (a boy’s and a girl’s name). The code should then display a message indicating whether the entered name was among the most popular list of names.
What I have so far from following the algorithm my teacher gave me, the text book, and any online source I could find:
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
|
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
const int RANK = 200;
int n;
int main()
{
ifstream names;
string target, boyname, girlname;
bool findBoy, findGirl;
int rank, i;
int rankArray[RANK];
string boyArray[RANK];
string girlArray[RANK];
vector<string> searchHistory;
names.open("BoyNames.txt");
names.open("GirlNames.txt");
cout << "This program will determine whether a name is a popular baby name for a boy or a girl." << endl;
cout << "(Btw, if you're expecting, congratulations!)" << endl << endl;
for (i = 0; i<RANK; i++){
names >> rank >> boyname >> girlname;
rankArray[i] = rank;
boyArray[i] = boyname;
girlArray[i] = girlname;
}
while (true) {
cout << "Type a boy's name, a girl's name, or both." << endl;
cin >> target;
cout << endl;
for (n = 0; n<RANK; n++)
{
if (target == boyArray[n]){
cout << target << " is ranked " << (n + 1) << " in popularity among boys." << endl;
findBoy = true;
}
if (target == girlArray[n]){
cout << target << " is ranked " << (n + 1) << " in popularity among girls." << endl;
findGirl = true;
}
}
if (findBoy != false) cout << target << " is not a popular boy names." << endl;
if (findGirl != false) cout << target << " is not a popular girl name." << endl;
}
return 0;
}
|
The problem: Even if I type a name that's on both lists, the program will tell me that "[blank] is not a popular boy/girl name"
#2. I'm given this code:
1 2 3 4 5
|
int doSomething(int &x, int &y)
{ int temp = x;
x = y * 10;
y = temp * 10;
return x + y; }
|
I'm supposed to rewrite the function so that it uses pointers instead of reference variables. Demonstrate that the function works by using it in a complete program.
The closest I could get to before I started completely drawing a blank is this:
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
|
#include <iostream>
using namespace std;
void dosomething(int *);
int main()
{
int x, y;
int output;
dosomething(&x, &y);
cout << "Enter a Value for X: ";
cin >> x;
cout << "Enter a Value for Y: ";
cin >> y;
}
void dosomething(int *temp)
{
*temp *= 10;
}
|
The output is supposed to be something along these lines:
"Please enter values for x and y (separated by a space): 2 3
I will now call the original (given) function ...
The value returned from the original function is 50"
I've tried about every different combination I could think of but nothing seems to be working. Help would be greatly appreciated :)