I have to write five unit tests and im getting quite a few errors
May 25, 2020 at 4:53pm UTC
I'm required to write 5 unit tests. I'm getting several errors. Can someone please fix class test so I know how to go about writing these tests?
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
#include <iostream>
using namespace std;
#include <fstream>
#include <string>
class user {
public :
char choice1;
string newpass;
fstream usernames;
fstream pswd;
fstream npswd;
fstream rnusernames;
fstream rnpass;
fstream rmaide;
};
class olduser : public user {
private :
string temp;
string temp1;
string temp2;
string username;
string userpass;
string newuserpass;
string loginyn;
string mynewpass;
public :
int checkusername(){
usernames.open("names.txt" );
cout << "\nplease enter your username " ;
cin >> username;
while ((usernames >> temp)) {
if (temp.compare(username) == 0) {
//cout << "found";
usernames.close();
return 1;
}
}
usernames.close();
return 0;
}
int checkpassword(){
cout<<"please enter your password " ;
cin>>userpass;
pswd.open("pass.txt" );
while (pswd >> temp1) {
if (temp1.compare(userpass) == 0){
cout<<"\n[ " <<username<<" ] having password [ " <<userpass<<" ] has been found in the database.\n\n" ;
pswd.close();
return 1;
}
}
pswd.close();
return 0;
}
int checknewpassword(){
cout<<"please enter your password " ;
cin>>newuserpass;
npswd.open("newpass.txt" );
while (npswd >> temp2) {
if (temp2.compare(newuserpass) == 0){
cout<<"Congratulations!!! You have successfully logged in with your new password." ;
npswd.close();
return 1;
}
else {
cout<<"ACCESS DENIED" ;
}
}
npswd.close();
return 0;
}
int creatnewpass(){
cout<<"Password: " ;
cin>>mynewpass;
npswd.open("newpass.txt" );
npswd<<mynewpass;
npswd.close();
cout<<"\nLogin now? (y/n) " ;
cin>>loginyn;
if (loginyn=="y" ){
cout<<"\n [LOGIN PROCESS STARTING...]\n" ;
checkusername();
checknewpassword();
}
return 0;
}
private :
};
class newuser : public user {
private :
string nusername;
string rpass;
string maiden;
public :
void askusername(){
cout<<"\nUsername: " ;
cin>>nusername;
rnusernames.open("newnames.txt" );
rnusernames<<nusername;
rnusernames.close();
}
void askuserpass(){
cout<<"Password: " ;
cin>>rpass;
rnpass.open("newpass.txt" );
rnpass<<rpass;
rnpass.close();
}
void askMMN(){
cout<<"Mother's maiden name: " ;
cin>>maiden;
rmaide.open("maidennames.txt" );
rmaide<<maiden;
rmaide.close();
}
};
class test{
public :
user u;
newuser r;
u.choice1 = "y" ;
r.nusername = "name" ;
r.rnpass = "pass" ;
r.maiden="maiden" ;
};
/*
class test1{
public:
user u;
olduser verify;
u.choice1 = "n";
success=0;
};
class test2{
public:
user u;
olduser verify;
u.choice1 = "n";
success = 1;
};
class test3{
public:
user u;
olduser verify;
u.choice1 = "x";
};
*/
int main() {
test t;
newuser r;
user u;
olduser verify;
int attempts, success;
cout<<"\nAre you a new user? (y/n) " ;
cin>>u.choice1;
if (u.choice1 == 'n' ){
for (attempts = 1; attempts < 4; attempts++){
success = verify.checkusername();
if (success == 1) {
break ;
}
}
if (success == 0){
cout<<"\nACCESS DENIED\n" ;
cin.ignore();
abort();
}
verify.checkpassword();
cout<<"would you like to create a new password? (y/n) " ;
cin>>u.newpass;
if (u.newpass=="y" ){
verify.creatnewpass();
}
else if (u.newpass=="n" ){
cin.ignore();
abort();
}
}
else if (u.choice1 == 'y' ){
r.askusername();
r.askuserpass();
r.askMMN();
test();
cin.ignore();
abort();
}
else {
cout<<"Error" ;
}
return 0;
}
May 25, 2020 at 5:45pm UTC
line 131 -> use single quotes for a char
line 132-134 -> you can't access private variables in a class
May 25, 2020 at 5:59pm UTC
i've already tried that. For more information these are the errors:
[cquery] unknown type name 'r'
[cquery] unknown type name 'u'
[cquery] expected member name or ';' after declaration specifiers
May 25, 2020 at 6:50pm UTC
For private variables you have to write "getters and setters" bc they can't be accessed by anything outside of class methods. To created class objects they don't exist.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
class test {
private : string name;
public :
test()
:name(){}
string getName() { return name; }
void setName(string n) { name = n; }
};
int main() {
test t;
t.setName("namey" );
cout << t.getName();
}
Last edited on May 25, 2020 at 6:56pm UTC
May 25, 2020 at 6:50pm UTC
131-134
You can't access the members of a class instance outside of the scope of a function. It's like having a "std::cout <<" call in the global scope.
Last edited on May 25, 2020 at 6:50pm UTC
Topic archived. No new replies allowed.