/* Will find and replace the given sub-string within
* the user provided string with the new sub-string.
*
* Require: user_string contains sub_string
* Ensure: user_string !contains sub_string &&
* user_string contains new_sub_string
*/
void replace(string& user_string,
const string& sub_string,
const string& new_sub_string);
int main() {
// Default Employee name on tag
const string DEFAULT_NAME_LAST("{DEFAULT_NAME_LAST}");
const string DEFAULT_NAME_FIRST("{DEFAULT_NAME_FIRST}");
// to hold string input from employee
string buffer("");
// Employee's ID Tag
string employee_tag("* EMPLOYEE: \n");
// add employee's last name first
employee_tag.append("* " + DEFAULT_NAME_LAST + ",\n");
employee_tag.append("* " + DEFAULT_NAME_FIRST + "\n");
// ask the employee for their first name
cout << "Hello new employee!" << endl;
cout << "What is your first name? ";
cin >> buffer;
// replace their first name with string in buffer
replace(employee_tag, DEFAULT_NAME_FIRST, buffer);
// then clear the buffer
buffer.clear();
// adk the employee for their last name
cout << "What is your last name? ";
cin >> buffer;
cout << endl;
// replace their last name with string in buffer
replace(employee_tag, DEFAULT_NAME_LAST, buffer);
// then clear the buffer
buffer.clear();
// thank them and display their name tag:
cout << "Thank You!" << endl;
cout << "Here is your name tag: " << endl;
cout << employee_tag;
exit(EXIT_SUCCESS);
}
/* Will find and replace the given sub-string within
* the user provided string with the new sub-string.
*
* Require: user_string contains sub_string
* Ensure: user_string !contains sub_string &&
* user_string contains new_sub_string
*/
void replace(string& user_string,
const string& sub_string,
const string& new_sub_string) {
// find position of sub_string in user_string
// replace sub_string in user_string with new_sub_string
}the code you need help with here.
[/code]
When the program is running. The it will ask the user for the "first name". After user enters first name...it will ask for last name. After that will say welcome employee "DEFAULT FIRST NAME" and then "DEFAULT LAST NAME" the object of the assignment is to use replace.string to replace the deafults with the actual user's input.
That code is supposed to be done without changing anything in the main. I know that the buffer(the user input) is passed to the new_sub_string. I know you're supposed to find the sub_string in the user_string, the sub_string is the default_name_first/last, and the user_string is the employee tag. Then put the buffer given string in the employee tag...I know I need to find where default name is in the employee tag, but I don't understand how to replace it with the buffer.
It looks like you are trying to modify the wrong string, and in the wrong part of the code.
From a purely technical point of view, the replace will fail because the string which you attempt to modify is declared as const which means you are not permitted to change them, by design.
However, you are supposed to be adding your code inside this function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Will find and replace the given sub-string within
* the user provided string with the new sub-string.
*
* Require: user_string contains sub_string
* Ensure: user_string !contains sub_string &&
* user_string contains new_sub_string
*/
void replace(string& user_string,
const string& sub_string,
const string& new_sub_string) {
// find position of sub_string in user_string
// replace sub_string in user_string with new_sub_string
}
Notice there are three parameters, user_string, sub_string and new_sub_string
Two of those are declared as const, so obviously you are not supposed to change them.
You need to write code which findssub_string inside user_string.
I think I get what you are saying......but i have tried everything finding which what to use.....I had demo wednesday and his demo got screwed up so badly. I am badly confused.
I don't know which one to use :/. I am extermely fustrated and confused....
is there a example you can show me or tell me in english what needs to be done. I appriciete all the help you guys are giving me.
void replace(string& user_string,
const string& sub_string,
const string& new_sub_string) {
int pos;
int size;
string str;
// find position of sub_string in user_string
pos = user_string.find(sub_string);
size = sub_string.size();
str = user_string;
// replace sub_string in user_string with new_sub_string
str.replace(pos, size , new_sub_string);
}
It compiles, but it wont change anything in the employee tag. I gave the replace function the starting position, the size of the text I want to change, and the text. I have no clue what I'm doing wrong...
Can you explain why exactly I wouldn't need string str?
You don't need it because it is a local variable which exists only for the duration of the function. When the function ends, the variable is destroyed and any changes you made to it are lost.
How would I apply the replace() to the user_string parameter?