1

I have written my code and I want to replace the "," to space and I just can't seem to solve this problem. Can someone help me?

#include <iostream>

using namespace std;

int main()
{

    std::cout << "Please type if you want pickup or delivery: ";

    string address;


    string space;
    
    
    cin >> input;

    delivery = "d";
    pickup = "pickup";
    cheese = "c";
    random = "r";
    space = " ";
    fee1 = "10";
    fee2 = "15";
    fee3 = "20";
    

    
    if (input == "pickup")
    {
        cout << "What pizza do you want(press r for a random pizza and c for cheese pizza!): ";
        cin >> input;
    }

    else if (input == "d")
    {
        cout << "Tell me your adress and i will deliver a random pizza to ur house!(when you type your address, plz type it comma space, we will help you replace it with space) ";
        cin >> address;
        cin.ignore();
    }

   

 
    if (address.find(space))
    {
        string address = replace(",",space);
    }


}
TechGeek49
  • 476
  • 1
  • 7
  • 24
  • 4
    [Why is β€œCan someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) Please take the [tour] and read [ask]. – Some programmer dude Mar 11 '21 at 06:50
  • Look at the error messages of your compiler. Variables must be declared. – Damien Mar 11 '21 at 06:54

2 Answers2

0

Please refer how to use std::string::replace function. https://www.cplusplus.com/reference/string/string/replace/

Please use the below code for replacing spaces by "," characters. (Pre conditions: 'adress' and 'space' variables should be declared)

 int pos = 0;
    int const LENGTH_OF_SPACE = 1;
    
    while((address.size() > 0) && (pos < address.size()))
    {
        pos = address.find(space, pos);
        cout << pos << endl;
       
       if (pos != string::npos)
       {
           address.replace(pos, LENGTH_OF_SPACE,  ",");
           pos++;
       }
       else
       {
           break;
       }
    }
0

you could use stand-alone replace function from algorithm header.

#include <algorithm>

.
.
.

replace(address.begin(), address.end(), ' ', ',');
kzlca
  • 391
  • 1
  • 6