Unnamed namespace

Pages: 12
Write your question here.
Perform input validation for a username and password.Also create header files


file user.cpp
namespace Authenticate
{
User::User():username("No name yet")
{
//empty
}
void inputUserName()
{
do
{
cout<<"Enter your username (8 letters only)"<<endl;
cin>>username;
}while (!isvalid());
}
string getUserName()
{
return username;
}
}

file password.cpp
namespace Authenticate
{
void inputPassword()
{
do
{
cout <<"Enter your password (at least 8 characters "<<
"and at least one nonletter)"<<endl;
cin >>password;
}while(!isValid());
}
string getPassword()
{
return password;
}
}

file validation.cpp
#include <fstream>
#include <string>
#include<cstdlib>
#include<iostream>
using namespace std;

int main()
{
inputUsername();
inputPassword();
cout <<"Your username is "<<getUserName()<<
" and your password is: "<<
getPassword()<<endl;

return 0;
}
What is your question?
Define the username variable and the isValid() function for user.cpp and password.cpp.
You forgot to ask a question. Questions end with question mark characters "?". An example of a question is "Why does my program install a virus?".

https://en.wikipedia.org/wiki/Question
Last edited on
The program must compile. The functions and variables must be defined.I am stuck. files user.cpp and password.cpp must compile and run. I have also added a part of the the main function.
I have added 3 files: user.cpp, password.cpp and validation.cpp. The program must validate the username and the password.using unnamed nampespace and header
closed account (E0p9LyTq)
Headers and Includes: Why and How
http://www.cplusplus.com/forum/articles/10627/
This program must compile
We still do not know your question, explain to us how you are compiling this.
Try:
 
g++ -std=c++14 user.cpp password.cpp validation.cpp -Wall
File user.cpp are snippets from a program to perform input validation for a username and password. The code to input and validate the username is in a file separate from the code to input and validate the password. Define the username variable and the isValid() function in the unnamed namespace so the code wille compile. Generate a header file for this code. Repeat the same steps for file password.cpp. The main function is in file validation.cpp
Firstly, this isn't a homework site. Secondly, you haven't posted your code in code tags. And thirdly, even the above isn't a question. It's an assignment, or a group of commands. This website is set up so people can ask self contained questions. For example, try and compile your program, see what errors you get, post those errors and the lines they occur on and ask how to solve them.
try this :

//user.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <iostream>

namespace Authenticate
{
	
	extern std::string username;

	bool isvalid();
	
	void inputUserName();
	std::string getUserName();
	
}


//user.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "user.h"

namespace Authenticate
{
	std::string username = "";
	void inputUserName()
	{
		do
		{
			std::cout<<"Enter your username (8 letters only)"<<std::endl;
			std::cin>>username;
		}while (!isvalid());
	}
	std::string getUserName()
	{
		return username;
	}
	bool isvalid(void)
	{
		return std::strlen(username.c_str()) <= 8;
	}
}


//password.h
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>

namespace Authenticate
{
	extern std::string password;

	bool isValid();
	void inputPassword();
	std::string getPassword();
}


//password.cpp
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
#include "password.h"



namespace Authenticate
{
	std::string password = "";
	void inputPassword()
	{
		do
		{
		std::cout <<"Enter your password (at least 8 characters "<<
		"and at least one nonletter)"<<std::endl;
		std::cin >>password;
		}while(!isValid());
	}
	std::string getPassword()
	{
		return password;
	}
		bool isValid(void)
	{
		return std::strlen(password.c_str()) <= 8;
	}
}


//validation.h

1
2
3
#include "password.h"

#include "user.h" 


//validation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "validation.h"


#include <fstream>
#include <string>
#include<cstdlib>
#include<iostream>
using namespace std;

int main()
{
	Authenticate::inputUserName();
	Authenticate::inputPassword();
	cout <<"Your username is "<<Authenticate::getUserName()<<
	" and your password is: "<<
	Authenticate::getPassword()<<endl;

	system("pause");
	return 0;
}


Hope that helps
closed account (E0p9LyTq)
Still no question being asked.

You need to try to compile your source code and then tell us what errors you receive, if any.
Hi Ericool. I get error message 'strlen' is not a member of 'std' on file user.cpp and password.cpp. On authenticate.cpp the error is : undefined reference to Authenticate::inputPassword()
closed account (E0p9LyTq)
strlen() is a C-library string function, you need to add the <cstring> or <string.h> header.

<cstring> is recommended with new C++ source.
Last edited on
Does anyone know the solution to this problem? Been experiencing multiple errors. Thank you!

Create a class named DentalAppointment. Include fields for a patient’s data (use the Person class),
the date (using the Date class), the time (use the Time class), and the duration of the appointment
in minutes. Also include a field that contains the ending time of the appointment; this field will be
calculated based on the start time and the duration using the Time class function that adds minutes
to a Time object. The DentalAppointment constructor requires a first and last name, and a month,
day, year, hour, and minute for the appointment. Allow a DentalAppointment to be constructed
with or without an additional argument for appointment duration, and force the duration to 30
minutes when no argument is supplied. The constructor does not allow any appointment over 240
minutes. The constructor calculates the appointment ending time based on the start time and the
duration. Also include a display function for the DentalAppointment class. Write a main()function
that loops at least three times, prompting the user for DentalAppointment data and displaying all
the information. (Note, if you use the Person class display function, the zip code will be “X”; this is acceptable.)

Note: We already made a file in notepad, ClassData.txt
So, am I going to use instream infile ofstream outfile? Thank you again!
Last edited on
closed account (E0p9LyTq)
@Ericool

Why use a C-library string length function? A C++ string keeps track of its size.

user.cpp:
1
2
3
4
bool isvalid()
{
   return (username.size() == 8);
}


password.cpp:
1
2
3
4
bool isValid()
{
   return (password.size() >= 8);
}
@ericool:

user.cpp line 28, password.cpp line 23:
strlen is a 'C' function. It is not a member of the C++ std:: namespace.
Since username is a std::string, we can rewrite line 28 as simply:
 
  return username.size() <= 8;

I would suggest checking for a minimum length also.

@calz:
Making Authenticate a namespace is rather funky. Why not just make it a class?
You're making username and password globals within the Authenticate namespace, which is something that should be avoided.

Something like this:
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
#include <string>
#include <iostream>
using namespace std;

class Authenticate
{   string  username;
    string  password;
    
public:
    Authenticate ();
	bool user_valid () const;
	bool password_valid () const; 
	string getUserName() const;	
	string getPassword() const;
	
	void inputUserName();
	void inputPassword();
};

void Authenticate::inputUserName()
{   do
	{   cout<<"Enter your username (8 letters only)"<<std::endl;
		std::cin>>username;
	} while (! user_valid());
}

string Authenticate::getUserName() const
{   return username;
}

bool Authenticate::user_valid () const
{   return username.size() <= 8;
}

void Authenticate::inputPassword()
{   do
	{   cout <<"Enter your password (at least 8 characters "<<
		"and at least one nonletter)"<<std::endl;
		cin >>password;
	} while (! password_valid());
}
	
string Authenticate::getPassword() const
{   return password;
}

bool Authenticate::password_valid () const
{   return password.size() <= 8;
}

int main()
{   Authenticate    user;

	user.inputUserName();
	user.inputPassword();
	cout << "Your username is " << user.getUserName() 
	    << " and your password is: " << user.getPassword() << endl;

	system("pause");
	return 0;
}


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
closed account (E0p9LyTq)
@AbstractionAnon

The OP needs the username to be exactly 8 characters, the password 8 or more characters.
I rarely use string but const char * , you can use string as well of course . For the namespace instead of a class , sure , but as you can see there were namespace in 2 cpp file in the post so I did like that . I did not get the question also .
Pages: 12