姓氏没有回来 [英] last name is not returning
问题描述
我有这个小项目应该从输入读取并通过使用Call by reference和按值调用返回它。我完成了99%(我认为)哈哈。但由于某种原因,姓氏不会返回。诀窍是,用户将以Last,First MI格式输入,返回将在First MI Last中显示。它现在的方式是返回Luis D.并且姓氏在行动中缺失
这是我的代码
I have this small project that is supposed to read from an input and return it by using Call by reference and call by value. I have it 99% done ( I think ) lol. But the last name, for some reason is not returning. The trick is, the user will input in a Last, First MI format and the return will show it in First MI Last. The way it is right now is returning Luis D. and the Last name is missing in action
Here's my code
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void inputData(string &); //Declare Call-by-reference function
void outputData(string); //Declare Call-by-value function
int main() {
string myString;
inputData(myString);
outputData(myString);
}
void inputData(string &myString) {
cout << "Last, First, MI : ";
cin >> myString;
getline( cin, myString );
istringstream parse( myString );
string first, mi, last;
parse >> first >> mi >> last;
if ( last.empty() ) swap( mi, last );
cout << myString << " by reference " << endl; //Pass-by-reference
}
void outputData(string myString) {
cout << myString << " by value " << endl; //Pass-by-value
}
我在大学里只编写了两次C ++,所以我对调试它不是很熟悉。所以比sergey更好的建议请。
谢谢
I've only programmed C++ twice in college so I'm not very familiar with debugging it. So a better advice than sergey's please.
Thank you
推荐答案
您显示的代码似乎不是您正在运行的代码。首先,你的提示
The code you are showing seems not to be what you are running. First of, your prompt
cout << "Last, First, MI : ";
按照与解析它们不同的顺序命名:
names things in a different order than the one you are parsing them:
parse >> first >> mi >> last;
你的琴弦首先,mi,last将永远不会出现在任何地方。您没有打印它们,也没有将它们返回给调用者。您只需返回原始输入行。所以这可能是你注意到的问题。
顺便说一句,使用调试器会让你的生活变得更加容易。为什么不听谢尔盖和其他人告诉你的事情。他们是绝对正确的。不,我不是他们的朋友; - )
And your strings first, mi, last will never appear anywhere. Your are not printing them nor are you returning them to the caller. You just return the raw input line. So that is probably the problem you are noticing.
And, by the way, the use of a debugger would make your life so much easier. Why not listen to what Sergey and the others are telling you. They are absolutely right. And no, I am not friends with them ;-)
对此的解决方案如下
The solution to this would be as follows
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void inputData(string &); //Declare Call-by-reference function
void outputData(string); //Declare Call-by-value function
int main() {
//declare variables
string myString;
inputData(myString);
outputData(myString);
}
void inputData(string &myString) {
cout << "Last, First, MI: ";
getline( cin, myString ); //accept string in requested format Last, First MI
string first, middle, last, fullname;
//instantiate the values for commas and spaces
string comma = ",";
string space = " ";
//find the location of the comma and the spaces in the string
int commaFound = myString.find(comma);
int firstSpace = myString.find_first_of(space);
int lastSpace = myString.find_last_of(space);
myString.erase (commaFound, 1); //erase 1 place from where the comma is found
last = myString.substr(0, commaFound); //isolate the last name
first = myString.substr(commaFound + 1, firstSpace - 2); //isolate the last namme
middle = myString.substr(lastSpace, -1); //isolate the middle initial
//takes the lenght of the word and inserts a space at the end of it
unsigned pos = first.length();
first.insert(pos,space);
unsigned pos2 = middle.length();
middle.insert(pos2,space);
myString = first + middle + last; //Pass-by-reference
}
void outputData(string myString) {
cout << myString<< " by value " << endl; //Pass-by-value
}
</sstream></string></iostream>
这篇关于姓氏没有回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!