Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
2.78/5 (5 votes)
See more:
I made this code that is supposed to receive a string of "Last, First MI" and it's supposed to return it in "First MI Last". It's supposed to read it using call by reference and return it on call by value.

It is also supposed to separate the values of last, first, and MI. and use find() to take out the commas and then concatenate them back up


This is what I have but I'm getting a compile error

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 : ";
    getline( cin, myString );
    istringstream parse( myString );
    string first, mi, last, fullname;
    int index;
    index = myString.find_last_of(' ');
    last = myString.substring(0, index);
    mi = myString.substring(index+1, index);
    first = myString.substring(index+1);
    fullname = first + mi + last;
    cout << fullname << " by reference " << endl; //Pass-by-reference*
}


void outputData(string myString) {
    cout << myString << " by value " << endl; //Pass-by-value
}


error is on
C++
last = myString.substring(0, index);
mi = myString.substring(index+1, index);
first = myString.substring(index+1);


error is
In function 'void inputData(std::string&)':|
|25|error: 'struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >' has no member named 'substring'|
|26|error: 'struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >' has no member named 'substring'|
|27|error: 'struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >' has no member named 'substring'|
||=== Build finished: 3 errors, 0 warnings ===|


Thanks in advance,

sorry,

Thanks
Posted
Updated 3-Jun-13 19:28pm
v4
Comments
Richard MacCutchan 4-Jun-13 3:43am    

1 solution

As the error says, std::basic_string does not have a member function called substring(), it does however have one called substr().
 
Share this answer
 
Comments
[no name] 3-Jun-13 23:38pm    
Thank you. I didn't see anything about that method in the tutorial. What exactly do I need to address in that method? Or function, I guess I should say. I keep confusing cpp with Java lol. Oh and, before I continue with this idea, will this work for what I'm trying to accomplish? After it compiles that is.
zlogdan 5-Jun-13 9:48am    
Please take a look here http://www.cplusplus.com/reference/string/. You only have to call it, it is already included in your project ( string header , namespace std )

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