Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.71/5 (6 votes)
See more:
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

C++
#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
}


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
Posted
Updated 3-Jun-13 14:07pm
v2
Comments
Sergey Alexandrovich Kryukov 3-Jun-13 19:56pm    
Ever used the debugger? It's just about time. You hardly need anything else.
—SA
[no name] 3-Jun-13 20:05pm    
Sergey, has anyone ever told you, your helping and tutoring style is very unorthodox? Thank you for your help.
Sergey Alexandrovich Kryukov 3-Jun-13 20:24pm    
Thank you, Rei. And you are very welcome. I'm really pretty much sure that what I say is the best you should do. By the way, you did not answer my question, so I'm not sure if you are doing right thing.
You see, this is not something that really need expert advice. It's not that you are doing something fundamentally wrong, or there is some hidden API to use, or any "professional secrets". It's just some attention and work. I hope you understand.
—SA
[no name] 3-Jun-13 20:43pm    
Actually, I still can't find the problem. I eliminated the cin line because I am using getline but the input is not reversing its order. And I don't know how to debug in Code:Blocks. But is ok, I'll figure it out somewhere else
[no name] 3-Jun-13 20:41pm    
Sergey is correct. Learn to use the debugger it is your friend. You also really need to make it more clear what the problem is. For example "the return will show" means what? There is no "return" anywhere.
Now. The reason that you the last is missing.... is that is not. It is being thrown away. You read it out of the stream with cin >> myString; and don't do anything with it and then go and read the rest of the stream with getline( cin, myString );. If you get rid of the cin >> myString; your missing last name will reappear and everything will be right with the universe.

The code you are showing seems not to be what you are running. First of, your prompt
C++
cout << "Last, First, MI : ";

names things in a different order than the one you are parsing them:
C++
parse >> first >> 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 ;-)
 
Share this answer
 
Comments
[no name] 4-Jun-13 9:13am    
Thank you, That was a much clearer explanation. I know debugging is going to make it easier. But as I mentioned above, I am not familiar with the IDE that I am using or c++ in general. I tried debugging and I was shooting in the dark just guessing stuff. I am a Linux front end developer (PHP, SQL, Jquery, JS, etc) I'm trying to relearn the C++.

The problem with Sergey comes from previous questions where he responded to my question in a manner that wasn't appreciated. Think about it. If a person who has absolutely no knowledge on a subject asks a question, it will most likely be a stupid question, but there is no need to make him feel stupid for asking it. This could have all been avoided if he just quit commenting on my questions.

And the Phantom....he's upset because I told him he didn't help me. But he didn't! Look at his answer "Well yes I can see that. You are parsing myString into first mi and last then swapping mi and last but printing the original myString to the console." He just repeated my code and my results. Where is the answer in that?

See, the problem here is that some of the people here are so knowledgeable in their material that they forget that what is easy for them, is not for the noobie.

Sorry,I just wanted to clear that out. But honestly I wish I could just delete this entire thread. I really don't like this bickering. I want to be able to ask a question, get an answer, learn and carry on. ;)

Thanks again for your response. I marked it as an answer but I ended up changing the code and going a different route anyway.
nv3 4-Jun-13 10:18am    
Thanks, Rei, for your honest and open reply. There is some truth in what you say. We long-year software developers, who know our platforms in and out, are sometimes to impatient with beginners. So it might have happened in your case. But consider that someone answering questions here over a longer period and seeing the same mistakes being made over and over again is too easily tempted towards that attitude. We are all simply human. And seeing how so many beginners are struggling with simple problems and not even considering using a debugger makes one sad sometimes.
[no name] 4-Jun-13 10:30am    
agreed. It takes a lot of patience to be an educator. I am a professional MMA fighter and trainer. People come to me from all walks of life with the desire to learn boxing or Muay Thai, or Jiu Jitsu. Some of them can't throw the most simple of all punches (The jab). But I have to understand they came to me for that very same reason.

by the way, I am remodeling the code...I don't know if I'm doing it right or wrong, but I found that checking consistently with a "cout" helps me see what is being stored in that variable before I move on to the next line.

The tutorial I am following wants me to use lenght(), substr(), insert(), erase(), and find(). I have a separate question that I reposted that is more updated

Thank you for your attention on this
The solution to this would be as follows

C++
#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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900