This is supposed to generate a password, but It is not displaying the way it is supposed to display, can someone please look at my code and let me know why it is not printing out

Enter 5 character string: hapPy

Password is: jAaRY

Press any key to continue_

This is my code:

#include <iostream> // required to perform C++ stream I/O
#include <string> // required to access string functions

using namespace std; // for accessing C++ Standard Library members

// function main begins program execution
int main()
// define variables
string plainText; // stores user input
char password; // contain password character

// prompt user for five character string
cout << "\nEnter five-character string: ";
getline( cin, plainText );

// display error message if five characters are not entered
if ( plainText.size() < 5 )
// display error message
cout << "\nError: You must enter a name\n" << endl;
} // end if
else // otherwise, do calculations to get password
// display password
cout << "\nPassword is: " << static_cast<char>( 4 ) - 15) 
<< static_cast<char>( 3 ) - 15)
<< static_cast<char>( 2 ) - 15)
<< static_cast<char>( 1 ) - 15)
<< static_cast<char>( 0 ) - 15)
<< endl << endl;

cin >> password; // stores the character to password

} // end else

return 0; // indicate that program ended successfully

} // end function main

Disregard that it's not printing, the only thing that it is not printing is

press any key to continue_

Why do you need input to 'generate' a password?

This is what its supposed to do:

Password Generator Application) Write an application that generates a password from a five- character string that the user enters. The application should generate the password by reversing the order of the string and subtracting 15 from each characters ASCII code. If the user enters a string that does not contain exactly five characters, the application should display an error message and exit.

1. Including the <string> standard library header file. In line 5, insert a preprocessor directive to include the <string> standard library header file so that you can access the string class.

2. Defining variables to store user input. In lines 12-14, insert a full- line comment and define string variable plainText to store user input, and char variable password, to contain a password character.

3. Prompting the user for a five- character string. In lines 16-18, prompt the user for a five-character string.

4. Generating a password. In lines 21-24, include the if part of an if... else statement. The if part should determine whether the string has five characters. If not, the body of the if statement should display an error message. The else part of the if... else statement should generate a password. First, display descriptive text for the password. To generate the password, you must modify each character of user input individually. To access each character of the string plainText, you will use string function at. When you call the at function, you must specify the position of the character that you want to access. The first character in a string is located at position 0, the second at position 1, the third and position 2 and so on. For example, the expression plainText. at(1) returns the second character in the string. For each character in plainText, subtract 15 from the current character and assign the result to password. Use cout to display the value contained in password. Recall that C++ allows such manipulations of chars because they are stored as integers. Start with the last plain text character, which can be accessed by calling plainText. at(4). For each character in plainText, modify the integer value of the character, assign the result to password and display it. After displaying the last password character, insert two endl stream manipulators in cout.

So, your only problem is that its not printing: "Press any key to continue" ?

If that's so, you can simply do something like this (add this to line 34):

cout << "Press Enter/Return key to continue: ";

Well, it works only with the enter/return key and not just any key, but who cares! (actually the line you want comes with system("pause") which is not advisable. Look here)

Also, you didn't use your password variable usefully (according to the write-up in your previous post, password should accumulate the final password and then you should print the variable password, instead of printing individual characters from plainText one by one)

The thing you did in line 33 is not doing what its supposed to do (see above)
In case you were trying to stop the prompt with what you did in line 33, see my code snippet where i've used cin.get() this function does something similar. It waits till the user hits enter/return.

Hope that helps...:)


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.