Password program, need help

Need to create a program that allows the user to enter a password, and prints out the password with all vowels changed to x's, numbers changed to z's and characters reversed, but I can't find anything on how to do it in the book or online.. Could someone please help? I'm also using strings for the password input and output

Seems simple enough, but what do you mean by the "characters reversed"?
One way I would do this is (after inputting the user's password) by looping through each letter of the string and using the <cctype> header file to check if they are vowels, numbers, etc (there's an article on isalpha here: http://www.cplusplus.com/reference/cctype/isalpha/ and one on isdigit here: http://www.cplusplus.com/reference/cctype/isdigit/.). Then, if it is, replace it. Here's something to get you started:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int i = 0; i < password.size(); i++)
{
     // Check if it is vowel
     if (isalpha(password[i] && checkVowel(password[i])
          password[i] = 'x';

     // Check if it is a consonant
     else if (isalpha(password[i] && checkVowel(password[i])
          // Code to "reverse" your characters

     // Check if it is a number
     else if (isdigit(password[i])
          // Code to change the password[i] to 'z'
}


The function to check if the letter is a vowel, void bool checkVowel (char x), is left to you to create.

Edit: I initially mistakenly wrote the vowel checker as a void function, it's supposed to be a bool function.
Last edited on
All the characters (numbers and letters) would be put in reverse, example: aaa23h would be z32xxx
I got reversing it and checking if it's a number, but I can't figure out the void function, we have barely covered that so far, and I'm having trouble with yee ol' google on finding somthing, and the consonant thing too
Last edited on
Ok. I made a mistake in the void function, it was actually supposed to be a bool function. My bad. But, basically, what the function should do is just run a long if statement to see if the letter is any consonant, 'a', 'e', 'i', 'o', and 'u'. Then, if it is, return true. If not, return false.

It'll look something like this:

1
2
3
4
5
6
7
8
bool checkVowel (char x)
{
     if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u')
          return true;

     else
          return false;
}


So, as you can see, it just checks if it's either an 'a', 'e', 'i', 'o', or 'u'.

Remember to declare it before your int main() and then defining it afterwards. Like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cctype>
// Whatever else you need to include

bool checkVowel (char x);

int main ()
{
     // Code
}

bool checkVowel (char x)
{
     if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u')
          return true;

     else
          return false;
}


And it's nice to see that you have the reversing function done! Well done.

-VX
Last edited on
Yeah, ol' Google can be an arse sometimes.
This is what I have so far, I feel like i'm on the right track here but i've got two errors

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
48
49

// Chapter 13 ex 34.cpp : Defines the entry point for the console application.
//

#include "string"
#include <iostream>
using namespace std;
bool checkVowel(char x);

int main()
{
	string password;
	string newPassword;
	string checkVowel;

	cout << "Enter a password that contains numbers and capital letters";
		cin >> password;

		for (int i = 0; i < password.size(); i++)
	{
		// Check if it is vowel
			bool checkVowel(char x);
		{
			if (has an error-->x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u')
				return true;

			else
				return false;
		}

		// Check if it is a consonant
		if (has an error-->password != "A" || "E" || "I" || "O" || "U" && isdigit = false)
			password[i] = 'x';

			// Check if it is a number
		if (isdigit(password[i]))
			password[i] = 'z';
				// Code to change the password[i] to 'z'

				for (int i = password.size() - 1; i >= 0; --i)
				{
					newPassword += password[i];
					cout << newPassword;
				}
	}

	return 0;
}
When you use multiple conditions that are OR'd or AND'd together, each condition must be an "independent statement".

if (password == "A" || "B" || "C") { }
is not correct.

You must do:
if (password == "A" || password == "B" || password == "C") { }

____________________________

1
2
3
4
			bool checkVowel(char x);
		{
			if (has an error-->x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u')
				return true

In C++, you can't define a function within a function.
Move your definition of bool checkVowel(char) outside of main.

Also, your indentation could use some work.

This
1
2
3
4
5
6
7
8
9
		if (isdigit(password[i]))
			password[i] = 'z';
				// Code to change the password[i] to 'z'

				for (int i = password.size() - 1; i >= 0; --i)
				{
					newPassword += password[i];
					cout << newPassword;
				}


should be

1
2
3
4
5
6
7
8
		if (isdigit(password[i]))
			password[i] = 'z'; // Code to change the password[i] to 'z'

		for (int i = password.size() - 1; i >= 0; --i)
		{
			newPassword += password[i];
			cout << newPassword;
		}

[/code]
Last edited on
I'll right i'll try it out thanks
Topic archived. No new replies allowed.