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.
Code is in C++
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.
Input is as follows highlighted in bold
John Doe 333224444 DoeJ 123Password
My problem is with my output, i am close with the code, but i have attached what happens on my output and i cannot figure out why? You can see how it prints out multiple times but i am lost? The terminal image is also attached. Thank you!
Here is the code:
#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 << " " << 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 << " " << info.substr(0, nameLength);
cout << " " << ssnMask;
cout << " " << info.substr(pos2 + 1, idLength);
cout << " " << passwordMask;
return;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images