473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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.

Here's my program:
/* ----------------------------------------------------------------------
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;
}
Oct 15 '06 #1
2 1691
Any ideas, anyone? Please, some help will be great.
Oct 15 '06 #2
Banfa
9,065 Expert Mod 8TB
The statement of what the program does and what it needs to be modified to do are not logically consistent

"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?"

It is a program to extract the middle name for a string containing a first middle and last name. It is not possible to alter it to extract the middle name for a string containing only a first and last name.

So either the problem is unsolveable or you have not correctly stated it. What action should be taken if there is only a first and last name?
Oct 16 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: newbie_mw | last post by:
Hi, I need urgent help with a novice problem. I would appreciate any advice, suggestions... Thanks a lot in advance! Here it is: I created a sign-up sheet (reg.html) where people fill in their...
1
by: newbie_mw | last post by:
Seems my post was buried in more cries for help :-) I will try again. It's probably a very novice question so please take a look! Thanks!...
5
by: josh dismukes | last post by:
/// Were having serious problems with this script it's basically the map script allowing player movement but the problem is in the QUERY and if statements on the cell type. Any help will be much...
8
by: Nathan Pinno | last post by:
Hi all, I need help figuring out how to fix my code. I'm using Python 2.2.3, and it keeps telling me invalid syntax in the if name == "Nathan" line. Here is the code if you need it. #This...
4
by: dwight0 | last post by:
I am having a problem with a query, I am not sure if i would use a join or a subquery to complete this problem. I have two queries, and i need to divide one by the other, but i cant seem to get...
2
by: thx606 | last post by:
Hello, I'm having some trouble with my code below. One of my If statements causes the code not to work. If I take the If Statement out it runs fine. I highlighted the area that I'm having...
3
by: Amy | last post by:
Hi, I have 6 If Then Else statements I was supposed to write. I did so but I know that they have to be wrong because they all look the same. Could someone take a look at them and point me in the...
14
by: morebeer | last post by:
I got the same problem, hundreds of SQL tables been infected with this malicious javascript code. But although closing the original injection leak and also having replaced all strings in all...
3
by: gmdune | last post by:
Hi All, I have written a program that doesn't seem to work and I can't figure out why it's not working. It compiles correctly, but when I run it I get prompted by the first couple of printf...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.