here is what I was suppose to do with this.
Pseudocode:
Main Method
1. Declare a string called myString.
2. Use Call-by-reference pass your string to a method called inputData(); (Hint: uses the &)
3. Use Call-by-value and pass your string to a method called outputData(); (Hint: does not use the &)
inputData Method (Receives string)
1. Get input using cin >> into the string variable you received into this method. (I will be entering my full name in the format of: Last, First Middle.
2. Using the string methods mentioned in this weeks lecture, have your program change the string to: First Middle Initial Last.
3. I would like you to break the string you received apart into 3 separate variables: first, last, middle. (Hint: use find() to find the "," and spaces and use substr() to extract the pieces)
4. Use string concatenation to reassemble the string and overwrite the string you received with the new value.
5. You must use each of the following string methods (at least once): substr(), length(), find(), insert(), erase().
6. Notice the "," is missing out of the name.
outputData Method (Receives string)
1. Output the new string to the screen using cout <<.
2. Comments are required, but pseudocode or flowchart are not.
3. All Input/Output should be done using the standard C++ library string class.
4. Include: system("PAUSE"); after your output to pause the screen.
with the code provided I was trying to make sure the user input was accurate. Where I have a problem is doing the call by reference which I understand requires an & but how is the part I guess I am trying to ask.
PS I fixed the loop saw that it wasn't in the look like you said after posting
This is what I got thus far:
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <iostream>
#include <string>
using namespace std;/*
void inputData(string& first, string& middle, string& last)
{
last =" " ;
first =" " ;
middle =" " = midddle.erase(1);
}
*/
int main()
{
string myString;
cout << "Please enter your Last, First Middle Name" << endl;
cout << endl;
cout << endl;
getline(cin, myString);
int comma = myString.find (","); // use this to make sure comma is equal to reversecomma since there can only be one comma
int reversecomma = myString.rfind(","); //to compare with comma
int totalName = myString.length(); // get the over all lenght
int firstSpace = myString.find(' '); // this finds the first space I was using rfind to find the second but found if there
int secondSpace = myString.find(' ', firstSpace + 1);// is a third white-space it still would include it when finding.
int thirdSpace = myString.find(' ', secondSpace + 1); // this is to test if when inputted someone hits the spacebar afterwords
while ((comma != reversecomma) || ((reversecomma + 2)>= secondSpace) || (firstSpace == 0) || (thirdSpace != -1))
{//Here I begin testing inputs to see if accurate notice comma has to equal reversecomma if not starts loop then I use
//reversecomma which measures from the end to beginning and add 2 to the int which is how many spaces it should include
// firstSpace happens if someone puts a space before last name
// finally thirdSpace is to test to see if there is any spaces after Middle name.
cout << "You need to have a comma in between Last and First name only" << endl;
cout << "For example Doe, John Ericson or you may have to many white-spaces" << endl;
cout << "Please try again enter Last, First Middle Name" << endl;
getline(cin, myString);
comma = myString.find (",");
reversecomma =myString.rfind(",");
totalName = myString.length();
firstSpace = myString.find(' ');
secondSpace = myString.find(' ' , firstSpace + 1);
thirdSpace = myString.find(' ', secondSpace + 1);
}
// now that it as gone through the checks to make sure information is accurate I can begin setting up container so I will need
// prototype this is located before Main();
string b = myString.substr(0, comma);
b.insert(0, " ");
cout << b << endl;
string a = myString.substr(firstSpace , (secondSpace - firstSpace));
cout << a << endl;
cout << firstSpace << endl << secondSpace << endl << totalName << endl;
string c = myString.substr(secondSpace + 1, (totalName - secondSpace)+ 1);
c.insert(0, " ");
cout << a << b << c << endl;
cout << endl;
cout << endl;
system("PAUSE") ;
return EXIT_SUCCESS;
}
|