Program Help

  • Thread starter Thread starter gonzal51
  • Start date Start date
G

gonzal51

Hi, I'm a student learning C++ and my professor isn't much help, her view is
if you dont get it I ain't helping you....

We are supposed to do a basic program as a logon system. I got as far as I
could and came up with some errors I could not understand.

Any Help would be appreciated.


Using: MS Visual Studio .Net 2003
Language: C++
Program centers around: Classes


**CODE**
/*
Name: Matthew Gonzalez
Prog Name: gonzalez_PA10
Date: 4/27/05
Purpose: Classes
*/

#include <iostream>
using namespace std;

//class
class User
{
private:

int userId;
char firstName[20];
char lastName[20];
char password[20];
public:

void setUserData(char firstName[20],char lastName[20],char password[20]);
void changePassword (char password[20],char newPass[20]);
void displayUser(int userId,char firstName[20],char lastName[20], char
password[20]);
};
//end class
User::setUserData(char firstName[20],char lastName[20],char password[20])
{
cout<<"Please Enter Your First Name: "<<endl;
cin>>firstName[20];
cout<<"Please Enter your Last Name: "<<endl;
cin>>lastName[20];
cout<<"Please Enter your Password: "<<endl;
cin>>password[20];
//end setuserdatafunction
}
User::changePassword(char password[20],char newPass[20])
{
cout<<"Please enter new password"<<endl;
cin>>newPass[20];

if(password[20]==newPass[20])
{
cout<<"Your password is the same. No Change will occur."<<endl;
}
else
{
if(password[20]!=newPass[20])

newPass[20]=password[20];
}
}
User::displayUser(int userId,char firstName[20],char lastName[20], char
password[20])
{
cout<<"The User id is: "<<userId<<endl;
cout<<"The First name is: "<<firstName[20]<<endl;
cout<<"The Last name is: "<<lastName[20]<<endl;
cout<<"The password is: "<<password[20]<<endl;
//end display user function
}

int main()
{
//user object
User logon;
int userId;
char firstName[20];
char lastName[20];
char password[20];
char newPass[20];
char answer;

cout<<"Please enter a user Id: "<<endl;
cin>>userId;
logon.setUserData(char firstName[20],char lastName[20],char password[20]);
cout<<"Do you want to change your password? Enter Y or N: "<<endl;
cin>>answer;
//if
if(answer=='y')
{
logon.changePassword(char password[20],char newPass[20]);
logon.displayUser(int UserId,char firstName[20],char lastName[20],char
password[20]);
}
else

if(answer=='n')
{
logon.displayUser(int UserId,char firstName[20],char lastName[20],char
password[20]);
}
//endif

return 0;
}



**END CODE**
 
gonzal51 said:
Hi, I'm a student learning C++ and my professor isn't much help, her view is
if you dont get it I ain't helping you....

We are supposed to do a basic program as a logon system. I got as far as I
could and came up with some errors I could not understand.

Any Help would be appreciated.


Using: MS Visual Studio .Net 2003
Language: C++
Program centers around: Classes


**CODE**
/*
Name: Matthew Gonzalez
Prog Name: gonzalez_PA10
Date: 4/27/05
Purpose: Classes
*/

#include <iostream>
using namespace std;

//class
class User
{
private:

int userId;
char firstName[20];
char lastName[20];
char password[20];


Using std::string instead of char arrays would be better.

public:

void setUserData(char firstName[20],char lastName[20],char password[20]);
void changePassword (char password[20],char newPass[20]);
void displayUser(int userId,char firstName[20],char lastName[20], char
password[20]);
};
//end class

void User::setUserData(char firstName[20],char lastName[20],char password[20])
{
cout<<"Please Enter Your First Name: "<<endl;
cin>>firstName[20];
cout<<"Please Enter your Last Name: "<<endl;
cin>>lastName[20];
cout<<"Please Enter your Password: "<<endl;
cin>>password[20];
//end setuserdatafunction
}

void User::changePassword(char password[20],char newPass[20])
{
cout<<"Please enter new password"<<endl;
cin>>newPass[20];

if(password[20]==newPass[20])
{
cout<<"Your password is the same. No Change will occur."<<endl;
}
else
{
if(password[20]!=newPass[20])

newPass[20]=password[20];
}
}

void User::displayUser(int userId,char firstName[20],char lastName[20], char
password[20])
{
cout<<"The User id is: "<<userId<<endl;
cout<<"The First name is: "<<firstName[20]<<endl;
cout<<"The Last name is: "<<lastName[20]<<endl;
cout<<"The password is: "<<password[20]<<endl;
//end display user function
}

int main()
{
//user object
User logon;
int userId;
char firstName[20];
char lastName[20];
char password[20];
char newPass[20];
char answer;

cout<<"Please enter a user Id: "<<endl;
cin>>userId;

logon.setUserData(firstName, lastName, password);
cout<<"Do you want to change your password? Enter Y or N: "<<endl;
cin>>answer;
//if
if(answer=='y')
{

logon.changePassword(password, newPass);
logon.displayUser(userId, firstName, lastName, password);

}
else

if(answer=='n')
{

logon.displayUser(userId, firstName, lastName, password);
}
//endif

return 0;
}


Just fixed it to compile. I did not check or even run the code.
 
I was wondering, (while I work on it) what were the problems I was having?


Ioannis Vranos said:
gonzal51 said:
Hi, I'm a student learning C++ and my professor isn't much help, her view is
if you dont get it I ain't helping you....

We are supposed to do a basic program as a logon system. I got as far as I
could and came up with some errors I could not understand.

Any Help would be appreciated.


Using: MS Visual Studio .Net 2003
Language: C++
Program centers around: Classes


**CODE**
/*
Name: Matthew Gonzalez
Prog Name: gonzalez_PA10
Date: 4/27/05
Purpose: Classes
*/

#include <iostream>
using namespace std;

//class
class User
{
private:

int userId;
char firstName[20];
char lastName[20];
char password[20];


Using std::string instead of char arrays would be better.

public:

void setUserData(char firstName[20],char lastName[20],char password[20]);
void changePassword (char password[20],char newPass[20]);
void displayUser(int userId,char firstName[20],char lastName[20], char
password[20]);
};
//end class

void User::setUserData(char firstName[20],char lastName[20],char password[20])
{
cout<<"Please Enter Your First Name: "<<endl;
cin>>firstName[20];
cout<<"Please Enter your Last Name: "<<endl;
cin>>lastName[20];
cout<<"Please Enter your Password: "<<endl;
cin>>password[20];
//end setuserdatafunction
}

void User::changePassword(char password[20],char newPass[20])
{
cout<<"Please enter new password"<<endl;
cin>>newPass[20];

if(password[20]==newPass[20])
{
cout<<"Your password is the same. No Change will occur."<<endl;
}
else
{
if(password[20]!=newPass[20])

newPass[20]=password[20];
}
}

void User::displayUser(int userId,char firstName[20],char lastName[20], char
password[20])
{
cout<<"The User id is: "<<userId<<endl;
cout<<"The First name is: "<<firstName[20]<<endl;
cout<<"The Last name is: "<<lastName[20]<<endl;
cout<<"The password is: "<<password[20]<<endl;
//end display user function
}

int main()
{
//user object
User logon;
int userId;
char firstName[20];
char lastName[20];
char password[20];
char newPass[20];
char answer;

cout<<"Please enter a user Id: "<<endl;
cin>>userId;

logon.setUserData(firstName, lastName, password);
cout<<"Do you want to change your password? Enter Y or N: "<<endl;
cin>>answer;
//if
if(answer=='y')
{

logon.changePassword(password, newPass);
logon.displayUser(userId, firstName, lastName, password);

}
else

if(answer=='n')
{

logon.displayUser(userId, firstName, lastName, password);
}
//endif

return 0;
}


Just fixed it to compile. I did not check or even run the code.
 
gonzal51 said:
I was wondering, (while I work on it) what were the problems I was having?
void User::setUserData(char firstName[20],char lastName[20],char

When you omit return type (void in this case) the default type is int,
so the compiler expects that you actually return something (return 0).
Since you don't return anything compiler calls it an error.

When you call a function you put arguments in argument list. But "char
firstName[20]" is not an argument. "firstName" is an argument. To the
compiler "char firstName[20]" looks like a declaration, but the whole
line doesn't look like anything recognizable, that's why it's confused
and reports an error.
 
Mihajlo said:
When you omit return type (void in this case) the default type is int,
so the compiler expects that you actually return something (return 0).
Since you don't return anything compiler calls it an error.


I think you are probably talking about pre-standard C++. By omitting the return type, no
int is implied, it is an error. :-)
 
Ioannis said:
I think you are probably talking about pre-standard C++. By omitting the
return type, no int is implied, it is an error. :-)

Alas, VC71 assumes int and gives only warnings, C4183 for member
functions and C4508 for global functions, but it wouldn't be an error to
treat is as such :-)
 
Mihajlo said:
Alas, VC71 assumes int and gives only warnings, C4183 for member
functions and C4508 for global functions, but it wouldn't be an error to
treat is as such :-)


Let's use some simple code:


class SomeClass
{
public:
void somefunc();
}

SomeClass::somefunc() {}


int main()
{
}



Do you get such warnings for this?


For this

class SomeClass
{
public:
somefunc();
}

SomeClass::somefunc() {}


int main()
{
}

I get such a warning, but it is followed by bizarre errors and does not compile either. :-)
 
Ioannis said:
For this

class SomeClass
{
public:
somefunc(); };

SomeClass::somefunc() {}


int main()
{
}

I get such a warning, but it is followed by bizarre errors and does not
compile either. :-)


I had omitted the ';' in the class definition. Yes you are right, it gives such a warning
and compiles. However this is not standard C++ behaviour, you may consider it as a system
extension (which probably would be better to be fixed sooner or later). :-)
 
I meant, while I load it into my visual.NET and physically look at it as
well as changes you've made (thanks!). Although I found that while it
compiles, I can enter an userId, and FirstName, but soon after it shows the
other outputs, i'm guessing its a problem with my SetUserData function. Such
is what I meant by working on it. :-)
 
Thanks for the info. I appreciate it.

"> >> logon.setUserData(firstName, lastName, password);
 
Back
Top