C++ Programing

ap1096
ap1096 used Ask the Experts™
on
I have written the following program.  


#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>


//prototypes
void showClassAverage(int count, double total);
void getgrades (double &grades);
void averaging(int &count, double grades, double &total);
char clettergrade(double grades, char &lettergrade);

double grades=0;   //the grade the uses inputs
double total=0;   //all the grades added together
int count=0;     //how many grades are entered
char lettergrade;

int main()
{

   while (1)
   {
      getgrades (grades);
     
      if (grades >= 0 )
      {
         averaging (count, grades, total);
      }

       else
       {
          break;
       }
   }

   showClassAverage(count, total);

                                   
   system("PAUSE");
   return 0;
}

void showClassAverage(int count, double total)
//caculates final class average
{  
      if(count > 0)
      {
         cout << endl;
         cout << "Class average of " << count << " grades = "
              << setiosflags(ios::fixed) << setw(6) << setprecision(2)
              << ((total)/(count)) << endl;
      }
        return;
}

char clettergrade(double grades, char &lettergrade)
//uses pa3 to assign letter grade
{
   double average = grades * 10 + .5;


   if (895 <= average)
   {
     lettergrade = "A";

   }
   else if (795 <= average)
   {
     lettergrade = "B";

   }
   else if (695 <= average)
   {
     lettergrade = "C";

   }
   else if (595 <= average)
   {
     lettergrade = "D";

   }
   else if (595 > average)
   {
     lettergrade = "F";

   }
   return(&lettergrade);
}

void getgrades (double &grades)
//prompts user for grades
{
   cout << "Enter final grades of students" << endl;
   cout << "Enter a negative number to quit    ";
   cin >> grades;

   return;
}

void averaging(int &count, double grades, double &total, char lettergrade)
//increments grades and count if user follows directions
{
   if (0 <= grades  && 100 >= grades)
   {
      total += grades;
      count++;
      cout << "Average " << grades << " is the letter grade "
           << clettergrade(grades, lettergrade) << endl << endl;
   }
   else  // only executes if the user inputs a value not in the specified range
   {
      cout << "The grade you entered was not between";
      cout << " the specified parameters" << endl << endl;
   }
   return;
}



When I run it I get this error: assignment to `char' from `const char*' lacks a cast.  I cannot figure out what to do to fix it.  Any and all help will be greatly appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
you have several minor errors.
first, all the assignments in clettergrade should be
lettergrade = 'A';
and not
lettergrade = "A";
and so on.
then, the return value should be
return(lettergrade);
and not
return(&lettergrade);
since you want to return a reference, not an address.

other then that there are other minor compilation problems of unmatched prototypes tha you can solve easily.

Njoy.

Commented:
You need to change "A" to 'A', the double quotes are used for strings and you are trying to use a single character.  Also you need to change the return at the end of the if statement to return(lettergrade) instead of return(&lettergrade).

  if (895 <= average)
  {
      lettergrade = 'A';

  }
  else if (795 <= average)
  {
      lettergrade = 'B';

  }
  else if (695 <= average)
  {
      lettergrade = 'C';

  }
  else if (595 <= average)
  {
      lettergrade = 'D';

  }
  else if (595 > average)
  {
      lettergrade = 'F';

  }
  return(lettergrade);
}

Hope that helps

Commented:
Yeah, you do have some other minor problems, as hoofien said.  Like with your function:
 
prototype:
void averaging(arguments)

but when you use averaging there is an extra argument.

you may have some more but thats what I see right now, ask if you need more help
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Commented:
One other thing is that in your clettergrade() function, since you pass lettergrade as a reference, the actual variable you passed gets changed.. therefore no return value is nesisary, really.

The error "cannot convert TYPE1 to TYPE2" usually means that you used the wrong variable type someplace. ie:

int noWork="this'll never work. ever";

but sometimes you want to do things like that, so you could do:

int work=int('a');

the int( ) part casts it to int (that is, the compiler tries it's best to turn 'a' into an int, in this case, it turns it into the integer 97, the ascii value for 'a'.)

But in this case where it's 'const char *' and 'char' it means that you're trying to use a string (more precicely a pointer to an array of chars) (declared like char *s="hi";) as a char. So on the line that gives the error, check if you're using the right values etc.

Commented:
This question didn't show any activity for more than 21 days. I will ask Community Support to close it unless you finalize it yourself within 7 days.
You can always request to keep this question open. But remember, experts can only help if you provide feedback to their comments.
Unless there is objection or further activity,  I will suggest to accept

    "hoofien"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
========
Werner
Force accepted

** Mindphaser - Community Support Moderator **

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial