Need Help with capitalizing first letter of name question

Hi, I was helping problems with the following question:

Write the definition of the class below. The insertion operator is overloaded to
display the name and year of birth of a Person object (e.g., “Galileo Galilei was born in 1564”). Member function getName returns the name of the Person capitalizing only the beginning of every word in the name; for example if name is “lEonArDo DA VINCi”, getName should return “Leonardo Da Vinci”. Submit only the definition of the function getName and the function that overloads the insertion operator; nothing else.

The problem is that the code should displayed the converted name and then "was born in year something". For example, if I put in (rAVniK JAgPaL", 1997) in the main function, I believe it should display, "Ravnik Jagpal was born in 1997".
Instead what happens is that it displays "Ravnik JagpalRavnik Jagpal".

Also could someone please explain the bolded lines of code, i really dont understand them, a friend told me to write them but I cant understand his explanation of it.

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 <string>
using namespace std;

class Person
{
public:
	Person(string n, int year) : name(n), yearOfBirth(year){}
	
	string getName()
	{
		int size = name.length(); // length of name is assigned to name
		if (name[0] != ' ')		//checks to see if first element of array is a space
		{
			name[0] = putchar(toupper(name[0]));	//puts first element to upper case
		}
		for (int i = 1; i < size; i++)
		{
			if (name[i] == ' ')		//checks if an element is equal to a space
			{
				name[i] = putchar(toupper(name[i]));	//space is set to upper case
				name[i + 1] = putchar(toupper(name[i + 1]));	//the element next to space is set to upper case
				i++;	//i is incremented
			}
			else
			{
				name[i] = putchar(tolower(name[i]));
			}
		}
		return name;	
	}
	
	friend ostream& operator <<(ostream& i, const Person& per);

private:
	string name; int yearOfBirth;
};

ostream& operator <<(ostream& i, const Person& per)
{
	cout << per.name << " was born in " << per.yearOfBirth;
	return i;
}

int main()
{
	Person Name("rAVniK JAgPaL", 1997);
	cout << Name.getName() << endl;
	
	system("Pause");
	return 0;
}
Last edited on


In your main

1
2
Person Name("rAVniK JAgPaL", 1997);
	cout << Name.getName() << endl;


You have nothing to cout the year....
Perhaps you left out a function, or a call to one...

Line 38 - 40
1
2
3
4
5
ostream& operator <<(ostream& i, const Person& per)
{
	cout << per.name << " was born in " << per.yearOfBirth;
	return i;
}


don't appear to be used.
yea thats the problem, i dont completely understand how to show it, could u maybe help on how i can show that, cause im new to overloading and not sure on how to show it. Also, if u can could u explain the bold lines i mentioned. Thanks
Last edited on
closed account (E0p9LyTq)
The friendly operator<< overload is so std::cout "knows" how to output your class:

1
2
3
4
5
6
7
int main()
{
   Person Name("rAVniK JAgPaL", 1997);
   cout << Name << endl;

   return 0;
}


That still doesn't address how to properly capitalize your name string, the logic in getName() is IMO a total mess.

I'd suggest looking at std::string's find operators, there are example programs to give you a hand in understanding how they work.

The example for std::string::find_first_of looks promising for your needs:

http://www.cplusplus.com/reference/string/string/find_first_of/
Yeah, you make a function for year the same way you did for name...
The lines have explanations on them It doesn't get much simpler... not sure what I can add to them.

http://www.cplusplus.com/reference/locale/toupper/
http://www.cplusplus.com/reference/cstdio/putchar/?kw=putchar


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
string getName()
	{
		int size = name.length(); // length of name is assigned to name
		if (name[0] != ' ')		//checks to see if first element of array is a space
		{
			name[0] = putchar(toupper(name[0]));	//puts first element to upper case
		}
		for (int i = 1; i < size; i++)
		{
			if (name[i] == ' ')		//checks if an element is equal to a space
			{
				name[i] = putchar(toupper(name[i]));	//space is set to upper case
				name[i + 1] = putchar(toupper(name[i + 1]));	//the element next to space is set to upper case
				i++;	//i is incremented
			}
			else
			{
				name[i] = putchar(tolower(name[i]));
			}
		}
		return name;	
	}



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
#include <iostream>
#include <string>
using namespace std;

class Person
{
public:
	Person(string n, int year) : name(n), yearOfBirth(year){}
	
	int getYear(){ return yearOfBirth;}
	
	string getName()
	{
		int size = name.length(); // length of name is assigned to name
		if (name[0] != ' ')		//checks to see if first element of array is a space
		{
			name[0] = putchar(toupper(name[0]));	//puts first element to upper case
		}
		for (int i = 1; i < size; i++)
		{
			if (name[i] == ' ')		//checks if an element is equal to a space
			{
				name[i] = putchar(toupper(name[i]));	//space is set to upper case
				name[i + 1] = putchar(toupper(name[i + 1]));	//the element next to space is set to upper case
				i++;	//i is incremented
			}
			else
			{
				name[i] = putchar(tolower(name[i]));
			}
		}
		return name;	
	}
	
private:
	string name; int yearOfBirth;
};


int main()
{
    Person Name("rAVniK JAgPaL", 1997);
	std :: cout << Name.getName();
	std :: cout << " " << Name.getYear() <<std :: endl;
	system("Pause");
	return 0;
}
Last edited on
Thanks, but what I dont understand by the bold lines is that how could spaces be uppercase or lower case, a space is a blank space isnt it?Whats meant by putchar? Also, why does the same line that shows up in the if statement come again in the else part?

And again for the code u replied with shows the year but doesnt have the overloaded operator anymore, the problem is that i need that operator part of the code, and it needs to display, ex. "Ravnik Jagpal was born in 1997". Instead it displays, "Ravnik JagpalRavnik Jagpal 1997". Thanks.
Last edited on
closed account (E0p9LyTq)
RAVSHAN02 wrote:
Instead it displays, "Ravnik JagpalRavnik Jagpal 1997". Thanks.


That is the fault of using putchar() when changing the capitalization. You are outputting the name string twice.

Admittedly messy rewrite, but it works with the operator<< overload:

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
#include <iostream>
#include <string>

class Person
{
public:
   Person(std::string n, int year) : name(n), yearOfBirth(year) {}

   std::string getName()
   {
      int size = name.length(); // length of name is assigned to name
      if (name[0] != ' ')		//checks to see if first element of array is a space
      {
         name[0] = toupper(name[0]);	//puts first element to upper case
      }
      for (int i = 1; i < size; i++)
      {
         if (name[i] == ' ')		//checks if an element is equal to a space
         {
            name[i] = toupper(name[i]);	//space is set to upper case
            name[i + 1] = toupper(name[i + 1]);	//the element next to space is set to upper case
            i++;	//i is incremented
         }
         else
         {
            name[i] = tolower(name[i]);
         }
      }
      return name;
   }

   friend std::ostream& operator <<(std::ostream& i, Person& per);

private:
   std::string name;
   int yearOfBirth;
};

std::ostream& operator <<(std::ostream& i, Person& per)
{
   std::cout << per.getName() << " was born in " << per.yearOfBirth;
   return i;
}

int main()
{
   Person Name("rAVniK JAgPaL", 1997);

   std::cout << Name << std::endl;

   return 0;
}


I still recommend you look into the string find operators.

Something to note, your getName() permanently changes the name data member. Is that something you want? If you don't, then copy the name string to a temporary string and modify and return that string.
Thanks so much, it actually works as it should now. I appreciate it. But, if u dont mind, could u explain the following section from the code:
I dont understand how a space can be upper case or lower case? Isnt it just a blank space? i just need an explanation on how it works. Thanks.
1
2
3
4
5
6
7
8
9
10
if (name[i] == ' ')		//checks if an element is equal to a space
         {
            name[i] = toupper(name[i]);	//space is set to upper case
            name[i + 1] = toupper(name[i + 1]);	//the element next to space is set to upper case
            i++;	//i is incremented
         }
         else
         {
            name[i] = tolower(name[i]);
         }
Last edited on
closed account (E0p9LyTq)
http://www.cplusplus.com/reference/cctype/toupper/
http://www.cplusplus.com/reference/cctype/tolower/

Simply put:

The if code block changes the case of the characters in your name string.
Also, so for bothering again but how does the following overloaded function work? I read the chapter in c++ for overloading but still dont get it. Also, what did u really change to make the year appear on the screen, how did removing getName change it? I no this might be alot to ask for but im really new to this stuff and im having a hard time understanding it even after reading through the book. Thanks.

1
2
3
4
5
std::ostream& operator <<(std::ostream& i, Person& per)
{
   std::cout << per.getName() << " was born in " << per.yearOfBirth;
   return i;
}
Last edited on
closed account (E0p9LyTq)
RAVSHAN02 wrote:
im having a hard time understanding it even after reading through the book.


Welcome to my world! ;)

+=========+

cout is written to deal with the built-in data types, ints, floats, strings, etc.

Your class created a custom data type, and the operator<< function overload is used so cout is able to work with your class as seamlessly as it does the other data types.
So what u mean is that the << operator in the main is pretty much calling the overloaded ostream& operator which gets the converted name from the getName() and the year and then displays them?
Topic archived. No new replies allowed.