Disclosure: I am very new to programming and am only in week 4 of my c++ class.
Is there a way to count the spaces in a string named line using a .get function?
The reason I want to use a ".get" is because we have learned it already. I don't want to use something I haven't learned or don't understand. I want to be able to complete my homework in a way that enhances my understanding, not simply copying and pasting something far more advanced.
Here is the Homwork Question: "The text file babynames2012.txt...contains a list of the 1000 most popular boy and girl names in the United States for the year 2012 as compited by the Social Security Administration.
This a space-delimited file of 1000 entries in which the rank is listed first, followed by the corresponding boy name and girl name. The most popular names are listed first and least popular names are listed last. For example, the file begins with
1 Jacob Sophia
2 Mason Emma
3 Ethan Isabella
This indicates that Jacob is the most popular boy name and Sophia is the more popular girl name. Mason is the second most popular boy name and Emma is the most popular girl name.
Write a program that allows the user to input a name. The program should then read from the file and search for a matching name among the girls and boys. If a match is found, it should output the rank of the name. The program should also indicate if there is no match.
For example, user inputs "Justice."
Justice is ranked 519 in popularity among boys.
Justice is ranked 518 in popularity amoung girls.
user inputs "Walter"
Walter is ranked 376 in popularity among boys.
Walter is not ranked among the topp 1000 girl names.
Here is my code so far.
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
|
#include <cstring>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string name; //Will be users input of name to search
string girlsOrBoys; //to distinguist if a girl or boy
int spaceCount = 0; //to count the spaces in line
char checkSpace; //to be used to check if a space
string line; //The whole line of text
int lineNumber = 0 ; //to count the lines
cin >> name; //User inputs a name
ifstream inFileName( "babynames2012.txt" ) ; //streams in file
while( getline( inFileName, line ) )
{
++lineNumber ;
if( line.find(name) != string::npos ) //searches the file for the name
{
//line.get (checkSpace);// attempt to read each charachter in line; does !work
cout<< checkSpace;
if (isspace (checkSpace))//I'd need to nest this into a loop to make sure it does this for each character
{
spaceCount++;
cout << spaceCount;
if (spaceCount == 1)
{
girlsOrBoys = "boy's";
}
else if (spaceCount == 2)
{
girlsOrBoys = "girl's";
}
else
{
cout << "It is not counting the spaces in the line."; //this doesnt happen because the original if doesn't know where to check the space.
}
}
cout << name << " is ranked " << lineNumber << " amongst " << girlsOrBoys << " names.";
cout << endl << "The line is " << line;
}
}
}
|
My problem is I don't know how to search the string "line" for characters to use an "isspace" boolean to count the spaces and tell if it is in the boy column or girl column. I'm sure there is an easier way to do this but this is the method I came up with so if anyone could help me figure out how to write that method into code it would be greatly appreciated.