How to ignore certain characters from input?

My current assignment tasks me with creating a program to convert a string into a phone number. My problem arises when I try to remove certain characters from the input. I tried to use cin.ignore() but I don't know if I'm doing it right. I need to remove spaces from the input, then convert the input to lowercase letters, and based off which letters they are, convert them to numbers.

How do I remove certain characters from an input?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>

using namespace std;
 
int main()
{
    //User asked if they want to convert word.  y or Y, anything else
    //to stop.  While convertingWords is y or Y, execute the program. 
    string convertingWords;
    cout << "If you want to convert a word, type y or Y.  If not, type any other key." << endl;
    cin >> convertingWords;
    string sloganWord;
    while(convertingWords == "y" or convertingWords == "Y")
    {
        cout << "Type your word/slogan to have it converted." << endl;
        cin.ignore(' ','\n');
        getline(cin,sloganWord);
        
        
        string newSloganWord;
        //Use for loop, loop length of sloganWord's string size, evaluate 53 possible characters, if letters uppercase or lowercase, convert respectively, spaces skipped
        //Increase string length every time a letter is found, only record 7 string characters.  
        cout << newSloganWord << endl;
        //Convert sloganWord to a phone number, which only has seven total numbers, and has a hyphen after the third number.  
        //Ignore spaces, make upper and lowercase numbers synonymous, and ignore everything after the seventh LETTER.  
        cout << "Do you want to convert another word?  Type y or Y to contiue, or any other key to stop.  " << endl;
        cin >> convertingWords;
    }
}
Let the user type in anything they want. In post processing is where you must form the new string.

This isn't exactly the answer to your prompt, but it's an example of forming an integer phone number from a string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Example program
#include <iostream>
#include <string>
#include <cctype> // isdigit

int parse(const std::string& text)
{
    int number = 0;
    for (size_t i = 0; i < text.size(); i++)
    {
        if (isdigit(text[i]))
        {
            number = number * 10 + static_cast<int>(text[i] - '0');
        }
    }
    
    return number;
}

int main()
{
    std::cout << "Enter phone number: ";
    std::string text;
    std::cin >> text;
    
    int number = parse(text);
    std::cout << "You entered: " << number << std::endl;
}



Enter phone number: (123)-456-7890
You entered: 1234567890
This class hasn't taught functions outside of main yet.
Then copy the body of the function into main and remove the return statement.
Topic archived. No new replies allowed.