Saturday, May 22, 2010

I need help fixing my C++ function program!!?

string lab10identification(){


string labId = "lab 10 Jkim jhk0042@unt.edu";


return labId;


}


char getLetterGrade(int gradeIn) {


if (gradeIn %26gt;= 90 %26amp;%26amp; gradeIn %26lt;= 100)


return 'A';


if (gradeIn %26gt;= 80 %26amp;%26amp; gradeIn %26lt;= 90)


return 'B';


if (gradeIn %26gt;= 70 %26amp;%26amp; gradeIn %26lt;= 80)


return 'C';


if (gradeIn %26gt;= 60 %26amp;%26amp; gradeIn %26lt;= 70)


return 'D';


if (gradeIn %26gt;= 0 %26amp;%26amp; gradeIn %26lt;= 59)


return 'F';


}


int getNumericGrade(){


int grade;


cout %26lt;%26lt; "Enter a grade(-1 to 100, -1 to stop): ";


cin %26gt;%26gt; grade;


return grade;


}


int main() {


int grade;


char letterGrade;


string labId;





lab10identification();





do {


grade = getNumericGrade();


if (grade != -1) {


letterGrade = getLetterGrade(grade);


cout %26lt;%26lt; "The student's letter grade is: " %26lt;%26lt; letterGrade %26lt;%26lt; endl;


}


else if(grade %26lt; -1 %26amp;%26amp; grade %26gt; 100)


cout %26lt;%26lt; "The number is out of range. Try again" %26lt;%26lt; endl;


}


while (grade != -1);


cout %26lt;%26lt; "Good-bye" %26lt;%26lt; endl;

I need help fixing my C++ function program!!?
You need to swap the conditions in your if %26amp; else if statements.


Also, it should be (grade %26lt; -1 || grade %26gt; 100). With %26amp;%26amp; it will never work.
Reply:ok look, the return statement is NOT to print to the screen


if you wanna print from that function you have to do something like





string lab10identification(){


string labId = "lab 10 Jkim jhk0042@unt.edu";


cout %26lt;%26lt; labld;


return labId;


}





and the reason why your check for a wrong number is not working is because when you do this...





else if(grade %26lt; -1 %26amp;%26amp; grade %26gt; 100)





the value of grade can NEVER be LESS than -1 AND greater than 100


you need the OR ( || ) operator instead of AND


because by using the and operator it goes back to the do-segment ..since the value is -6...and it gets into the if( number!= -1) and it prints for a number...รบ


No comments:

Post a Comment