Name program help! Urgent!

Help with C++ name code

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 4694 Views - Last Post: 22 April 2007 - 07:36 PM Rate Topic: -----

#1 eugene1017  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 22-April 07

Name program help! Urgent!

Posted 22 April 2007 - 11:42 AM

#include<iostream>
#include<string>
#include<cctype>

using namespace std;

int main()
{
		string first, last, middle;
		char initial;

		cout<<"Enter your  name: ";
		cin>>first;
		first[0] = toupper(first[0]);
		cin>>last;
		last[0] = toupper(last[0]);

		if (cin.peek() == '\n')
		{
				// no middle name
		}
		else
		{
				// middle name was read as last!
				//cout<<"What is your middle name?";
				middle = last;
				cin>>last;
				middle[0] = toupper(middle[0]);
				initial = middle[0];
		}
		cout<<"Your formal name would be: "<<last<<", "<<first;
		if (!middle.empty())
		{
				cout<<" " <<initial;
		}
		cout<<".\n";

		cout<<"Thank you for using the program!\n"
			  "Stay warm!!\n";

		return 0;
}


This is what I have so far for my C++ name code.
I need to figure out how to do such followings
If user puts in "MARY AVERAGE USER", program returns "User, Mary A."
How do I change capital letters not including first letters to lower case?

And, I need to fix it so that it allows for correct capitalization of Mc and Mac names (lke McIntyre or MacTavish)


I need help1! please help me out!
Thank you so much

This post has been edited by jayman9: 22 April 2007 - 12:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Name program help! Urgent!

#2 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 17
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Name program help! Urgent!

Posted 22 April 2007 - 11:56 AM

i think like this:
For the first letter capital and the rest not:
string needtochange;
for(int a = 0;a < needtochange.size();a++){
needtochange[a]=tolower(needtochange[a]);
}
needtochange[0]=toupper(needtochange[0]);


This post has been edited by manhaeve5: 22 April 2007 - 12:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 eugene1017  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 22-April 07

Re: Name program help! Urgent!

Posted 22 April 2007 - 12:21 PM

View Postmanhaeve5, on 22 Apr, 2007 - 11:56 AM, said:

i think like this:
For the first letter capital and the rest not:
string needtochange;
for(int a = 0;a < needtochange.size();a++){
needtochange[a]=tolower(needtochange[a]);
}
needtochange[0]=toupper(needtochange[0]);





it didn't work
So, i added
	   for (int a=0; a<first.size();a++)
		{
				first[a]=tolower(first[a]);
		}
		for (int a=0; a<last.size();a++)
		{
				last[a]=tolower(last[a]);
		}

but the complier said "comparision between signed and unsigned integer expressions"
How should I fix it?

This post has been edited by NickDMax: 22 April 2007 - 01:15 PM

Was This Post Helpful? 0
  • +
  • -

#4 vasdueva  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 03-April 07

Re: Name program help! Urgent!

Posted 22 April 2007 - 12:44 PM

Use .length() to compare the length of the string to "a"

size returns an unsigned integer.

And unsigned integer can either be positive or 0, while signed integers can be negative as well. This is where the problem arises in comparing them.

Or you could declare a an unsigned integer if you must use .size

This post has been edited by vasdueva: 22 April 2007 - 12:47 PM

Was This Post Helpful? 0
  • +
  • -

#5 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 17
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Name program help! Urgent!

Posted 22 April 2007 - 12:48 PM

it worked for me
Was This Post Helpful? 0
  • +
  • -

#6 eugene1017  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 22-April 07

Re: Name program help! Urgent!

Posted 22 April 2007 - 02:36 PM

View Postmanhaeve5, on 22 Apr, 2007 - 12:48 PM, said:

it worked for me



How do I declare an unsigned integer?????
Was This Post Helpful? 0
  • +
  • -

#7 eugene1017  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 22-April 07

Re: Name program help! Urgent!

Posted 22 April 2007 - 04:05 PM

View Posteugene1017, on 22 Apr, 2007 - 02:36 PM, said:

View Postmanhaeve5, on 22 Apr, 2007 - 12:48 PM, said:

it worked for me



How do I declare an unsigned integer?????



Help needed quickly. and none helped with the second part yet TT
Was This Post Helpful? 0
  • +
  • -

#8 vasdueva  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 03-April 07

Re: Name program help! Urgent!

Posted 22 April 2007 - 05:52 PM

unsigned Integer "var name"

I believe it would just be better to use the .length() method instead.

first.length(); instead of first.size();

The second part, well you can throw an if statement into your loop, if last[0]== m and last[1] ==c

Then assign the correct capitalization, you'll just have to come up with a list of prefixes such as that.
Was This Post Helpful? 0
  • +
  • -

#9 eugene1017  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 22-April 07

Re: Name program help! Urgent!

Posted 22 April 2007 - 06:00 PM

View Postvasdueva, on 22 Apr, 2007 - 05:52 PM, said:

unsigned Integer "var name"

I believe it would just be better to use the .length() method instead.

first.length(); instead of first.size();

The second part, well you can throw an if statement into your loop, if last[0]== m and last[1] ==c

Then assign the correct capitalization, you'll just have to come up with a list of prefixes such as that.


I changed according to your advice but the compiler still gave me an error" comparision between signed and unsigned expressions"

Can someone give me a code for the second part? for Mc and Mac

as of now my program looks something like this:

#include<iostream>
#include<string>
#include<cctype>

using namespace std;

int main()
{
string first, last, middle;
char initial;

cout<<"Enter your name: ";
cin>>first;
first[0] = toupper(first[0]);
cin>>last;
last[0] = toupper(last[0]);

for (int a = 0; a < first.length(); a++)
{
first[a]=tolower(first[a]);
}
for (int a = 0; a < last.length(); a++)
{
last[a]=tolower(last[a]);
}

if (cin.peek() == '\n')
{
// no middle name
}
else
{
// middle name was read as last!
//cout<<"What is your middle name?";
middle = last;
cin>>last;
middle[0] = toupper(middle[0]);
initial = middle[0];
}
cout<<"Your formal name would be: "<<last<<", "<<first;
if (!middle.empty())
{
cout<<" " <<initial;
}
cout<<".\n";
cout<<"Thank you for using the program!\n"
"Stay warm!!\n";

return 0;
}

This post has been edited by eugene1017: 22 April 2007 - 06:06 PM

Was This Post Helpful? 0
  • +
  • -

#10 vasdueva  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 03-April 07

Re: Name program help! Urgent!

Posted 22 April 2007 - 06:05 PM

Like i said, leave your declarations the way they are.

Instead of using .size(); insert .length(); instead, that will fix your problem.
Was This Post Helpful? 0
  • +
  • -

#11 eugene1017  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 22-April 07

Re: Name program help! Urgent!

Posted 22 April 2007 - 06:07 PM

View Postvasdueva, on 22 Apr, 2007 - 06:05 PM, said:

Like i said, leave your declarations the way they are.

Instead of using .size(); insert .length(); instead, that will fix your problem.



for (int a = 0; a < first.length(); a++)
{
first[a]=tolower(first[a]);
}
for (int a = 0; a < last.length(); a++)
{
last[a]=tolower(last[a]);
}


like this right? it still gave me an error though
Was This Post Helpful? 0
  • +
  • -

#12 vasdueva  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 03-April 07

Re: Name program help! Urgent!

Posted 22 April 2007 - 06:10 PM

After you process the correct capital letters.
If((last[0] == M) && (last[1] == c) && (last.length() > 2))
{last[3]=toupper(last[3]);}



implement this for all possible combinations you wish to capitalize. Should work
Was This Post Helpful? 0
  • +
  • -

#13 vasdueva  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 03-April 07

Re: Name program help! Urgent!

Posted 22 April 2007 - 06:16 PM

Which line does it throw the error on?
Was This Post Helpful? 0
  • +
  • -

#14 eugene1017  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 22-April 07

Re: Name program help! Urgent!

Posted 22 April 2007 - 06:19 PM

View Postvasdueva, on 22 Apr, 2007 - 06:10 PM, said:

After you process the correct capital letters.
If((last[0] == M) && (last[1] == c) && (last.length() > 2))
{last[3]=toupper(last[3]);}



implement this for all possible combinations you wish to capitalize. Should work



Thank you so much for your help with my second part!
Could you please help me with the first though?
I posted my entire code and the code for that part before you posted so it's up there not under your last reply.

Thank you!

View Postvasdueva, on 22 Apr, 2007 - 06:16 PM, said:

Which line does it throw the error on?



oh line 18 and 22

This post has been edited by eugene1017: 22 April 2007 - 06:19 PM

Was This Post Helpful? 0
  • +
  • -

#15 eugene1017  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 22-April 07

Re: Name program help! Urgent!

Posted 22 April 2007 - 06:29 PM

My entire code looks something like this:

#include<iostream>
#include<string>
#include<cctype>

using namespace std;

int main()
{
string first, last, middle;
char initial;

cout<<"Enter your name: ";
cin>>first;
first[0] = toupper(first[0]);
cin>>last;
last[0] = toupper(last[0]);

for (int a = 0; a < first.length(); a++)
{
first[a]=tolower(first[a]);
}
for (int a = 0; a < last.length(); a++)
{
last[a]=tolower(last[a]);
}

if (cin.peek() == '\n')
{
// no middle name
}
else
{
// middle name was read as last!
//cout<<"What is your middle name?";
middle = last;
cin>>last;
middle[0] = toupper(middle[0]);
initial = middle[0];
}

if ( (last[0] == 'M')&&(last[1] == 'c')&&(last.length < 2))
{
last[3]=toupper(last[3]);
}
else if ( (last[0] =='M')&&(last[1] == 'a')&& (last[2] == 'c')&&(last.length < 3))
{
last[4]=toupper(last[4]);
}


cout<<"Your formal name would be: "<<last<<", "<<first;
if (!middle.empty())
{
cout<<" " <<initial;
}
cout<<".\n";

cout<<"Thank you for using the program!\n"
"Stay warm!!\n";

return 0;
}


compiler gives me errors for line 18, 22, 41, and 45

This post has been edited by eugene1017: 22 April 2007 - 06:30 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2