Program Help. AGAIN:)

Alright guys. Im back with another assignment that I need help on. Ive been able to get a majority of my previous assignments, however I really need help on this one. Here it is...

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 by x. (The Social Security number is in the form 000-00-0000, and the user ID and the password do not contain any spaces.) Your program should not use the operator [ ] to access a string element.

If you guys can help me with this that would be great. The instructor wants us to use string functions as much as possible. Im still very very new to programming, so any help would be great. Thanks guys:)
If you're allowed to use string functions then you're in luck. They'll make life much easier here.

Can you post in particular what you're stuck with? You're more likely to get help if you're specific. People around are reluctant to post full solutions for fear of doing your homework for you.
Okay well here is what I have so far. Sorry about the late response... But what I have runs as its supposed to, however my professor wants the input to be all at one time. As in he wants it to read just a single string for the input. I have it as single inputs... I need someone to point me in the right direction. Maybe some help on where I could include more strings. Thanks :)

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
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int counter;
	string name,ssn,userid,password;
	
	cout <<"Enter Name: ";
	getline (cin, name);

	cout << "Please Enter SSN (no spaces required): ";
	cin >> ssn;

	cout << "Please Enter User ID: ";
	cin >> userid;

	cout << "Please Enter password: ";
	cin >> password;

	cout << endl;

	cout << "Name: " << name <<endl;
	cout << "SSN:  xxx-xx-xxxx"<<endl;
	cout << "User ID: ";
	for(counter=0; counter<userid.length(); counter++)
	{
		cout << "x";
	}
	cout << endl;
	cout << "Password: ";
	for(counter=0; counter<password.length(); counter++)
	{
		cout << "x";
	}

	cout << endl;

system("Pause");
return(0);
}
If you're okay with restricting the name to only one word, then replace getline(cin, name); with cin >> name; and your program already works as you want it to:
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int counter;
	string name,ssn,userid,password;
	
	cout <<"Enter Name: ";
	//getline (cin, name);
	cin >> name;

	cout << "Please Enter SSN (no spaces required): ";
	cin >> ssn;

	cout << "Please Enter User ID: ";
	cin >> userid;

	cout << "Please Enter password: ";
	cin >> password;

	cout << endl;

	cout << "Name: " << name <<endl;
	cout << "SSN:  xxx-xx-xxxx"<<endl;
	cout << "User ID: ";
	for(counter=0; counter<userid.length(); counter++)
	{
		cout << "x";
	}
	cout << endl;
	cout << "Password: ";
	for(counter=0; counter<password.length(); counter++)
	{
		cout << "x";
	}

	cout << endl;

//system("Pause");
return(0);
}
Enter Name: Bob 123-45-6789 admin password
Please Enter SSN (no spaces required): Please Enter User ID: Please Enter password:
Name: Bob
SSN:  xxx-xx-xxxx
User ID: xxxxx
Password: xxxxxxxx

All you need to do is change your first cout statement to ask for the whole input and then get rid of the other cout statements asking for the other pieces.
See, when you input something, any extra input you put after what the program reads in doesn't magically "disappear" -- it gets read in the next time you input something else.

Now, if you insist on full names (with the space(s)), then it becomes a little harder to do (but still not too difficult)....
That does help, and is a little closer to what I'm looking to accomplish with the program. However, I'd still like the string to read spaces as well. And if possible, instead of just using cout << "xxx-xx-xxxx" to replace the SSN, I'd like the program to actually replace the characters. I guess I need some help using some more strings. Thanks in advance. :)
someone please help me. T_T
okay, can someone just help me with inputting multiple string values into multiple variables. If that makes sense. So i want multiple variables to be assigned values from one string of data. Id also like to include spaces. I have done my research with google and my textbook. I cat seem to find anything of use. Thanks guys.
There are a couple of ways to do this. One of them involves reading the input right off of the input stream, and another involves dumping the whole input into a string and picking out the parts that you want.

Since the name is followed immediately by the SSN, one thing you could do is grab input and stick it onto the name until you run into a digit.
Then do what I mentioned above.

For instance, something like this:
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
#include <iostream>
#include <string>
#include <cctype>
using std::cout;
using std::cin;
using std::string;
using std::isdigit;

int main()
{
    cout << "Enter your name followed by a something starting with a number:";
    cout << "\n>> ";
    string name;
    while (!isdigit(cin.peek())) // Peek at the next character in the input
    {
        string tmp;
        cin >> tmp;
        cin.ignore(); // Get rid of the whitespace
        name += (tmp + ' ');
    }
    name.pop_back(); // Get rid of the trailing space, not like it matters
    // If you have an older compiler, you might need to replace that line with
    // name.erase(name.end()-1);
    string other;
    cin >> other;
    
    cout << "Your name is " << name;
    cout << "\nand the other thing you entered was " << other;
}

This produces the output:
Enter your name followed by a something starting with a number:
>> Jay Jay the Jet Plane 123that's-me!
Your name is Jay Jay the Jet Plane
and the other thing you entered was 123that's-me!


If you decide to use getline to grab the whole input all at once, you can use std::find_if (http://www.cplusplus.com/reference/algorithm/find_if/ ) to find where the first digit (presumably the start of the SSN) is, and then use the substr member function of std::string to grab everything up to that point (which will presumably be the full name).

As for "actually" replacing the digits of the SSN, you can use std::replace_if (http://www.cplusplus.com/reference/algorithm/replace_if/ ) to replace all of the digits with 'x's.
(Or you can just do what you've already been doing, since you know that the SSN has to be in that format)

(For those of you who can tell, I'm just trying to dodge the use of operator[] here.)


Or...since you can't use operator[] to access string elements, you can just use iterators for everything instead:
1
2
3
4
5
for (std::string::iterator it = ssn.begin(); it != ssn.end(); ++it)
{
    if (std::isdigit(*it)) // or if (*it != '-')
        *it = 'x'; // Replace digits with 'x'
}

(Geez, why didn't I think of that before?)
That helps a little too. This is for an online class, and all ive been able to get out of my instructor is that using cin.get(input) would be the best way to go about completing the assignment. I just am having trouble using it... any input? And thank you long double main. I know your posts take some time to make, its just I'm so new to this that a lot of what you say doesn't make a lot of sense to me:/
I'm not sure how cin.get will help here, since cin.get is for either single characters (which doesn't seem to be the case for cin.get(input)) or for "C strings" (which are basically just char arrays, which can be used kind of like strings), which you aren't using and probably don't need to use.
Its funny you mention char arrays. We are currently in chapter 7 of our book (which is of no use to me), and char arrays are mentioned in chapter 8. But on top of that, one student posted that he believes that this programs requires some info from chapter 8. Maybe char arrays are the answer. I just dont know. I'm stuck. This program is a little too complicated for me.
Nah, if you're going to use char arrays, you'd be much better off just using std::string.

One thing you could try is repeatedly doing cin >> someTmpString; followed by name += someTmpString; to add that to the name, until you "accidentally" read in the SSN. When that happens, then you can basically just do what I did in my first post above to grab the rest of the variables.

As for figuring out when you've actually read the SSN, you could technically use *(myString.begin() + n) as a substitute for myString[n], but I don't know if that's considered "cheating" or not.
Maybe I'm not making a lot of sense... here's exactly what he said.

You need to take in all the data from a single line. string input; cin.get(input); Then you need to use the string functions to find, separate, and replace all the data.

So what he said sounds like it might relate to what you said.
Umm...cin.get(input); doesn't work for string input;. He probably meant to say use getline(cin, input); instead.

Then, what you could probably do is use the find member function (http://www.cplusplus.com/reference/string/string/find/ ) to look for a '-' character (which is the first dash of the SSN, unless your name has a hyphen in it, in which case...well, hopefully it doesn't).
Then, since you know there are exactly 4 digits before that first dash, you can back up the search result 4 places (string::find returns a number indicating the position of the first occurrence found, so you can just subtract 4 from that), and use the substr member function (http://www.cplusplus.com/reference/string/string/substr/ ) to grab the name.

Then, since everything else is separated by spaces, you can just use string::find (looking for a space character) and string::substr a few more times to sift out the other pieces.
Topic archived. No new replies allowed.