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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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;
}

>_ Terminal
+
John Doe, 333224444, DoeJ,123Passwor
d.
John Doe XXXXXXXXX DoeJ XXXXXXXXX
XX
Transcribed Image Text:>_ Terminal + John Doe, 333224444, DoeJ,123Passwor d. John Doe XXXXXXXXX DoeJ XXXXXXXXX XX
Input
40 passwordMask.erase(i, 1);
41 passwordMask.insert(i, "X");
John
42
John Doe 333224444 DoeJ 123Password
43 }
Joh
44
XX
Output
45 string ssnMask = info.substr(pos1 +
1, ssnLength); //ssnMask is a
substring to convert the social
security
46 for (int i = 0; i < ssnLength; i++) /
John Doe 333224444 DoeJ 123Password
/numbers into X's, added condition
if there are '-'
John Doe 333224444 DoeJ 123Password John Doe 333224444 DoeJ 123Password John Doe 33322444
47 { //to NOT make them x's
48
49 if (ssnMask.at(i) != '-')
Results e
50 {
51 ssnMask.erase(i, 1);
John Doe XXXXXXXXX DoeJ XXXXXXXXXXX
52 ssnMask.insert(i, "X");
Expected Output O
53 }
54 }
John Doe хххXхххXх DoeJ Xxххххххххх
55
Transcribed Image Text:Input 40 passwordMask.erase(i, 1); 41 passwordMask.insert(i, "X"); John 42 John Doe 333224444 DoeJ 123Password 43 } Joh 44 XX Output 45 string ssnMask = info.substr(pos1 + 1, ssnLength); //ssnMask is a substring to convert the social security 46 for (int i = 0; i < ssnLength; i++) / John Doe 333224444 DoeJ 123Password /numbers into X's, added condition if there are '-' John Doe 333224444 DoeJ 123Password John Doe 333224444 DoeJ 123Password John Doe 33322444 47 { //to NOT make them x's 48 49 if (ssnMask.at(i) != '-') Results e 50 { 51 ssnMask.erase(i, 1); John Doe XXXXXXXXX DoeJ XXXXXXXXXXX 52 ssnMask.insert(i, "X"); Expected Output O 53 } 54 } John Doe хххXхххXх DoeJ Xxххххххххх 55
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education