Help please !!! No if...else statements.

F

farah727rash

Hey All,
This is a program to extract the middle name from a person's full name,
but it only works for a person who has a first, middle and a last name.
What do I do so that it works for a person who has just a first and
last name? For example: Fabio or Santa Claus. I am not supposed to use
conditional statements, other wise using if...else it would have been
so easy. I can't think of a way to do it without if...else. Any
ideas/suggestions would be great. Thanks a lot, Farah.


/*
----------------------------------------------------------------------
Program: Program 6.cpp
Purpose: This program extracts the middle name from a person's full
name
Written: Farah Rasheed
Date : 10/12/06
Version: .NET
------------------------------------------------------------------------*/

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main(void)
{
// Purpose of the program
cout << "The program reads your full name extracts the middle name."
<< endl << endl;

// Read in the full name
cout << "Enter your full name: ";
cin.ignore();
string name;
getline(cin, name, '\n');

int lastBlankPos = name.rfind(" ");
int nextToLastBlankPos = name.find(" ", 0);
int length_of_middle_name = lastBlankPos-nextToLastBlankPos;
//cout << lastBlankPos << "\t" << nextToLastBlankPos << "\t" <<
length_of_middle_name<<"\n";

string middle_name = name.substr(nextToLastBlankPos,
length_of_middle_name);

cout << middle_name << endl << endl;

// End of the program
return 0;
}
 
K

Kai-Uwe Bux

Hey All,
This is a program to extract the middle name from a person's full name,
but it only works for a person who has a first, middle and a last name.
What do I do so that it works for a person who has just a first and
last name? For example: Fabio or Santa Claus. I am not supposed to use
conditional statements, other wise using if...else it would have been
so easy. I can't think of a way to do it without if...else. Any
ideas/suggestions would be great.
[snip]

I consider such puzzles rather pointless. But anyway, one way to fake an
if-statement comes to mind: you can exploit [5.14/1] which says that
boolean expressions use conditional evaluation, i.e., when you write

condition && some_boolean_func( args );

then some_boolean_func( args ) is only evaluated when condition is true.


Best

Kai-Uwe Bux
 
F

Frederick Gotham

(e-mail address removed) posted:
This is a program to extract the middle name from a person's full name,
but it only works for a person who has a first, middle and a last name.


Possible strategy:

View the string as

[sequence of characters] space [sequence of characters] space [sequence
of characters] nullterminator

What do I do so that it works for a person who has just a first and
last name? For example: Fabio or Santa Claus.


You don't. You'll have to make one up for them. May I suggest "Jason"?

I am not supposed to use conditional statements, other wise using
if...else it would have been so easy.


Ther ternary operator is your friend.

I can't think of a way to do it without if...else.
int main(void)
{
// Purpose of the program
cout << "The program reads your full name extracts the middle name."
<< endl << endl;


You must include <ostream> to make use of "endl".

Here's a possible algorithm:

#include <cassert>
#include <cstring>
#include <string>

bool EnsureAtLeastTwoWordsFollowedByOneSpaceEach(char const *p)
{
assert(p);

char const *q = std::strchr(p,' ');

if(!q || (q==p)) return false;

p = std::strchr(++q,' ');

if(!p || (q==p)) return false;

return true;
}

std::string ExtractSecondWord(char const *p)
{
p = std::strchr(p,' ') + 1;

char const *const plast = std::strchr(p,' ');

return std::string(p,plast);
}

#include <iostream>
#include <ostream>

int main()
{
std::cout << "Enter three words: ";

std::string str;

std::getline(std::cin,str);

char const *p;

if (EnsureAtLeastTwoWordsFollowedByOneSpaceEach(p = str.c_str()))
std::cout << "\n\n" << ExtractSecondWord(p) << std::endl;
}


It really depends on what exactly you're trying to do...
 
F

farah727rash

Thanks for the help, but like I said, this is tje intro to C++ course,
and we have just had three one-and-half-hour lectures till now. so, all
this stuff is really way over my head. Can't you think of something
simple our professor wants from us instead? Thanks again, Farah.

Frederick said:
(e-mail address removed) posted:
This is a program to extract the middle name from a person's full name,
but it only works for a person who has a first, middle and a last name.


Possible strategy:

View the string as

[sequence of characters] space [sequence of characters] space [sequence
of characters] nullterminator

What do I do so that it works for a person who has just a first and
last name? For example: Fabio or Santa Claus.


You don't. You'll have to make one up for them. May I suggest "Jason"?

I am not supposed to use conditional statements, other wise using
if...else it would have been so easy.


Ther ternary operator is your friend.

I can't think of a way to do it without if...else.
int main(void)
{
// Purpose of the program
cout << "The program reads your full name extracts the middle name."
<< endl << endl;


You must include <ostream> to make use of "endl".

Here's a possible algorithm:

#include <cassert>
#include <cstring>
#include <string>

bool EnsureAtLeastTwoWordsFollowedByOneSpaceEach(char const *p)
{
assert(p);

char const *q = std::strchr(p,' ');

if(!q || (q==p)) return false;

p = std::strchr(++q,' ');

if(!p || (q==p)) return false;

return true;
}

std::string ExtractSecondWord(char const *p)
{
p = std::strchr(p,' ') + 1;

char const *const plast = std::strchr(p,' ');

return std::string(p,plast);
}

#include <iostream>
#include <ostream>

int main()
{
std::cout << "Enter three words: ";

std::string str;

std::getline(std::cin,str);

char const *p;

if (EnsureAtLeastTwoWordsFollowedByOneSpaceEach(p = str.c_str()))
std::cout << "\n\n" << ExtractSecondWord(p) << std::endl;
}


It really depends on what exactly you're trying to do...
 
G

gw7rib

Hey All,
This is a program to extract the middle name from a person's full name,
but it only works for a person who has a first, middle and a last name.
What do I do so that it works for a person who has just a first and
last name? For example: Fabio or Santa Claus. I am not supposed to use
conditional statements, other wise using if...else it would have been
so easy. I can't think of a way to do it without if...else. Any
ideas/suggestions would be great. Thanks a lot, Farah.

How about something along the following lines? I'm assuming that the
name which goes in is a c-type string, and that you are allowed to
modify it (for example, it's not a literal). You were only wanting a
hint so I'll happily leave any necessary modifications up to you...

char *middlename(char *name) {
char *b, *e;

for (b = name; name != 0 && name != ' '; b++);

for (e = name + strlen(name) - 1; e > b && name[e] != ' '; e--);

name[e] = 0;

return name+b;
}

This is completely untested, but the idea is that -

firstly we set b to where the second name starts, or the end of the
string if there is only one name;

secondly, we set e to where the final name starts, but not going back
before b

we mark e as the end of the string to return

we return the string starting from b

As it stands, the returned string will have more spaces than you want,
but it should at least fish out the required name. Hope it is helpful!

Paul.
 
D

dieu.tout.puissant

You can replace, in your name string, the first and last name by an
error message. Thus, if the whole name has less than 3 separate parts,
you will extract an error message instead of a middle name.

Here is an implementation of this trick (I tried to indroduce as few
new concepts as possible):

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main(void)
{
// Purpose of the program
cout << "The program reads your full name extracts the middle name."
<< endl << endl;

// Read in the full name
cout << "Enter your full name: ";
string name;
getline(cin, name, '\n');

// Replace last name by an error message
int lastBlankPos = name.rfind(" ") + 1;
name.replace(lastBlankPos,name.size(),"<error>");

// Replace first name by an error message
int firstBlankPos = name.find(" ");
name.replace(0,firstBlankPos,"<error>");

//Take the middle name from the newly constructed name
firstBlankPos = name.find(" ") + 1;
lastBlankPos = name.rfind(" ");

int length_of_middle_name = lastBlankPos-firstBlankPos;
string middle_name = name.substr(firstBlankPos,
length_of_middle_name);
cout << middle_name << endl << endl;

// End of the program
return 0;
}

The design and syntax of the program can be improved depending on your
c++ knowledge.
 
G

gw7rib

Hey All,
This is a program to extract the middle name from a person's full name,
but it only works for a person who has a first, middle and a last name.
What do I do so that it works for a person who has just a first and
last name? For example: Fabio or Santa Claus. I am not supposed to use
conditional statements, other wise using if...else it would have been
so easy. I can't think of a way to do it without if...else. Any
ideas/suggestions would be great. Thanks a lot, Farah.

How about something along the following lines? I'm assuming that the
name which goes in is a c-type string, and that you are allowed to
modify it (for example, it's not a literal). You were only wanting a
hint so I'll happily leave any necessary modifications up to you...

char *middlename(char *name) {
char *b, *e;

for (b = name; name != 0 && name != ' '; b++);

for (e = name + strlen(name) - 1; e > b && name[e] != ' '; e--);

name[e] = 0;

return name+b;
}


Whoops! You can either use character pointers for b and e, or have then
as ints indicating a position in name, but you do have to decide
which... Here are a couple of versions which have survived a minimal
amount of testing:

char *middlename1(char *name) {
char *b, *e;


for (b = name; *b != 0 && *b != ' '; b++);


for (e = name + strlen(name) - 1; e > b && *e != ' '; e--);

while (b < e && *b == ' ') b++;
while (e > b && *(e-1) == ' ') e--;

*e = 0;

return b;
}

or

char *middlename2(char *name) {
int b, e;


for (b = 0; name != 0 && name != ' '; b++);


for (e = strlen(name) - 1; e > b && name[e] != ' '; e--);

while (b < e && name == ' ') b++;
while (e > b && name[e-1] == ' ') e--;

name[e] = 0;


return name+b;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top