Cant get program to work.

Instructions: 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.

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
45
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int counter;
	string name, ssn, user, pass;

	cout << "Enter Name: ";

	cin >> name;

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

	cout << "Enter User ID: ";
	cin >> user;

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

	cout << endl;

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

	cout << endl;

	return(0);
}

Note: I am very new to C++
Last edited on
Your Program works but it automatically closes as soon as it is finished add system("pause"); before return 0. Here is your changed code
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
45
46
47
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int counter;
	string name, ssn, user, pass;

	cout << "Enter Name: ";

	cin >> name;

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

	cout << "Enter User ID: ";
	cin >> user;

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

	cout << endl;

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

	cout << endl;
	system("pause");

	return(0);
}
I knew it was something small. I could not figure it out. THANKS!
Topic archived. No new replies allowed.