So for my first assignment of the semester I need to write a program that:

1.) Asks the user to input a string
2.) Asks them to replace part of the string
3.) Have the program prompt "Yes" or "No" on whether the user wants the program to restart, if yes then it does and if no then it ends.(I haven't written this part of the code yet because it seems like the easiest and I can do at the end. It's a simple loop with a boolean value right?)

Okay so obviously I don't want you guys to just give me the answers because I still want to learn, but if you could help me onto the right track that would be great!

Thanks a lot!

This is what I have so far minus the restart function and loops:

I tried to test this and received errors. I am unsure of how to code this search and replace function.

#include <iostream>
#include <cstring>

using namespace std;

void replaceString (string, string);

int main()
{
    // Ask user to input a string
    // Create a function that reads a string, locates a portion, and replaces it
    // with a new string
   
    string s1, s2, s3;

    cout << "Please input a string: " << endl;
    getline(cin,s1);
    
    cout << "What would you like to replace? " << endl;
    getline(cin,s2);
    
    cout << "What would you like to replace '" << s2 << "' with? " << endl;
    getline(cin,s3);

    replaceString (string& s1, string& s2)
    
    system("pause");
    return 0;
}
    
    
void replaceString (string& s1, string& s2)
{
     int loc;
     
     loc = s1.find(s2);
     
     s1.replace(loc,s3);
     
     cout << s1 << endl;
     
     }

Can you post errors here?

C:\Dev-Cpp\Projects\DPR 226\assign1.cpp In function `int main()':
25 C:\Dev-Cpp\Projects\DPR 226\assign1.cpp expected primary-expression before '&' token
25 C:\Dev-Cpp\Projects\DPR 226\assign1.cpp expected primary-expression before '&' token
C:\Dev-Cpp\Projects\DPR 226\assign1.cpp In function `void replaceString(std::string&, std::string&)':
38 C:\Dev-Cpp\Projects\DPR 226\assign1.cpp `s3' undeclared (first use this function)

Hmm, I'm using the '&' operator completely wrong. I know it means to pass by reference instead of value, but I have yet to grasp exactly how to use it. And it shows s3 as undeclared, but I called the function AFTER I had the user input the strings...?

>>replaceString (string& s1, string& s2);

Wrong way of calling a function.

>>void replaceString (string, string);

has to be void replaceString (string&, string&);

>>replaceString (string& s1, string& s2);

That was line no. 25 and the other one was the prototype

Okay, I corrected the function, but now I am receiving errors on the " s1.replace(loc,s3);" line. Did I not use the replace function correctly?

#include <iostream>
#include <cstring>

using namespace std;

void replaceString (string&, string&, string&);

int main()
{
    // Ask user to input a string
    // Create a function that reads a string, locates a portion, and replaces it
    // with a new string
   
    string s1, s2, s3;

    cout << "Please input a string: " << endl;
    getline(cin,s1);
    
    cout << "What would you like to replace? " << endl;
    getline(cin,s2);
    
    cout << "What would you like to replace '" << s2 << "' with? " << endl;
    getline(cin,s3);

    replaceString (s1, s2, s3);
    
    system("pause");
    return 0;
}
    
    
void replaceString (string& s1, string& s2, string& s3)
{
     int loc;
     
     loc = s1.find(s2);
     
     s1.replace(loc,s3); //Error here
     
     cout << s1 << endl;
     
     }

Not sure if we're allowed to bump, but...

bump!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.