This program is to accept input from a user to select there own username and then a password which will be compared to the password KazzyB, and give an output saying Welcome, The only problem in it is a single error KB.cpp(18) : error C2447: missing function header (old-style formal list?)

#include <iostream.h>
#include <string.h>
class Password
{
	char user[6], pass[6], username[6], passwrd[6], KazzyB;
public:
	Password(void);
	void user(void);
	void set(void);
};
Password::user(void)
{
	cout<<"Enter username"<<endl;
	cin>>user;
	cout<<"enter password:"<<endl;
	cin>>pass;
}
void Password::set(void);
{
if (pass = KazzyB)
{
	cout<<"user"<<endl;
	cout<<"Welcome"<<endl;
}
else if (pass != KazzyB)
{
	cout<<"user"<<endl;
	cout<<"Enter passwrd again"<<endl;
	cin>>pass;
}
else if (pass != KazzyB)
{
	cout<<"user"<<endl;
	cout<<"Good bye"<<endl;
	exit;
}
}
void main()
{
	Password y;
	y.user();
	y.set();
	
}

Recommended Answers

All 15 Replies

Well, I don't know if it fixes your error but:

char user[6], pass[6], username[6], passwrd[6], KazzyB;

You declare KazzyB as a char, and pass is an char with a array of 6.

if (pass = KazzyB)

else if (pass != KazzyB)
etc..

You are comparing an char with a char[6].

You have a ; at the end of line 18 that should be there.

However once you fix that I would expect all of these types of comparison if (pass = KazzyB) tp cause an error, or certainly not do what you expect.

KB.cpp(18) : error C2447: missing function header (old-style formal list?)

public:
	Password(void);

I would not expect to see void void being used as a parameter () is standard.

but what I don't see is a definition of

Password::Password() 
{
}

and the compiler might be complaining that it thinks that you have a function without a return type and not seeing a constructor.
though you might well just not have shown us :)

as well as line 18 as banfa says

this is the corrected program with no errors but its not comparing the entered password correctly as i want it too

#include <iostream.h>
#include <string.h>

class Password
{
	char owner[6], pass[6];
	
	
public:
	Password(void);
	void user(void);
	void set(void);
};
Password::Password(void)
{
}
void Password::user(void)
{
	cout<<"Enter username"<<endl;
	cin>>owner;
}
void Password::set(void)
{
	cout<<"enter password:"<<endl;
	cin>>pass;	
if (pass == "Kazzy")
{
	cout<<owner<<endl;
	cout<<"Welcome"<<endl;
}
else if (pass != "Kazzy")
{
	cout<<owner<<endl;
	cout<<"Enter passwrd again"<<endl;
	cin>>pass;
}
else if (pass != "Kazzy")
{
	cout<<owner<<endl;
	cout<<"Enter passwrd again"<<endl;
	cin>>pass;
}
else if (pass != "Kazzy")
{
	cout<<owner<<endl;
	cout<<"Good bye"<<endl;
	"exit";
}
}
void main()
{
	Password y;
	y.user();
	y.set();
	
}

You can't compare char arrays using the == or the != operators and you can't compare C style strings (null terminated char arrays) using those operators either. To compare char arrays you need to use a loop and compare each char in each array one by one. To compare C style strings you need to use strcmp(), or similar, functions found in the cstring header file (or string.h header file if your compiler doesn't allow namespace std;).

Note: using void as the return type for main() requires you to type an extra char compared with int and it isn't standard so it isn't as portable.

am not really so good with c++ can you please tell me how I should go about comparing them?
I tried something but now i get four errors stating:

error C2664: 'strcmp' : cannot convert parameter 2 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called.

There are four errors each pointing to the if and else ifs

#include <iostream.h>
#include <string>
using namespace std;
class Password
{
	char owner[6], pass[6];
	string s1;
public:
	Password(void);
	void user(void);
	void set(void);
};
Password::Password(void)
{
}
void Password::user(void)
{
	cout<<"Enter username"<<endl;
	cin>>owner;
}
void Password::set(void)
{
	s1 ="KazzyB";
	cout<<"enter password:"<<endl;
	cin.getline (pass,6);

if (strcmp (pass,s1) ==0)
{
	cout<<owner<<endl;
	cout<<"Welcome"<<endl;
}
else if (strcmp (pass,s1) !=0)
{
	cout<<owner<<endl;
	cout<<"Enter passwrd again"<<endl;
	cin>>pass;
}
else if (strcmp (pass,s1) !=0)
{
	cout<<owner<<endl;
	cout<<"Enter passwrd again"<<endl;
	cin>>pass;
}
else if (strcmp (pass,s1) !=0)
{
	cout<<owner<<endl;
	cout<<"Good bye"<<endl;
	"exit";
}
}
void main()
{
	Password y;
	y.user();
	y.set();
	
}

sl is a string class, and strcmp() works only on char*.

Use the string class for owner and pass and you can simply use == to do the compare.

Ok I have no errors now but on running, it only allows me to enter the password twice when it should be three times before exiting the program, also It is not comparing the entered pass with the stored one. even if I enter the right password I dont get the message welcome.

#include <iostream>
#include <string>

using namespace std;
class Password
{
	string s1;
	string owner, pass;
public:
	Password(void);
	void user(void);
	void set(void);
};
Password::Password(void)
{
}
void Password::user(void)
{
	cout<<"Enter username"<<endl;
	getline(cin,owner);
}
void Password::set(void)
{
	s1 ="KazzyB";
	cout<<"enter password:"<<endl;
	getline(cin,pass);

if (pass.compare(s1) == 0)
{
	cout<<owner<<endl;
	cout<<"Welcome"<<endl;
}
else if (pass.compare(s1) != 0)
{
	cout<<owner<<endl;
	cout<<"Enter password again"<<endl;
	getline(cin,pass);
}
else if (pass.compare(s1) != 0)
{
	cout<<owner<<endl;
	cout<<"Enter password again"<<endl;
	getline(cin,pass);
}
else if (pass.compare(s1) != 0)
{
	cout<<owner<<endl;
	cout<<"Good bye"<<endl;
	"exit";
}
}
void main()
{
	Password y;
	y.user();
	y.set();
	
}

a couple of things to note == can be used directly

so

if(s1 == pass)
{
}

more importantly
you are using

else if(pass != s1)
{
}

which only only happens (i.e. is checked), if none of the earlier conditions fire

so you meant:

if (pass.compare(s1) != 0)
{
	cout<<owner<<endl;
	cout<<"Enter password again"<<endl;
	getline(cin,pass);
}

if (pass.compare(s1) != 0)
{
	cout<<owner<<endl;
	cout<<"Enter password again"<<endl;
	getline(cin,pass);
}

if (pass.compare(s1) != 0)
{
	cout<<owner<<endl;
	cout<<"Good bye"<<endl;
	cout << "exit" << endl;
}

if (pass.compare(s1) == 0)
{
	cout<<owner<<endl;
	cout<<"Welcome"<<endl;
}

you might want to think about adding a private method

std::string get_password();

and putting this in a while loop to count the max number of tries
with break

a couple of things to note == can be used directly

so

if(s1 == pass)
{
}

more importantly
you are using

else if(pass != s1)
{
}

which only only happens (i.e. is checked), if none of the earlier conditions fire

so you meant:

if (pass.compare(s1) != 0)
{
	cout<<owner<<endl;
	cout<<"Enter password again"<<endl;
	getline(cin,pass);
}

if (pass.compare(s1) != 0)
{
	cout<<owner<<endl;
	cout<<"Enter password again"<<endl;
	getline(cin,pass);
}

if (pass.compare(s1) != 0)
{
	cout<<owner<<endl;
	cout<<"Good bye"<<endl;
	cout << "exit" << endl;
}

if (pass.compare(s1) == 0)
{
	cout<<owner<<endl;
	cout<<"Welcome"<<endl;
}

you might want to think about adding a private method

std::string get_password();

and putting this in a while loop to count the max number of tries
with break

I did that, but now it only displays the welcome message the second time even if I enter the correct password on the first try

Post the loop and or other pertinent code.

#include <iostream>
#include <string>

using namespace std;
class Password
{
	string s1;
	string owner, pass;
public:
	Password(void);
	void user(void);
	void set(void);
};
Password::Password(void)
{
}
void Password::user(void)
{
	cout<<"Enter username"<<endl;
	cin>>owner;
	s1 ="KazzyB";
}
void Password::set(void)
{
	

if (pass.compare(s1) != 0)
{
	cout<<"Enter password"<<endl;
	getline(cin,pass);
}

if (pass.compare(s1) != 0)
{
	cout<<owner<<endl;
	cout<<"Enter password again"<<endl;
	getline(cin,pass);
}
if (pass.compare(s1) != 0)
{
	cout<<owner<<endl;
	cout<<"Enter password again"<<endl;
	getline(cin,pass);
}
if (pass.compare(s1) != 0)
{
	cout<<owner<<endl;
	cout<<"Good bye"<<endl;
}
if (pass.compare(s1) == 0)
{
	cout<<owner<<endl;
	cout<<"Welcome"<<endl;
}
}
void main()
{
	Password y;
	y.user();
	y.set();
	
}

Your first check should n't happen as you always want to prompt for
password so
set should look more like this

and maybe should have a different function name
as you care actually checking password rather than setting s1

void Password::set()
{

cout<<"Enter password"<<endl;
getline(cin,pass);


if (pass != s1)
{
	cout<<owner<<endl;
	cout<<"Enter password again"<<endl;
	getline(cin,pass);
}
if (pass !=  s1)
{
	cout<<owner<<endl;
	cout<<"Enter password again"<<endl;
	getline(cin,pass);
}
if (pass  != s1)
{
	cout<<owner<<endl;
	cout<<"Good bye"<<endl;
 }
 if (pass == s1)
 {
	cout<<owner<<endl;
	cout<<"Welcome"<<endl;
 }
}

>>I did that, but now it only displays the welcome message the second time even if I enter the correct password on the first try

It's probably because input in C++ is buffered. That means that internally there is an input buffer that holds your input until the indicated input method puts the input information into the indicated variable. >> leaves the terminating whitespace char in the input buffer. getline() has the newline char, which is a whitespace char, as the default delimiting char. Therefore, since you call user(), which has a call to >> in it, before you call set() which is using getline(), the first call to getline() will see the newline char left in the input buffer as the first char to put in the variable. But since it is the terminating char, nothing is put in the variable. The second call to getline() is then probably putting the first "guess" in the variable and the second guess is still in the input buffer, though it looks like the second "guess" is the one that's put in the variable, because both the first and second "guesses" were the same. By the way, getline() does remove the terminating char from the input buffer, not that that has any bearing here.

Thank you all for taking time out and helping me.

#include <iostream>
#include <string>

using namespace std;
class Password
{
	string s1;
	string owner, pass;
public:
	Password(void);
	void user(void);
	void set(void);
};
Password::Password(void)
{
}
void Password::user(void)
{
	cout<<"Enter username"<<endl;
	cin>>owner;
}
void Password::set(void)
{

	cout<<"Enter Password"<<endl;
	cin>>pass;
if (pass == "KazzyB")
 {
	cout<<owner<<endl;
 }
if (pass != "KazzyB")
{
	cout<<owner<<endl;
	cout<<"Enter password again"<<endl;
	cin>>pass;
}
if (pass != "KazzyB")
{
	cout<<owner<<endl;
	cout<<"Enter password again"<<endl;
	cin>>pass;
}
if (pass  != "KazzyB")
{
	cout<<owner<<endl;
	cout<<"Good bye"<<endl;
}
else 
{
	cout<<"Welcome"<<endl;
}
}
void main()
{
	Password y;
	y.user();
	y.set();
	
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.