I'm working in a project and the task is to make the signup function so when I try several test cases in password function I got stuck in an infinite loop when I try to type an invalid password without giving me a chance to retype the password. Thanks in advance...
#include <iostream>
#include <string>
#include <cstring>
#include <conio.h>
#include <stdio.h>
using namespace std;
#define size 40
int main(){
bool flag = true;
while (flag){
bool flagS = false, flagN = false; int count = 0;
char password[size] = { 0 };
cout << "Type your Password .....\n Note :: Must be more than 8 characters including at least one number and one special character...\n";
cin.get(password, size);
count = strlen(password);
for (int z = 0; z < count; z++){
if (password[z] >= 48 && password[z] <= 57)
flagN = 1;
if ((password[z] >= 33 && password[z] <= 47) || (password[z] >= 58 && password[z] <= 64))
flagS = 1;
}
if ((flagS == 1) && (flagN == 1) && (count >= 8))
{
cout << "Valide Password ...\nCongrats!! ..you created a NEW account.." << endl;
flag = false;
}
else
{
cout << "invalide password..\nPlease try again..\n";
flag = true;
}
}
}
You must write cin
inside of while
loop. And this will fix your problem:
#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
#define size 40
int main(){
bool flag = true;
bool flagS = false, flagN = false; int count = 0;
char password[size] = { 0 };
cout << "Type your Password .....\n Note :: Must be more than 8 characters including at least one number and one special character...\n";
while (cin >> password && flag){
count = strlen(password);
for (int z = 0; z < count; z++){
if (password[z] >= 48 && password[z] <= 57)
flagN = true;
if ((password[z] >= 33 && password[z] <= 47) || (password[z] >= 58 && password[z] <= 64))
flagS = true;
}
if ((flagS) && (flagN) && (count >= 8))
{
cout << "Valide Password ...\nCongrats!! ..you created a NEW account.." << endl;
flag = false;
}
else
{
cout << "invalide password..\nPlease try again..\n" << "Type your Password .....\n Note :: Must be more than 8 characters including at least one number and one special character...\n";;
flag = true;
}
}
}
std::istream::get
does not remove the newline from the stream. This means the next call to std::istream::get
immediately finds the newline and reads nothing into the buffer, sets the failbit, and returns. Because the steream is now in the fail state all subsequent reads instantly fail. You could cin.ignore()
the newline, but a more direct approach is to use std::istream::getline
because it removes the newline for you.
Always test the stream state after an IO transaction to make sure it succeeded.
Side note:
Prefer to use std::string
and std::getline
to raw character arrays and functions that use them. std::istream::get
and std::istream::getline
both mark the stream as failed if the buffer provided is too small. The string
provided to std::getline
will be resized, so as long as you have dynamic storage remaining, buffer over runs and the errors raised to prevent them are a non-problem. If the system cannot allocate a buffer of sufficient size for the string
an exception is thrown to make you aware of the problem.
Prefer to use letters, for example 'A'
to a raw ASCII code. For one thing the intent of 'A"
is immediately recognizable to even the most raw of programmers and there is always the possibility that the implementation does not use ASCII as the default encoding.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.