GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Dec-2005, 22:40
fidelljf fidelljf is offline
New Member
 
Join Date: Dec 2005
Posts: 3
fidelljf is on a distinguished road

need help on c-strings


I'm working on a problem where the user inputs the first name, middle name and last name and should produce the output, last name, first name then midde initial.
example:

input: John Smith Doe

output: Doe, John S.

the program should also work even if the user does not put the middle name.
example:

input: John Doe
output: Doe, John

So far my program takes care only of the first example and I can't figure out on how to take care of the second example.
So far here is what I got.

CPP / C++ / C Code:
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;


int main()
{ using namespace std;

	string first_name, middle_name, last_name;
	

	cout << "Enter your first name, middle name or initial and last name in that order\n";
    cin >> first_name >> middle_name >> last_name;
    cout << endl;
	cout << last_name << ", " << first_name << " " << middle_name[0] << "." << endl;
	
	return 0;
}
  #2  
Old 02-Dec-2005, 01:13
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: need help on c-strings


Hi fidel,

Welcome to GIDForums.

The second example is somewhat complicated.
Dont worry.We'll discuss how to do it.

We do not know whether the user will enter three names or not.
So, the better option is get the entire line the user enters.

This can be done using getline function.
CPP / C++ / C Code:
    string line;
    getline(cin, line);

The next step is to calculate how many strings are there in the line.
If there is only two strings, we understand that the user has not entered the middle name.
If there is three strings, the user has entered all of his names.
We are using an array of strings instead of separate names. this helps in easy coding.
CPP / C++ / C Code:
    string name[3];
Then we should initialize each of the strings like this:
CPP / C++ / C Code:
    name[0]="";
    name[1]="";
    name[2]="";


Consider that 'j' is a variable that indicate the current name.
If j is 0, then name[j] indicates the first name.
if j is 1, name[j] indicates the second name, and
if j is 2, name[j] indicates the third name.
So, initialize j to zero at the start.

Next, how to find out that how many strings are there in the line?
Here comes the problem.
We should check character by character in string line.

For example, consider that the user enters this:
Code:
John Doe


So,
  • In a loop, where 'i' varies from 0 to line.length():
  • check whether character line[i] is a space.
  • if it is a space, then increment j.
  • Else, it is not a space, and add line[i] to name[j].
  • end loop.
Something Like this:
CPP / C++ / C Code:
    for(i = 0, j = 0; i < line.length(); i++)
    {
        if( line[i] == ' ' )
            j++;
        else 
            name[j] += line[i];
    }


Then, in the end, check whether j is 0, 1 or 2.
If j is 1, the user has only entered his first name. So, print name[0].
else if j is 2, the user has entered first and last names only. So, print name[0] and name[1].
else, j is 3. So the print name[0], name[1] and name[2].

Note that this is a simple program, that works only if the user enters three strings with only one space between them.
Like this:
Code:
John Smith Doe

Suppose that the user enters like this:
Code:
John Smith Doe

Now, the program fails!
Because we have not checked for multiple spaces inbetween the words.

What can we do now?

We should increment j only once in multiple spaces that occurs in line.

So, we should increment j only if:
1. present character line[i] is a space, and
2. previous character should not_be_equal to a space.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 02-Dec-2005, 15:47
fidelljf fidelljf is offline
New Member
 
Join Date: Dec 2005
Posts: 3
fidelljf is on a distinguished road

Re: need help on c-strings


thanks a lot Paramesh
  #4  
Old 02-Dec-2005, 17:04
fidelljf fidelljf is offline
New Member
 
Join Date: Dec 2005
Posts: 3
fidelljf is on a distinguished road

Re: need help on c-strings


im still having problems. somehow it would not display the desired results

CPP / C++ / C Code:
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;


int main()
{ using namespace std;

	




    string line;

    string name[3];


	name[0]="";
	name[1]="";
	name[2]="";


    cout << "Enter your first name, middle name or initial and last name in that order\n";
    getline(cin, line);

	

	
    	for(int i = 0, int j = 0; i < line.length(); i++)
    {
        if( line[i] == ' ' )
            j++;
        else 
            name[j] += line[i];
	}

    
    

        if(j=1)
	{
		cout << name[0] << endl;
	}

	else if (j=2)
	{
		cout << name[1] << ", " << name[0] << endl;
	}

	else
	{
		cout << name[2] << ", " << name[0] << " " << name[1][0] << "." << endl;
	}

    
	cout << endl;
 
	

	
    cout << endl;
	
	
	return 0;
}

//Then, in the end, check whether j is 0, 1 or 2.
//If j is 1, the user has only entered his first name. So, print name[0].
//else if j is 2, the user has entered first and last names only. So, print name[0] and name[1].
//else, j is 3. So the print name[0], name[1] and name[2].


  #5  
Old 02-Dec-2005, 18:15
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: need help on c-strings


Hi fidel,

You cant do like this: (in the for loop)
CPP / C++ / C Code:
int i = 0, int j = 0;

And since you want to use j later in the program, you can declare them at the start itself.
Like this:
CPP / C++ / C Code:
int main()
{
    int i = 0, j = 0;
   //code....
Next, My typing fault:
Then, in the end, check whether j is 0, 1 or 2.

And should use double quotes when you are checking conditions.
Like this:
CPP / C++ / C Code:
        if(j==0)

With that change, the first hurdle is over.!

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings tripping me up once more Elsydeon C++ Forum 5 04-Dec-2005 17:41
need help - questions about strings Benayoun C Programming Language 6 24-Jan-2005 02:15
array of pointers to strings mirizar C++ Forum 5 21-Jan-2005 10:24
C++ style strings and STL dexter C++ Forum 14 04-Jan-2005 07:46
I am reviewing Arrays and need help converting some strings to arrays jenmaz C Programming Language 22 22-Nov-2004 23:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:30.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.