I'm working on an assignment for class and keep getting an "expected unqualified-id" for the "{" after defining my bool types in the function. I can't figure out why I'm getting this error and it is making it hard to get my assignment done without being able to run my program. Can anyone tell me why I'm getting this error? Here is my code
//Page 825 Problem 12
#include <iostream>
#include <string>
using namespace std;
//Function Prototype
bool testPassword(char []);
const int passLength = 21;
char password[passLength];
int main()
{
//Ask user to enter password matching the following criteria
cout << "Please enter a password at six characters long. \n"
<< "Password must also contain at least one uppercase and one lowercase letter. \n"
<< "Password must also contain at least one digit. \n"
<< "Please enter your password now \n";
cin.getline (password, passLength);
if (testPassword(password))
cout << "Password entered is of the correct format and has been accepted.";
else
cout << "Password does not meet criteria \n";
return 0;
}
//*******************************
//**Function to test password ***
//**to determine if it meets ***
//**criteria listed ***
//*******************************
//Test password to determine if it is at least six characters long
bool testPassword (char password[]);
bool lower;
bool upper;
bool digit;
bool length;
{
if (strlen(password) < 6)
length = true;
else
length = false;
cout << "Password must be at least 6 characters long.\n";
for (int k = 0; k < passLength; k++)
{
if (islower(password[k])
lower = true;
else
lower = false;
cout << "Password must contain a lowercase letter.\n";
if (isupper(password[k])
upper = true;
else
upper = false;
cout << "Password must contain an uppercase letter.\n";
if (isdigit(password[k])
digit = true;
else
digit = false;
cout << "Password must contain a digit.\n";
}
if (lower && upper && digit && length == true)
return true;
else
return false;
}
testPassword: has a ";" and the end of line that it shouldn't have.
The bool variables need to be inside the first "{", not before it.
It sounds like you actually want this:
bool testPassword (char password[])
{
bool lower;
bool upper;
bool digit;
bool length;
if (strlen(password) < 6) {
length = true;
}
else {
length = false;
cout << "Password must be at least 6 characters long.\n";
}
...
NOTES:
"testPassword()" with a ";" is a FUNCTION PROTOTYPE (not an actual function definition)
Unlike Python, just indenting doesn't make a conditional block. If you want more than one line in the block, you need curly braces.
This part is placed within global scope:
bool testPassword (char password[]); // <-- declaration of function
bool lower; // <-- global variables
bool upper;
bool digit;
bool length;
{ // <-- start of the scope? what does it belong to?
...
and it is invalid, you can not place program's logic in global scope... functions can not be just called "out of nowhere"... if it was supposed to be the body of testPassword function already, it should be:
bool testPassword (char password[])
{
bool lower;
bool upper;
bool digit;
bool length;
...
}
Related
I'm trying to create a password login program in C++. So there is some mistake in looping.
If the entered password satisfied all the conditions means it had to come out of loop but it doesn't end. So if anyone know means explain me properly. I just have started to learn programming.
Look at the output first. It is showing it must include uppercase and digits and I enter some correct password and when I enter the wrong password knowingly, it is showing that it's good password, which is unexpected for me.
Here's what I've attempted to do:
#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;
int main()
{
int alp = 0, i, num = 0, j;
char a[10];
do
{
cout << "\nEnter a password:";
cin >> a;
if (strlen(a) > 8)
{
for(i = 0; i <= sizeof(a); i++)
{
if(isupper(a[i]))
alp++;
else if(isdigit(a[i]))
num++;
}
if (alp > 0 && num > 0)
cout << "\nGood password\n";
else
cout << "Your password must include atleast one digit and one uppercase\n";
}
else
cout << "\nYour password must have atleast 8 characters";
} while(true);
return 0;
}
Here's the output:
Enter a password:harry
Your password must have atleast 8 characters
Enter a password:harrypot
Your password must have atleast 8 characters
Enter a password:harrypott
Your password must include atleast one digit and one uppercase
Enter a password:Harry1817t
Good password
Enter a password:harrypott
Good password
Enter a password: // forever
Any help on this is appreciated.
There are several defects in your code:
The while(true) is a non-terminating loops, but no break is added.
The alp and num weren't reset at the end of the loop iteration.
Since the alp and num weren't reset, they're incremented in each iteration.
A few notes on the code:
You don't need to include string.h or ctype.h after iostream.
In C++, you may use std::string class to manipulate better with strings by using a number of helpful class functions (e.g. if you want to get the string length, then use variable.length(), etc.)
You shouldn't use:
using namespace std;
In large programs, they may cause ambiguity. Why is using namespace std considered bad practice?
The variable j is unused and redundant declaration.
Enhanced version of your code is as follows:
#include <iostream>
#include <string>
int main(void) {
// important declaration
std::string password;
char temp;
int alpha = 0, digit = 0;
do {
std::cout << "Enter a password: ";
std::cin >> password;
// getting the length
size_t len = password.length();
// conditions begins, checks if the password is greater than 8 (9 or more)
if (!(len > 8)) {
std::cout << "Password must be greater than 8 chars." << std::endl;
continue;
}
// testing each letter
for (size_t i = 0; i < len; i++) {
temp = password[i];
// if the char is an alphabet, alpha++
if (isalpha(temp)) alpha++;
// or a digit, digit++
if (isdigit(temp)) digit++;
}
// verifying if digit and alpha are greater than 0
if (alpha > 0 && digit > 0)
break;
else {
std::cout << "The password must have an alphabetic letter and a digit." << std::endl;
alpha = digit = 0;
}
} while (true);
// prints when out of loop reaches
std::cout << "Password was set successful." << std::endl;
return 0;
}
Output (sample test cases):
Enter a password: asdf // --------------------------------- < 8 chars
Password must be greater than 8 chars.
Enter a password: safasdfasdfasdf // ---------------------- > 8 chars, but no numeric
The password must have an alphabetic letter and a digit.
Enter a password: 34523523452345 // ----------------------- > 8 chars, but no alpha
The password must have an alphabetic letter and a digit.
Enter a password: asdfa34 // ------------------------------ alpha + numeric, but < 8 chars
Password must be greater than 8 chars.
Enter a password: asfasdfdsaf2 // ------------------------- a good password
Password was set successful.
You should add "break;" statment to where you want to break the loop after like
if(alp>0 && num>0){
cout<<"\nGood password\n";
break;
}
Or add some condition which break the loop
while(i<1){
if(alp>0 && num>0){
cout<<"\nGood password\n";
i=2;
}
}
Try using break when the password is good and also bad stuff will happen if the input is bigger than 9 characters, use std::string instead of char array something like this:
#include<iostream>
#include<string>
using namespace std;
int main()
{
int alp=0,num=0;
string a;
do
{
cout<<"\nEnter a password:";
cin>>a;
if(a.length()>8)
{
for(int i=0; i<a.length(); ++i)
{
if(isalpha(a[i]))
{
++alp;
}
else if(isdigit(a[i]))
{
++num;
}
}
if(alp>0 && num>0)
{
cout<<"\nGood password\n";
break;
}
else
{
cout<<"Your password must include atleast one digit and one uppercase\n";
}
}
else
{
cout<<"\nYour password must have atleast 8 characters";
}
}while(true);
return 0;
}
I'm trying to build a simple password validator:
Program prints
"Very weak" if password has less than 8 characters and all numbers.
"Weak" if password has less than 8 characters and all alphabets.
"Strong" if password has 8 or more characters and contains numbers and alphabets.
"Very strong" if password has 8 or more characters and contains numbers, alphabets and special characters.
As you can see, I know how to check if a string has either one of the three types of characters.
How can I check if a string has two or all three?
#include <iostream>
#include <ctype.h>
#include <cstring>
int main()
{
std::cout << "Enter your new password: ";
std:: string password{};
std::cin >> password;
bool veryweak;
bool weak;
bool strong;
bool verystrong;
if (password.length() < 8)
{
for (int i = 0; i < password.length(); i++)
{
if (isdigit(password[i]))
{
veryweak = true;
}
else if (isalpha(password[i]))
{
weak = true;
}
}
}
else if (password.length() >= 8)
{
for (int i = 0; i < password.length(); i++)
{
//if (password has digits and alphabets)
//strong = true;
//if (password has digits and alphabet and special characters)
//verystrong = true;
}
}
else
{
std::cout << "Password is invalid.";
}
//---------------------------------------------------------------------------------------------------------------------
if (veryweak)
{
std::cout << "Your password is very weak.";
}
else if (weak)
{
std::cout << "Your password is weak.";
}
else if(strong)
{
std::cout << "Your password is strong.";
}
else if (verystrong)
{
std::cout << "Your password is very strong.";
}
return 0;
}
I would introduce the booleans:
isShortPassword
containsNumbers
containsAlphabet
containsSpecialCharacters
than you can write
std::string passwortStrengt () {
if (isShortPassword && !containsAlphabet && !containsSpecialCharacters) {
return "weak";
//forumlate all cases as you did in prosa up above
} else if (...) {
} else if (...) {
}
return "weak"; // just in case you missed a case above
}
Why don't you use some counter like weakness_counter or something. For each property that is fulfilled, the counter is incremented by one. And at the end you check how much properties are fulfilled and rate the password strength after this.
Further I would recommend you to write an own function for each of your properties, like:
bool containsNumbers(string pw);
bool containsLetters(string pw);
And so on. In this case it's more easy to read, change and extend the code by new properties etc..
I hope I could help you.
Regards :)
This code is meant to detect REAL numbers from a string entered continuously by a user, and return the found real number as a double value . I was able to construct it to the point where it detects whole numbers, but if I try a decimal number it doesn't detect it. I think my error is within my isvalidReal() function, but I'm not sure how to move things around to get it to work.
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
double getaReal(); //function prototype
int value;
cout << "Enter a number: ";
value = getaReal();
cout << "the number entered is a real number: " <<value << endl;
return 0;
}
bool isvalidReal(string str) //function to find real numbers.
{
int start = 0;
int i;
bool valid = true;
bool sign = false;
if (int(str.length()) == 0) valid = false; //check for empty string
if (str.at(0) == '-' || str.at(0) == '+') //check for sign
{
sign = true;
start = 1; //check for real num at position 1
}
if (sign && int(str.length()) == 1) valid = false; //make sure there's atleast 1 char after the sign
i = start;
while (valid && i<int(str.length()))
{
if (!isdigit(str.at(i))) valid = false; //found a non-digit character
i++; // move to next character
}
return valid;
}
double getaReal()
{
bool isvalidReal(string); //function declaration prototype
bool isnotreal = true;
string input;
while (isnotreal)
{
try
{
cin >> input; //accepts user input
if (!isvalidReal(input)) throw input;
}
catch (string e)
{
cout << "No real number has been detected, continue to\n enter string values: ";
continue; //continues user input
}
isnotreal = false;
}
return atof(input.c_str());
}
So I've been trying to create this program that will take up to 12 digits from the user using string and string classes. The issue I'm having is:
Ignoring the (-) sign.
Ignoring the decimal point.
Giving an error when more than 12 digits are entered.
Only accepting digits (i.e no letters)
So far this is what I have:
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
using namespace std;
bool test(char [] , int);
int main()
{
const int SIZE= 13;
char number[SIZE];
int count;
cout<< "Please enter a number up to "<< (SIZE-1) <<" digits long." << endl;
cout<< "The number may be positive or negative" << endl;
cout<< "and may include fractions (up to two decimal positions)" << endl;
cout<< "Sign and decimal dot(.) are not included in the digit count:"<< "\t";
cin.getline (number, SIZE);
if (test(number, SIZE))
{
while (number[count]!='\0')
{
cout<< "The currency value is: \t $";
cout<< setprecision(2) << number[count];
count++;
}
}
else
{
cout << "Invalid number: contains non-numeric digits.";
}
return 0;
}
bool test(char testNum[], int size)
{
int count;
for (count = 0; count< size; count++)
{
if(!isdigit(testNum[count]))
return false;
}
return true;
}
Any help is very much appreciated, but the most important to me at the moment is the 4th point. No matter what the input is, the output is "Invalid number:...." and I'm not sure why that is.
Your test function always test 13 chars even if the input is shorter.
Instead pass a string and use the range based for-loop so that you only test the valid chars - something like:
bool test(string testNum)
{
for (auto c : testNum)
{
if(!isdigit(c))
return false;
}
return true;
}
Further you should change your main-loop (where you print the value) as well, i.e. use string instead of char-array.
BTW - notice that this will only check for digits. Your description of the valid input format will require a more complex test-function.
For instance to check for sign you could add:
bool test(string testNum)
{
bool signAllowed = true;
for (auto c : testNum)
{
if (c == '-')
{
if (!signAllowed) return false;
}
else
{
if(!isdigit(c)) return false;
}
// Sign not allowed any more
signAllowed = false;
}
return true;
}
But you still need more code to check for the dot (.)
If you don't want to use a range-based for loop, you can do:
bool test(string testNum)
{
for (int i = 0; i < testNum.size(); i++)
{
if (testNum[i] == '-')
{
// Sign is only allowed as first char
if (i != 0) return false;
}
else
{
if(!isdigit(testNum[i])) return false;
}
}
return true;
}
I can't get my password verify program to work. My loop seems to only iterate once, I just put "it" as output to see if it's constantly iterating, but its not. I'm not sure why, the booleans are working, but it just iterates once, and if my first letter is a lowercase, then it'll say I need an uppercase and a digit, and vice-versa if my first character is a digit or uppercase. This is a homework assignment, but I'm a little lost. Any help would be greatly appreciated.
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
const int LENGTH = 20;
char pass[LENGTH];
cout << "Enter a password, that's at least 6 characters long, one uppercase, one lowercase letter ";
cout << " and one digit." << endl;
cin.getline(pass,LENGTH);
bool isdig = true;
bool isdown = true;
bool isup = true;
bool correct = false;
for(int index = 0; correct == false; index++)
{
cout << "it" << endl;
if(isupper(pass[index]) == 0)
{isup = false;}
if(islower(pass[index]) == 0)
{isdown = false;}
if(isdigit(pass[index]) == 0)
{isdig = false;}
if(isdig == true && isup == true && isdown == true)
{correct = true;}
if(index = LENGTH - 1)
{
if(isdig == false)
{cout << "Your password needs a digit." << endl;}
if(isup == false)
{cout << "Your password needs an uppercase letter." << endl;}
if(isdown == false)
{cout << "Your password needs a lowercase letter." << endl;}
cout << "Re-enter another password. " << endl;
cin.getline(pass,LENGTH);
index = 0;
isdown = true;
isup = true;
isdig = true;
}
}
system("pause");
return 0;
}
The problem is probably this line:
if(index = LENGTH - 1)
Here you assign the value of LENGTH - 1 to index, so you are always asked to re-enter your password as that expression always is true.
You should enable your compiler warning (-Wall if you are using g++) and pay attention to the warnings:
es.cpp:52:30: warning: suggest parentheses around assignment used as truth value
This tells you that some condition (a==b) as probably been written as (a=b) which is an assignment. And indeed
if(index = LENGTH - 1)
should be written
if (index == LENGTH - 1)
Also for readability
if(isdig == true && isup == true && isdown == true)
could be replaced by
if (isdig and isup and isdown)
and
if(isdig == false)
by
if (not isdig)