Jump to content

Need help with password generator

- - - - -

  • Please log in to reply
4 replies to this topic

#1
maria536

maria536

    Newbie

  • Members
  • PipPip
  • 21 posts
This is what the assignment is:

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.

This is what my code looks like:

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

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>(plainText.at( 4 ) - 15) 
<< static_cast<char>(plainText.at( 3 ) - 15)
<< static_cast<char>(plainText.at( 2 ) - 15)
<< static_cast<char>(plainText.at( 1 ) - 15)
<< static_cast<char>(plainText.at( 0 ) - 15)
<< endl << endl;

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

} // end else

return 0; // indicate that program ended successfully

} // end function main

Edited by ZekeDragon, 22 September 2011 - 03:16 PM.
WingedPanther pasted code body twice with one having no whitespace making code look worse than it was.


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
I added code tags to your code (the # button), but I'm not sure what your question is.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
maria536

maria536

    Newbie

  • Members
  • PipPip
  • 21 posts
For some reason my code is not printing, press any key to continue_

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
How are you running your code? Does it ask for input at all?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
This is how your code works.

Attached Files

  • Attached File  psd.png   25.53K   20 downloads

I think i'm able to write a code for printing "Hello, World!". Proud of that!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users