Okay guys, I am having a problem with this code. I really need help with this one because I am stuck. The problem with this question is that the name, ss, id, and pass have to be ONE STRING. It's stupid because I wish I could make the strings split up but we have to create a substring. PLEASE NOTHING TOO FANCY, I'm still a beginner. This is what I have so far:

    Write a program that reads in a line consisting of a student’s name, Social
    Security number, user ID and password. The program outputs the string in which
    all the digits of the Social Security number, and all the characters in the password
    are replaced with X. (The Social Security number is of the form 000-00-0000, and the
    user ID and the password do not contain any spaces.) Your program should not use
    operator [ ] to access a string element. Use appropriate functions that described in the
    textbook.


#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void getInfo(string& info);
void splitInfo(string& a, string& b);
void unseenInfo(string& hide);

int main()
{
    string k, name;
    getInfo(k);
    splitInfo(k, name);

    system ("PAUSE");
    return 0;
}

void getInfo(string& info)
{
    cout << "Enter your Name, Social Security number, User ID, and Passord - separated by commas: " << endl;
    getline(cin, info);

    return;
}
    /*cout << "Name: ";
    for (counter = 0; counter < b.length(); counter++)
    {
        if (b.length() == ',')
            cout << " ";
*/

    //cout << "SSN: ";
    //for (counter = 0; counter < b.length(); counter++)
    //{
    //  cout << "x";
    //}
    //cout << endl;
    //cout<<"User ID: ";

    //for (counter = 0; counter < b.length(); counter++)
    //{
    //  cout << b;//if I did cout << "x"; it could replace the user ID with x's
    //}
    //cout << endl;
    //cout << "Password: ";
    //for (counter = 0; counter < b.length(); counter++)
    //{
    //  cout << "x";
    //}

    /*cin.ignore();

    return;
}*/

//void split(char a)
//{
//  
//}
void splitInfo(string& a, string& b)
{
    b = "a";

    int p = 1;

    for (int i = 0; a.at(i) != ','; i++)
        p++;
    if (a.at(0) != ' ')
        b = a.substr(0, p);//assigned substring to b
    else
        b = a.substr(1, p);//end of pulling out the substring from string

    a.erase(0, p + 1);//erase from 0 to p + 1, erase the first comma and everything before it
        cout << a << endl;
        cout << b << endl;

        cout << endl;
        cout << endl;

    for (int i = 0; a.at(i) != ','; i++)
        p++;
    if (a.at(0) != ' ')
        b = a.substr(0, p);
    else
        b = a.substr(1, p);

    a.erase(0, p + 1);
        cout << a << endl;
        cout << b << endl;

    return;
}

void unseenInfo(string& hide)
{
    char y = 'x';//to hide social security and password

    for (int i = 0; i < hide.length(); i++)
        hide[i] = y;//turns string values into x's to hide the string

    return;
}

Thank you for helping me!

2 things to note.

#include <iomanip>

You're not even using this.

getline(cin, info);

Why not cin >> info;?

Whats the problem with your code? What works and what doesn't? Be spesific about the things your having problems with.

Well if you assume that no name has a number in it then to find the social in the line you would do size_t spot = line.find_first_of("0123456789"). Once you have that you can erase the social from the string and then insert into it "XXX-XX-XXXX". After that you would have to find the password and you can get that by doing line.find_last_of(",") since the last comma in the string comes right before the password. Once you know where the password starts then you need to find out how long that is by taking the size of the string minus where the password start. Then you erase the end of the string where the password is and add to the end of the line string(passwordSize, 'X'). All total this would be like 10 to 20 lines of code.

As a side note you are using at() in your program. since [] = at() for the most part I doubt your teacher will let you use it as well.

I'm not sure how to get the name, ss, id, and pass in different cout statements and only put X's on ss and pass. This is really frustrating me and I need help.

You dont need to. You can use the functions of the string and do all of this with the string and then just output the string when completed.

How can I do this?

You can use the functions that I descrided in my previous post. Check this reference out for everything you can do with a string object.

Okay this is what I have right now, BUT...everytime I type:

R Y, 123-45-6789, ya123, abcd1234

there is an added X in the beginning of the social security and the password, they are white spaces with the X. What condition do I make to stop this??? Please show me because I am a visual learner. Thanks!

#include <iostream> //include statement(s)
#include <iomanip>
#include <string>

using namespace std; //using namespace statement(s)

void getInfo(string info); //void function header to get info

int main()
{
    string k; //variable declaration(s)
    cout << "Enter your Name, Social Security number, User ID, and Passord - separated\nby commas: " << endl;
    cout << endl;

    getline(cin, k); //reads the values the user inputs
    cout << endl;

    getInfo(k); //void function call to get info
    cout << endl;

    system ("PAUSE"); //black box appears
    return 0; //return statement(s)
}

void getInfo(string info)
{
    int pos1 = info.find(",", 0); //finds the position(s) after each comma
    int pos2 = info.find(",", pos1 + 1);
    int pos3 = info.find(",", pos2 + 1);
    int lastPos = info.length() - 1; //finds the last position

    int nameLength = pos1 - 0; //finds the length for each position(s)
    int ssnLength = pos2 - (pos1 + 1);
    int idLength = pos3 - (pos2 + 1);
    int passLength = lastPos - (pos3 + 1) + 1;

    string passwordMask = info.substr(pos3 + 1, passLength); //passwordMask is a substring to convert the
    for (int i = 0; i < passLength; i++)                  //password characters into X's
    {
        passwordMask.erase(i, 1);
        passwordMask.insert(i, "X");

    }

    string ssnMask = info.substr(pos1 + 1, ssnLength); //ssnMask is a substring to convert the social security
    for (int i = 0; i < ssnLength; i++)                 //numbers into X's, added condition if there are '-'
    {                                                  //to NOT make them X's

        if (ssnMask.at(i) != '-')
        {
            ssnMask.erase(i, 1);
            ssnMask.insert(i, "X");
        }
    }

    cout << "Your name is: " << info.substr(0, nameLength) << endl;
    cout << "Social Security Number is: " << ssnMask << endl;
    cout << "Your ID is: " << info.substr(pos2 + 1, idLength) << endl;
    cout << "Your password is: " << passwordMask << endl;

    return;
}

This would be the simplest way I can think of to do it.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string info = "R Y, 123-45-6789, ya123, abcd1234";
    // social starts 2 places after the first coma
    size_t ssStart = info.find_first_of(",", 0) + 2;
    info.erase(ssStart, 11);
    info.insert(ssStart, "XXX-XX-XXX");

    // password starts 1 place after the last space
    size_t passStart = info.find_last_of(" ") + 1;
    size_t passSize = info.size() - passStart;
    // get rid of the password
    info.erase(passStart);
    // make a string with all X's the size of the password and ad it to the end
    info += string(passSize, 'X');
    cout << info;
    cin.get();
    return 0;
}

All done in 24 lines with comments and blank lines(fist pump).

I have to let the user type in their string though, what I said was an example of what the program is doing.

Well thats easy enough to change the code to do that. I am not writing your program for you. I am showing you how it could be done. You should be able to handle getting user input and putting it into a string.

I use: getline.(cin, info); to replace it, I see...BLAH lol, I wish we could do our own thing for these assignments >_<

What is the code you are using now? Is it not working?

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.