Is there a way to count the spaces in a string named line using a .get function?

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.
Last edited on
A very simple way of counting the amount of spaces is

1
2
3
4
5

int main() {
    string s = "Hello there, world!";
    cout << count( s.begin(), s.end(), ' ' ) << sendl;
}


This is the only way I know.

This will be the output -

2


Because there is 2 spaces in "Hello there, world!"
Last edited on
We haven't covered that in my book yet but I feel a little thrown to the wolves on this assignment so if I had to look up external methods, I feel like that would be reasonable.

In my code I would rewrite that as
count( line.begin(), line.end(), ' ')

Why is there "sendl" is that the same as "endl"?

Also, it looks like count is a function. Its that predefined, if so do I have to #include something? Its telling me count is not declared. Could you send me the function if I have to write it?
Yes sorry it is suppose to be endl yes.

And another sorry. You have to include #include <algorithm>
I figured out a much simpler way to do what I needed to.


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int numberOfLines = 1000; // Number of lines in the file

int main()
{
string name; // Input name to search for
int i; //Integer that will represent the number of iterations
ifstream inStream; //Declares inStream as a stream in variable
bool foundBoy = false; // Flag if we found a boy or girl name
bool foundGirl = false;

cout << "Enter name (capitalize first letter): " << endl; //Prompt and user input
cin >> name;

inStream.open("babynames2012.txt");
if (inStream.fail())//tells the user if the file doesn't exist
{
cout << "I/O failure opening file babynames2012.txt." << endl;
exit(1);
}
for (i=0; i<numberOfLines;i++) //for loop to loop for 1000 iterations
{
int rank; //declare rank as an integer
string boyName; //declase boyname as a string
string girlName; //declare girlname as a string
inStream >> rank; //1st string in each iteration of code is set to rank
inStream >> boyName; //2nd string in each iteration of code is set to boyname
inStream >> girlName; //3rd string in each iteration of code is set to girlname
//The code will go through each line of the 1,000 line list sorting the three pieces into a category one iteration at a time

if (name == boyName) //if the users input (name) matches (is equal to) a boy name on the list...
{
foundBoy = true; //the Boolean will be set to true
cout << name << " is ranked " << rank << " in popularity among boys." << endl; //and the code will cout the name (user input) and the rank (at the time the "name" matched the "boyname" on the list)
}
if (name == girlName) //if the users input (name) matches (is equal to) a girl name on the list...
{
foundGirl = true; //the Boolean will be set to true
cout << name << " is ranked " << rank << " in popularity among girls." << endl; //and the code will cout the name (user input) and the rank (at the time the "name" matched the "girlname" on the list)
}
}
// Message if not found
if (!foundBoy) //boolean "foundBoy" is false thus it went through the entire list and found no "boyname" that matched
cout << name << " is not ranked among the top 1000 names for boys." << endl;
if (!foundGirl) //boolean "foundGirl" is false thus it went through the entire list and found no "girlname" that matched
cout << name << " is not ranked among the top 1000 names for girls." << endl;
inStream.close(); //Close the stream for safe practice

system("pause");
return 0;
}
Topic archived. No new replies allowed.