Correct bool mistakes! This a prgoram which can help you edit a password. You wi
ID: 3665314 • Letter: C
Question
Correct bool mistakes!
This a prgoram which can help you edit a password. You will be asked retype 0 or 1, if you type wrong number in parts which are asked you "Do you want........0-No 1-Yes". I write a bool function to make a choice. If user types wrong number they will be asked type again until they type 0 or 1. It works when user type 0, but it doesn't work when user type 1. When user type 1, logically program will continue next step. But, in fact if user type 1 when they asked retype they will be asked again. So that means user will be asked twice retype, and user need type twice 1 then grogram continue next step. This the example for program working.
Do you want letters? (0-no, 1-yes) 2
Your type should be (0 or 1) 1
Your type should be (0 or 1) 1
Do you want uppercase letters? (0-no, 1-yes)2
Your type should be (0 or 1) 0
Do you want lowercase letters? (0-no, 1-yes)
The mistake might be in bool function.
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <string>
#include <cmath>
using namespace std;
double ps, pn, pu, pl;
int number, wantletters, passwordlong, lowlett, wantupp;
string a;
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <string>
#include <cmath>
using namespace std;
double ps, pn, pu, pl;
int number, wantletters, passwordlong, lowlett, wantupp;
string a;
bool repeat(string a){
do{
if(a[0]!='1' && a[0]!='0'){
cout<<"Your type should be (0 or 1) ";
cin>>a;
}
if(a[0]=='0'|| a[0]=='1'){
break;
}
}while(a[0]!='1'&& a[0]!='0');
while(a[0]=='0'|| a[0]=='1'){
if(a[0]=='1'){
return true;
break;
}
if(a[0]=='0'){
return false;
break;
}
}
}
int main(){
srand(time(NULL));
double blahu, blahl, blahspe, perupp, perlow, k=1, letternumber, passward, blahnumber, spechar;
while (k==1){
cout<< "How long do you want password to be? " ;
cin >> passwordlong;
if (passwordlong > 0){
cout<<"Do you want letters? (0-no, 1-yes) " ;
cin>>a;
if (repeat(a)==false){
pu=0; pl=0;
}
else if (repeat(a)==true){
cout<<"Do you want uppercase letters? (0-no, 1-yes)" ;
cin>>a;
if (repeat(a)==false){pu=0;
}
else if (repeat(a)==true){cout<<"What percent of the password should be uppercase? ";
cin>> pu;
}
cout<<"Do you want lowercase letters? (0-no, 1-yes) ";
cin>>a;
if (repeat(a)==false){pl=0;
}
else if (repeat(a)==true){cout<<"What percent of the password shoud be lowercase? ";
cin>> pl;
}
}
cout<<"Do you want numbers(0-no, 1-yes) ";
cin>>a;
if (repeat(a)==false){pn=0;
}
else if (repeat(a)==true){cout<<"What percent of the password should be numbers?";
cin>>pn;
}
cout<<"Do you want special characters?(0-No, 1-yes) ";
cin>>a;
if (repeat(a)==false){ps=0;
}
if (repeat(a)==true){
ps=(100-pl-pu-pn);
if(((pl+pu+pn)>100) || ((pl+pu+pn)<0) || ((pl/100)>1) ||((pu/100)>1) || ((pn/100)>1) || ((abs(pl)+abs(pn)+abs(pu)+abs(ps))/100)>1){
cout<<"Your percentage is illegal.";
cout<<" "<
}
else if(((pl+pu+pn)<100)||((pl+pu+pn)>0)){
cout<<""<
cout<<"Your password is : ";
for(int i=0; i<(passwordlong*(pu/100)); i++){
blahu=rand()%26+65;
cout<
}
for(int i=0; i<(passwordlong*(pl/100)); i++){
blahl=rand()%26+97;
cout<
}
for(int i=0; i<(passwordlong*(ps/100)); i++){
blahspe=rand()%15+33;
cout<
}
for(int i=0; i<(passwordlong*(pn/100)); i++){
blahnumber=rand()%10+48;
cout<
}
cout<<""<
cout<<""<
}
}
}
cout<<""<
cout<<"Do you want to run one more time? (1-Yes, other number--NO)" ;
cin >>k;
cout<<""<
}
return 0;
}
Explanation / Answer
These lines and brackets should be corrected:
if (passwordlong > 0){
cout<<"Do you want letters? (0-no, 1-yes) " ;
cin>>a;
if (repeat(a)==false){
pu=0; pl=0;
}
else if (repeat(a)==true){
cout<<"Do you want uppercase letters? (0-no, 1-yes)" ;
cin>>a;
}
if (repeat(a)==false){pu=0;
}
else if (repeat(a)==true){cout<<"What percent of the password should be uppercase? ";
cin>> pu;
cout<<"Do you want lowercase letters? (0-no, 1-yes) ";
cin>>a;
}
if (repeat(a)==false){pl=0;
}
else if (repeat(a)==true){cout<<"What percent of the password shoud be lowercase? ";
cin>> pl;
}
}
cout<<"Do you want numbers(0-no, 1-yes) ";
cin>>a;
if (repeat(a)==false){pn=0;
}
else if (repeat(a)==true){cout<<"What percent of the password should be numbers?";
cin>>pn;
}
cout<<"Do you want special characters?(0-No, 1-yes) ";
cin>>a;