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;
     
     }

Dani AI

Generated

Short checklist and quick fixes (what prevented compilation and what to change)

  • As pointed out, call a function with variable names, not type syntax. The prototype and definition must match exactly (use string& in both if you want references).
  • Use <string> (for std::string) and use string::size_type or size_t for positions — find returns npos if not found, so check that before calling replace.
  • string::replace does not take a replacement string as the single second argument; use the overload that accepts a start position and a length then the replacement string.

A minimal correct single-replace function (illustrates proper signatures and checks):

#include <string>
#include <iostream>

void replaceOnce(std::string &text, const std::string &target, const std::string &repl) {
    std::string::size_type pos = text.find(target);
    if (pos == std::string::npos) return;    // not found
    text.replace(pos, target.length(), repl);
}

Replace-all loop and important cautions

  • To replace every occurrence, loop with find(pos) and advance pos by repl.length() after each replacement. Guard against target.empty() (otherwise infinite loop). If repl contains target you may still get unexpected behavior, so design accordingly.
void replaceAll(std::string &text, const std::string &target, const std::string &repl) {
    if (target.empty()) return;
    std::string::size_type pos = 0;
    while ((pos = text.find(target, pos)) != std::string::npos) {
        text.replace(pos, target.length(), repl);
        pos += repl.length();
    }
}

References and tie-back

  • : fixing the prototype/call and using the overload above will resolve the compile errors you saw; — when posting compiler feedback, include the exact error lines to speed diagnosis. See find and replace docs for details: std::basic_string::find and std::basic_string::replace.

Recommended Answers

All 6 Replies

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.