Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help! What's wrong with this program

Status
Not open for further replies.

joebeach

Programmer
Sep 12, 1999
13
0
0
US
#include &lt;iostream.h &gt;<br>#include &lt;iomanip.h &gt;<br>#include &lt;string.h &gt;<br>//#include &lt;ctype&gt;<br>#include &lt;ctype.h&gt;<br>#include &lt;stdlib.h&gt;<br>//using namespace std;<br>float calcAverage(float[], float);<br>class Password&nbsp;&nbsp;&nbsp;&nbsp;// class declaration<br>{<br>public:<br>Password() { bRetVal = 0;} <br>Password(char* szPassPhase){<br>length = strlen(szPassPhase);<br>szPassword = new char[length+1];<br>strcpy(szPassword , szPassPhase );<br>}<br>int check(void);<br>void setPassword(char*);<br>private:<br>int HasUpperCase(void);&nbsp;&nbsp;&nbsp;//check&nbsp;&nbsp;for password<br>int HasLowercase(void);<br>int HasDigit(void);<br>char* szPassword;<br>int length;<br>int bRetVal;<br>};<br><br>//implementation<br><br>void Password::setPassword(char* string) {<br>strcpy(szPassword,string);<br>cout &lt;&lt; szPassword &lt;&lt; endl;<br>}<br><br>int Password :: check(){<br><br>while(bRetVal != 0){<br>if( (HasLowercase())&nbsp;&nbsp;&&<br>(HasUpperCase())&nbsp;&nbsp;&&<br>(HasDigit())&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&&<br>(length &gt;= 6))<br><br>cout&lt;&lt;&quot;Your password is correct. Please process &quot;&lt;&lt;endl;<br>break;<br>}<br>if(!bRetVal) {<br>cout&lt;&lt; &quot;Your password is incorrect.&nbsp;&nbsp;exit and try again. &quot;&lt;&lt;endl<br> &lt;&lt; &quot;Your password should be at least six characters long, &quot;&lt;&lt;endl<br> &lt;&lt; &quot;and the password should contain at least one uppercase and at least one lower case letter. &quot;&lt;&lt;endl<br> &lt;&lt; &quot;An example of the password is B12c34 &quot;&lt;&lt;endl;<br>exit(0);<br>}<br>&nbsp;&nbsp;return bRetVal;<br><br>}<br><br>int Password::HasLowercase(){<br>&nbsp;bRetVal =0;<br>&nbsp;for(int x=0;x &lt; length; x++)<br>if(islower((int)szPassword[x]))&nbsp;&nbsp;//return a non-zero value if true<br>{<br>bRetVal = 1;<br>break;<br>}<br>&nbsp;return bRetVal;<br>}<br><br>int Password::HasUpperCase() {<br>bRetVal = 0;<br>for(int x=0; x&lt;length;x++)//returns a no-zero<br>if(isupper((int)szPassword[x])){<br>bRetVal = 1;<br>break;<br>}<br>return bRetVal;<br>}<br><br>int Password::HasDigit() {<br>bRetVal =0;<br>for (int x=0;x&lt;=length;x++)<br>if(isdigit((int)szPassword[x]))&nbsp;&nbsp;{<br>bRetVal = 1;<br>break;<br>}<br>return bRetVal;<br>}<br><br>struct Student<br>{<br>&nbsp;&nbsp;char name[20];<br>&nbsp;&nbsp;char idnum[10];<br>&nbsp;&nbsp;float testGrade[];<br>};<br>void populate(Student *);<br>// calcAverage(Student *);<br>//void display (Student *);<br><br><br><br><br>int main()<br>{<br>char* string =&quot;B12c34 &quot;;<br>Password a = Password(string) ;<br>cout&lt;&lt;&quot;Type in your password: &quot;;<br>cin.getline(string, 10);<br>cout&lt;&lt;&quot;the password is: &quot; &lt;&lt;string&lt;&lt; endl;<br>a.check();<br>a.setPassword(string);<br><br>Student stud&nbsp;&nbsp;&nbsp;;<br><br>int i=0 , ch=0;<br>float testGrade[]= {0.00};<br>float average, num=0.00;<br>cout&lt;&lt; &quot;\nEnter a grade or -1 to terminate the program &quot;;<br>cin&gt;&gt;testGrade<i>;<br>while(stud.testGrade<i> &gt;= 0.00 && i &lt; ch)<br>{<br> i++;<br> cout&lt;&lt;&quot;Enter a grade or -1 to terminate data entry: &quot;;<br> cin &gt;&gt; stud.testGrade<i>;<br>}<br>ch =&nbsp;&nbsp;i;<br><br>if(ch &gt;0);<br>{<br>&nbsp;&nbsp;average = calcAverage(testGrade, num ) ;<br>&nbsp;&nbsp;cout &lt;&lt;&quot;The average of the grades is &quot;&lt;&lt; average&lt;&lt;endl;<br>&nbsp;// display(average);<br>}<br>return 0;<br>}<br><br>void populate(Student *record)<br>{<br>&nbsp;&nbsp;cout&lt;&lt; &quot;\nEnter student information: &quot;;<br>&nbsp;&nbsp;cout&lt;&lt; &quot;\nStudent's name &quot;;<br>&nbsp;&nbsp;cin.getline(record-&gt;name, 10);<br>&nbsp;&nbsp;cout&lt;&lt; &quot;\nEnter the student's id numbner: &quot;;<br>&nbsp;&nbsp;cin&gt;&gt;record-&gt;idnum;<br>&nbsp;&nbsp;cout&lt;&lt; &quot;\nEnter The test grade &quot;;<br>&nbsp;&nbsp;cin&gt;&gt;record-&gt;testGrade[10];<br><br>&nbsp;&nbsp;return;<br>}<br><br>&nbsp;float calcAverage(float testGrade[], float )<br>{<br> int&nbsp;&nbsp;ch, i=0;<br><br> float total =0.00, average;<br> cout&lt;&lt;&quot;List all the grades for this sutdent: &quot;;<br> cin&gt;&gt;testGrade<i>;<br> for(i=0; i&lt; ch; i++)<br> {<br> cout&lt;&lt; &quot;List all the grades of this student: &quot;;<br> cin&gt;&gt;testGrade<i>;<br><br> //calculate the sum of the grades<br> total = total + testGrade<i>;<br> }<br> average = total /ch&nbsp;&nbsp;;<br> return average;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//calculate and return average<br><br>}<br><br><br><br><br>
 
joebeach,<br><br>&nbsp;&nbsp;&nbsp;&nbsp;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?<br><br>&nbsp;&nbsp;&nbsp;&nbsp;We will be glad to help you but we need the specifics. Most of us are <b>very</b> busy as I expect you are. We need to know where to spend our time and resources on this problem.<br><br> <p>James P. Cottingham<br><a href=mailto:main@ivcusa.com>main@ivcusa.com</a><br><a href= Veneer Co., Inc.</a><br>All opinions are mine alone and do not necessarily reflect those of my employer.
 
I too agree with&nbsp;&nbsp;2ffat's&nbsp;&nbsp;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.<br>Thanx<br>Siddhartha singh<br><A HREF="mailto:ssingh@aztecsoft.com">ssingh@aztecsoft.com</A>
 
joebeach, <br><br>&nbsp;&nbsp;&nbsp;&nbsp;<i>I am posting your e-mail here to further the speed of your answer</i>.<br><br>2ffat,<br>Thank you very much.&nbsp;&nbsp;I tried to send you a mail earlier, but there was a <br>fatal error in my system.&nbsp;&nbsp;I am sending this again.&nbsp;&nbsp;If you receive the <br>first please disregard this one.<br><br>Here is the breakdown of what I think:<br><br>Thank you for answering my desperate screaming for help.<br><br>The problem I am working on is&nbsp;&nbsp;a general student grading problem.&nbsp;&nbsp;I am<br>supposed to write a program so the user can get in with the right password.<br><br>1.) The password should be at lease six characters long.<br>2.) The password&nbsp;&nbsp;should contain at least one uppercase and one lover case<br>letter.<br>3.) The password should have at lease one numeric digit.<br><br><br>After access is approved, the user can by typing the student's name, ID<br>number, and his test grade, and the program should calculate the average of<br>all the grades of the student.&nbsp;&nbsp;&nbsp;For example:<br><br>name:&nbsp;&nbsp;&nbsp;John Smith<br>ID number: 20510<br>grades:&nbsp;&nbsp;99, 78, 90, 99.<br>average of grades:&nbsp;&nbsp;91.5 .<br>student's letter grade is an 'A'.<br><br>Few problem I have is:<br><br>1.)&nbsp;&nbsp;bRetVal doesn't always work.&nbsp;&nbsp;It will work sometime, and many time I<br>will type<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;anything and it will said ok.<br>2.) When I tried to list the grade of the students, any number I put in will<br>be the<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end of the program.&nbsp;&nbsp;This program will&nbsp;&nbsp;compile.<br>3.) It will not show anything in the populate student section.<br>4.) for loop will not loop, so a count can be obtained to calculate the<br>average of<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the grade.&nbsp;&nbsp;I cannot set a fixed loop count because some<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;student might have only 3 grades, and some have 6 grades to compute.<br><br>Can you help?<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<i>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&nbsp;&nbsp;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. <font color=red>If</font> I have time this weekend, I will do the same (no promises, though). We can compare our results on Monday.</i><br><br> <p>James P. Cottingham<br><a href=mailto: > </a><br><a href= Veneer Co., Inc.</a><br>All opinions are mine alone and do not necessarily reflect those of my employer.
 
joebeach,<br><br>&nbsp;&nbsp;&nbsp;&nbsp;I broke you program into two parts. The first tests the <i>Password</i><br>class. From your specs, any password that has at least one upper case, one<br>lower case, one number, and is at least 6 characters long, will pass the test.<br>For example, C12345j, CcCc8700, etc. Passwords like 123456, AAAAAAA, cccccc,<br>aB3, will <b>not</b> pass.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;I have included the program I compiled and tested.<br><br>#include &lt;iostream.h &gt;<br>#include &lt;iomanip.h &gt;<br>#include &lt;string.h &gt;<br>//#include &lt;ctype&gt;<br>#include &lt;ctype.h&gt;<br>#include &lt;stdlib.h&gt;<br>//using namespace std;<br><br>class Password&nbsp;&nbsp;&nbsp;&nbsp;// class declaration<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;public:<br>&nbsp;&nbsp;&nbsp;&nbsp;Password() { bRetVal = 0;}<br>&nbsp;&nbsp;&nbsp;&nbsp;Password(char* szPassPhase)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;length = strlen(szPassPhase);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;szPassword = new char[length+1];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(szPassword , szPassPhase );<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;int check(void);<br>&nbsp;&nbsp;&nbsp;&nbsp;void setPassword(char*);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;private:<br>&nbsp;&nbsp;&nbsp;&nbsp;int HasUpperCase(void);&nbsp;&nbsp;&nbsp;//check&nbsp;&nbsp;for password<br>&nbsp;&nbsp;&nbsp;&nbsp;int HasLowercase(void);<br>&nbsp;&nbsp;&nbsp;&nbsp;int HasDigit(void);<br>&nbsp;&nbsp;&nbsp;&nbsp;char* szPassword;<br>&nbsp;&nbsp;&nbsp;&nbsp;int length;<br>&nbsp;&nbsp;&nbsp;&nbsp;int bRetVal;<br>};<br><br>//implementation<br><br>void Password::setPassword(char* string)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;strcpy(szPassword,string);<br>&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; szPassword &lt;&lt; endl;<br>}<br><br>int Password :: check()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(bRetVal != 0)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((HasLowercase()) && (HasUpperCase()) && (HasDigit()) && (length<br>&gt;= 6))<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// braces added<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;Your password is correct. Please process &quot;&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;if(!bRetVal)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// IMHO, when using endl, always put a ; afterwards.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// endl flushes the buffer and sometimes putting &lt;&lt; after endl<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// without cout MAY cause problems.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; &quot;Your password is incorrect.&nbsp;&nbsp;exit and try again.<br>&quot;&lt;&lt; endl; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; &quot;Your password should be at least six characters<br>long, &quot;&lt;&lt; endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; &quot;and the password should contain at least<br>one uppercase and at least one lower case letter. &quot; &lt;&lt; endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; &quot;An example of the password is B12c34 &quot; &lt;&lt; endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(0);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;return bRetVal;<br>}<br><br>int Password::HasLowercase()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;bRetVal =0;<br>&nbsp;&nbsp;&nbsp;&nbsp;for(int x=0;x &lt; length; x++)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// added braces<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(islower((int)szPassword[x]))&nbsp;&nbsp;//return a non-zero value if true<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bRetVal = 1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;return bRetVal;<br>}<br><br>int Password::HasUpperCase()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;bRetVal = 0;<br>&nbsp;&nbsp;&nbsp;&nbsp;for(int x=0; x&lt;length;x++) //returns a no-zero<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// added braces<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(isupper((int)szPassword[x]))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bRetVal = 1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;return bRetVal;<br>}<br><br>int Password::HasDigit()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;bRetVal =0;<br>&nbsp;&nbsp;&nbsp;&nbsp;for (int x=0;x&lt;=length;x++)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Added braces<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(isdigit((int)szPassword[x]))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bRetVal = 1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;return bRetVal;<br>}<br><br>int main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;/* This was yours<br>&nbsp;&nbsp;&nbsp;&nbsp;char* string =&quot;B12c34 &quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;Password a = Password(string);<br>&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; &quot;Type in your password: &quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;cin.getline(string, 10);<br>&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; &quot;the password is: &quot; &lt;&lt; string &lt;&lt; endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;a.check();<br>&nbsp;&nbsp;&nbsp;&nbsp;a.setPassword(string);<br>&nbsp;&nbsp;&nbsp;&nbsp;*/<br><br>&nbsp;&nbsp;&nbsp;&nbsp;// This works better<br>&nbsp;&nbsp;&nbsp;&nbsp;char strPW[11];<br>&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; &quot;Type in your password: &quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;cin.getline(strPW, 10);<br>&nbsp;&nbsp;&nbsp;&nbsp;Password a = Password(strPW);<br>&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; &quot;the password is: &quot; &lt;&lt; strPW &lt;&lt; endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;a.check();<br>&nbsp;&nbsp;&nbsp;&nbsp;a.setPassword(strPW);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br>}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;I cleaned the code up a bit and added braces where I thought they should<br>be. <i>Advice:</i> always make your code as readable as possible. This will<br>help others that have to read your code, it will help you when you have to<br>read your own code later, and it will help to debug your code.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Look at <i>main</i>. You are setting up a character string with <i>char*<br>string = &quot;B12c34 &quot;;</i>. Next you are setting it up as the password. When you<br>run <i>a.check</i>, it is checking the original string, <b>not</b> the newly<br>inputted string. Also, avoid using <u>string</u> as a variable since this is a<br>reserved word in STL.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;As far as your grade code, you must either define the size of arrays<br>at compile time, or you must allocate memory for it dynamically using<br><i>new</i>.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;This should get you started.<br><br><br> <p>James P. Cottingham<br><a href=mailto: > </a><br><a href= Veneer Co., Inc.</a><br>All opinions are mine alone and do not necessarily reflect those of my employer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top