for some reason the code is just ending instantly



#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <bits/stdc++.h>
#include <fstream>
using namespace std;

void enterEmail(string& email, int& e);
void checkEmail ( string& email,int& e, char emaila[], bool checkamp, int percheck, bool checkper);
void enterPassword(string password, int& p);
void checkPassword(int& p, string password,char pasworda[],bool checklow,bool checkup,bool checksymbol);
void upload(string email, string password);




int main()
{ string email,
password;
int e,
p,
percheck;
char emaila[e+1],
passworda[p+1];
bool checkamp=false,
checkper=true,
checksize=false,
checklow=false,
checkup=false,
checksymbol=false;


for(int i=0;i<3;i++)
{
enterEmail(email, e);

checkEmail (email, e,emaila,checkamp,percheck, checkper);

enterPassword(password, p);

checkPassword( p, password, passworda, checklow, checkup, checksymbol);

upload(email, password);
}




}

void enterEmail( string& email, int& e)
{ cout<<"email: ";
cin>>email;
e = email.length();

}

void checkEmail( string& email,int& e, char emaila[], bool checkamp, int percheck, bool checkper)
{
strcpy(emaila, email.c_str());

for(int i=0;i<e+1;i++)
{

emaila[i];

if(emaila[i]='@')
{
checkamp= true;
i=percheck;
}




}
if (emaila[percheck + 1] == ('.')||emaila[percheck + 2] ==('.'))
{
checkper = true;
}

else if(checkamp==false)
{
cout<<"Sorry you neeed a @\n";

enterEmail(email,e);;
}

if(checkper==false)
{
cout<<"Sorry you neeed a .\n";
enterEmail(email,e);
}




}

void enterPassword(string password, int& p)
{
cout<<"\nPassword";
cin>>password;
p = password.length();
}

void checkPassword(int& p, string password,char passworda[],bool checklow,bool checkup,bool checksymbol)
{
if(p<9)
{
cout<<"Sorry your password must be at least 9 characters";
enterPassword(password, p);

}

strcpy( passworda, password.c_str());

for(int i=0;i<p+1;i++)
{ passworda[i];

if(islower(passworda[i]))
{
checklow=true;
}

else if(isupper(passworda[i]))
{
checkup=true;
}

else if (ispunct(passworda[i]))
{
checksymbol=true;

}

}

if(checkup==false)
{
cout<<"Sorry your password seems to be missing a uppercase letter/n";
enterPassword(password, p);

}

if(checksymbol==false)
{
cout<<"sorry your password needs a symbol/n";
enterPassword(password,p);
}


if(checklow==false)
{
cout<<"sorry your password needs a undercase letter /n";
enterPassword(password,p);
}
}

void upload( string email, string password)
{
ofstream fout;
fout.open("email.txt",ofstream::app);
fout<< email<<"\n";
fout<< password<<"\n";


}



Please use code tags. See http://www.cplusplus.com/articles/jEywvCM9/
1
2
3
4
5
int main()
{
  string email, password;
  int e, p, percheck;
  char emaila[e+1], passworda[p+1];

Q: How many elements are in array emaila?
A: e+1?

Q: What is the value of e+1?
A: Undefined

Q: Is it valid C++ to define an array, whose size is not known during compilation?
A: No

Why do you have an array at all? You could use the std::string. That behaves almost like an array (but better).
By initializing a handful of integer variables and commenting out the #include shown ???, program produces the output shown on my machine. Not so much on c++shell

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
//#include <bits/stdc++.h> ???
#include <fstream>
using namespace std;

void enterEmail(string& email, int& e);
void checkEmail ( string& email,int& e, char emaila[], bool checkamp, int percheck, bool checkper);
void enterPassword(string password, int& p);
void checkPassword(int& p, string password,char pasworda[],bool checklow,bool checkup,bool checksymbol);
void upload(string email, string password);




int main()
{ string email,
    password;
    int e{0},
    p{0},
    percheck{0};
    char emaila[e+1],
    passworda[p+1];
    bool checkamp=false,
    checkper=true,
    checksize=false,
    checklow=false,
    checkup=false,
    checksymbol=false;
    
    
    for(int i=0;i<3;i++)
    {
        enterEmail(email, e);
        
        checkEmail (email, e,emaila,checkamp,percheck, checkper);
        
        enterPassword(password, p);
        
        checkPassword( p, password, passworda, checklow, checkup, checksymbol);
        
        upload(email, password);
    }
    
    
    
    
}

void enterEmail( string& email, int& e)
{ cout<<"email: ";
    cin>>email;
    e = email.length();
    
}

void checkEmail( string& email,int& e, char emaila[], bool checkamp, int percheck, bool checkper)
{
    strcpy(emaila, email.c_str());
    
    for(int i=0;i<e+1;i++)
    {
        
        emaila[i];
        
        if(emaila[i]='@')
        {
            checkamp= true;
            i=percheck;
        }
        
        
        
        
    }
    if (emaila[percheck + 1] == ('.')||emaila[percheck + 2] ==('.'))
    {
        checkper = true;
    }
    
    else if(checkamp==false)
    {
        cout<<"Sorry you neeed a @\n";
        
        enterEmail(email,e);;
    }
    
    if(checkper==false)
    {
        cout<<"Sorry you neeed a .\n";
        enterEmail(email,e);
    }
    
    
    
    
}

void enterPassword(string password, int& p)
{
    cout<<"\nPassword";
    cin>>password;
    p = password.length();
}

void checkPassword(int& p, string password,char passworda[],bool checklow,bool checkup,bool checksymbol)
{
    if(p<9)
    {
        cout<<"Sorry your password must be at least 9 characters";
        enterPassword(password, p);
        
    }
    
    strcpy( passworda, password.c_str());
    
    for(int i=0;i<p+1;i++)
    { passworda[i];
        
        if(islower(passworda[i]))
        {
            checklow=true;
        }
        
        else if(isupper(passworda[i]))
        {
            checkup=true;
        }
        
        else if (ispunct(passworda[i]))
        {
            checksymbol=true;
            
        }
        
    }
    
    if(checkup==false)
    {
        cout<<"Sorry your password seems to be missing a uppercase letter/n";
        enterPassword(password, p);
        
    }
    
    if(checksymbol==false)
    {
        cout<<"sorry your password needs a symbol/n";
        enterPassword(password,p);
    }
    
    
    if(checklow==false)
    {
        cout<<"sorry your password needs a undercase letter /n";
        enterPassword(password,p);
    }
}

void upload( string email, string password)
{
    ofstream fout;
    fout.open("email.txt",ofstream::app);
    fout<< email<<"\n";
    fout<< password<<"\n";
    
    
}


email: 
Last edited on
You use both std::strings and C-style arrays.
That isn’t a problem, but, echoing keskiverto’s advice, if you may use std::strings, why using C-style arrays at all?

Please consider:
- std::strings can expand as much as needed (vs C-style arrays: you need to allocate enough memory from the very beginning);

- you can safely return std::strings from functions (vs C-style array: you need to guarantee the memory they are pointing to won’t be deleted);

- std::strings know their dimension (you don’t need to store that piece of information);

- you can easily copy std::strings by means of the assignment operator = (vs C-style array: you need to provide a large enough array and than invoke a proper function)

What I mean is, even if you can mix them up, std::strings and C-style arrays lead to quite different coding styles, so the easiest thing is to pick one of them and then stick to it.


- - -
About your code, please consider:
- do not add arguments you don’t need.
You can declare variables like ‘checkup’, ‘checksymbol’ inside checkPassword(), therefore you don’t need to pass them. Keep things local, if you can.

- KISS (keep it stupid simple)
a) sometimes using return values is simpler than passing by reference

b) If you are not going to modify a variable, why copying it?
(Inside checkEmail() and checkPassword() you don’t need to copy the passed array)

c) return instead of declaring new variables (when it makes sense)
For example, once you have checked there’s no @ in the email, just return an error value: you have already assessed the mail address is incorrect, you don’t need to perform the following checks.

- One function should do just one thing.
A function which checks whether an email address is correct or not should not ask the user for a new one.


- - -
What about this logic?
1
2
3
4
5
6
7
8
9
- LOOP 3 TIMES
    - ask for email and store it
    - LOOP UNTIL the email address is correct (--> loop if it is wrong!)
        - ask for new email (overwriting the previous one)
    - ask for password and store it
    - LOOP UNTIL the password is correct (--> loop if it is wrong!)
        - ask for new password (overwriting the previous one)
    - save email and password in a file
- END LOOP

checkEmail() logic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- declare an int variable 'found' to -1
- loop through the string
    - is there an @ ?
        - yes --> save its position inside found
- end loop
- is found == -1 ?
    - yes --> return false

- declare a bool variable 'check' to false
- loop through the string starting from previous saved position
    - is there a dot ?
        - yes --> set check to true
- end loop
return check;


Similar logic may apply to checkPassword().
Topic archived. No new replies allowed.