×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Help! What's wrong with this program

Help! What's wrong with this program

Help! What's wrong with this program

(OP)
#include <iostream.h >
#include <iomanip.h >
#include <string.h >
//#include <ctype>
#include <ctype.h>
#include <stdlib.h>
//using namespace std;
float calcAverage(float[], float);
class Password    // class declaration
{
public:
Password() { bRetVal = 0;}
Password(char* szPassPhase){
length = strlen(szPassPhase);
szPassword = new char[length+1];
strcpy(szPassword , szPassPhase );
}
int check(void);
void setPassword(char*);
private:
int HasUpperCase(void);   //check  for password
int HasLowercase(void);
int HasDigit(void);
char* szPassword;
int length;
int bRetVal;
};

//implementation

void Password::setPassword(char* string) {
strcpy(szPassword,string);
cout << szPassword << endl;
}

int Password :: check(){

while(bRetVal != 0){
if( (HasLowercase())  &&
(HasUpperCase())  &&
(HasDigit())      &&
(length >= 6))

cout<<"Your password is correct. Please process "<<endl;
break;
}
if(!bRetVal) {
cout<< "Your password is incorrect.  exit and try again. "<<endl
<< "Your password should be at least six characters long, "<<endl
<< "and the password should contain at least one uppercase and at least one lower case letter. "<<endl
<< "An example of the password is B12c34 "<<endl;
exit(0);
}
  return bRetVal;

}

int Password::HasLowercase(){
 bRetVal =0;
 for(int x=0;x < length; x++)
if(islower((int)szPassword[x]))  //return a non-zero value if true
{
bRetVal = 1;
break;
}
 return bRetVal;
}

int Password::HasUpperCase() {
bRetVal = 0;
for(int x=0; x<length;x++)//returns a no-zero
if(isupper((int)szPassword[x])){
bRetVal = 1;
break;
}
return bRetVal;
}

int Password::HasDigit() {
bRetVal =0;
for (int x=0;x<=length;x++)
if(isdigit((int)szPassword[x]))  {
bRetVal = 1;
break;
}
return bRetVal;
}

struct Student
{
  char name[20];
  char idnum[10];
  float testGrade[];
};
void populate(Student *);
// calcAverage(Student *);
//void display (Student *);




int main()
{
char* string ="B12c34 ";
Password a = Password(string) ;
cout<<"Type in your password: ";
cin.getline(string, 10);
cout<<"the password is: " <<string<< endl;
a.check();
a.setPassword(string);

Student stud   ;

int i=0 , ch=0;
float testGrade[]= {0.00};
float average, num=0.00;
cout<< "\nEnter a grade or -1 to terminate the program ";
cin>>testGrade;
while(stud.testGrade >= 0.00 && i < ch)
{
i++;
cout<<"Enter a grade or -1 to terminate data entry: ";
cin >> stud.testGrade;
}
ch =  i;

if(ch >0);
{
  average = calcAverage(testGrade, num ) ;
  cout <<"The average of the grades is "<< average<<endl;
 // display(average);
}
return 0;
}

void populate(Student *record)
{
  cout<< "\nEnter student information: ";
  cout<< "\nStudent's name ";
  cin.getline(record->name, 10);
  cout<< "\nEnter the student's id numbner: ";
  cin>>record->idnum;
  cout<< "\nEnter The test grade ";
  cin>>record->testGrade[10];

  return;
}

 float calcAverage(float testGrade[], float )
{
int  ch, i=0;

float total =0.00, average;
cout<<"List all the grades for this sutdent: ";
cin>>testGrade;
for(i=0; i< ch; i++)
{
cout<< "List all the grades of this student: ";
cin>>testGrade;

//calculate the sum of the grades
total = total + testGrade;
}
average = total /ch  ;
return average;                  //calculate and return average

}




RE: Help! What's wrong with this program

joebeach,

    Where exactly are you running into problems? Are you getting some error messages, and if so, what are they? What are you expecting your output to look like, and what are you really getting?

    We will be glad to help you but we need the specifics. Most of us are very busy as I expect you are. We need to know where to spend our time and resources on this problem.

James P. Cottingham
main@ivcusa.com
International Veneer Co., Inc.
All opinions are mine alone and do not necessarily reflect those of my employer.

RE: Help! What's wrong with this program

I too agree with  2ffat's  suggestion. we are too busy. So please be specific. Try to debug your program with small independent functions. If you find some error at some specific function. Then specify that error. Although I had given you the previous answer. Please get back with some specific error.
Thanx
Siddhartha singh
ssingh@aztecsoft.com

RE: Help! What's wrong with this program

joebeach,

    I am posting your e-mail here to further the speed of your answer.

2ffat,
Thank you very much.  I tried to send you a mail earlier, but there was a
fatal error in my system.  I am sending this again.  If you receive the
first please disregard this one.

Here is the breakdown of what I think:

Thank you for answering my desperate screaming for help.

The problem I am working on is  a general student grading problem.  I am
supposed to write a program so the user can get in with the right password.

1.) The password should be at lease six characters long.
2.) The password  should contain at least one uppercase and one lover case
letter.
3.) The password should have at lease one numeric digit.


After access is approved, the user can by typing the student's name, ID
number, and his test grade, and the program should calculate the average of
all the grades of the student.   For example:

name:   John Smith
ID number: 20510
grades:  99, 78, 90, 99.
average of grades:  91.5 .
student's letter grade is an 'A'.

Few problem I have is:

1.)  bRetVal doesn't always work.  It will work sometime, and many time I
will type
      anything and it will said ok.
2.) When I tried to list the grade of the students, any number I put in will
be the
     end of the program.  This program will  compile.
3.) It will not show anything in the populate student section.
4.) for loop will not loop, so a count can be obtained to calculate the
average of
     the grade.  I cannot set a fixed loop count because some
     student might have only 3 grades, and some have 6 grades to compute.

Can you help?

    Let's start with ssingh's suggestion. Break out one of the functions or classes that returns bRetVal. Put it into a new program that only sends and receives data to/from that function/class. Once that function or class is working, move on to the next  one. Keep going until you have tested and debugged each function and class seperately. Now put the corrected functions and classes back into the program and test the program. IMHO, a properly designed program should be modular enough to allow you to do this. If I have time this weekend, I will do the same (no promises, though). We can compare our results on Monday.

James P. Cottingham

International Veneer Co., Inc.
All opinions are mine alone and do not necessarily reflect those of my employer.

RE: Help! What's wrong with this program

joebeach,

    I broke you program into two parts. The first tests the Password
class. From your specs, any password that has at least one upper case, one
lower case, one number, and is at least 6 characters long, will pass the test.
For example, C12345j, CcCc8700, etc. Passwords like 123456, AAAAAAA, cccccc,
aB3, will not pass.

    I have included the program I compiled and tested.

#include <iostream.h >
#include <iomanip.h >
#include <string.h >
//#include <ctype>
#include <ctype.h>
#include <stdlib.h>
//using namespace std;

class Password    // class declaration
{
    public:
    Password() { bRetVal = 0;}
    Password(char* szPassPhase)
    {
        length = strlen(szPassPhase);
        szPassword = new char[length+1];
        strcpy(szPassword , szPassPhase );
    }

    int check(void);
    void setPassword(char*);

    private:
    int HasUpperCase(void);   //check  for password
    int HasLowercase(void);
    int HasDigit(void);
    char* szPassword;
    int length;
    int bRetVal;
};

//implementation

void Password::setPassword(char* string)
{
    strcpy(szPassword,string);
    cout << szPassword << endl;
}

int Password :: check()
{
     while(bRetVal != 0)
    {
        if((HasLowercase()) && (HasUpperCase()) && (HasDigit()) && (length
>= 6))
    {
        // braces added
        cout<<"Your password is correct. Please process "<<endl;
        break;
    }

    if(!bRetVal)
    {
        // IMHO, when using endl, always put a ; afterwards.
        // endl flushes the buffer and sometimes putting << after endl
        // without cout MAY cause problems.
        cout << "Your password is incorrect.  exit and try again.
"<< endl;         cout << "Your password should be at least six characters
long, "<< endl;
        cout << "and the password should contain at least
one uppercase and at least one lower case letter. " << endl;
        cout << "An example of the password is B12c34 " << endl;
        exit(0);
    }
    return bRetVal;
}

int Password::HasLowercase()
{
    bRetVal =0;
    for(int x=0;x < length; x++)
    {
        // added braces
        if(islower((int)szPassword[x]))  //return a non-zero value if true
        {
            bRetVal = 1;
            break;
        }
    }
    return bRetVal;
}

int Password::HasUpperCase()
{
    bRetVal = 0;
    for(int x=0; x<length;x++) //returns a no-zero
    {
        // added braces
        if(isupper((int)szPassword[x]))
        {
            bRetVal = 1;
            break;
        }
    }
    return bRetVal;
}

int Password::HasDigit()
{
    bRetVal =0;
    for (int x=0;x<=length;x++)
    {
        // Added braces
        if(isdigit((int)szPassword[x]))
        {
            bRetVal = 1;
            break;
        }
    }
    return bRetVal;
}

int main()
{
    /* This was yours
    char* string ="B12c34 ";
    Password a = Password(string);
    cout << "Type in your password: ";
    cin.getline(string, 10);
    cout << "the password is: " << string << endl;
    a.check();
    a.setPassword(string);
    */

    // This works better
    char strPW[11];
    cout << "Type in your password: ";
    cin.getline(strPW, 10);
    Password a = Password(strPW);
    cout << "the password is: " << strPW << endl;
    a.check();
    a.setPassword(strPW);

    return 0;
}

    I cleaned the code up a bit and added braces where I thought they should
be. Advice: always make your code as readable as possible. This will
help others that have to read your code, it will help you when you have to
read your own code later, and it will help to debug your code.

    Look at main. You are setting up a character string with char*
string = "B12c34 ";
. Next you are setting it up as the password. When you
run a.check, it is checking the original string, not the newly
inputted string. Also, avoid using string as a variable since this is a
reserved word in STL.

    As far as your grade code, you must either define the size of arrays
at compile time, or you must allocate memory for it dynamically using
new.

    This should get you started.


James P. Cottingham

International Veneer Co., Inc.
All opinions are mine alone and do not necessarily reflect those of my employer.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close