Thread: static_casting

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    41

    static_casting

    I've just started learning C++ so I am at the basics. Can anyone help me with my code? It's an excercise from my book and I can't seem to get it to do what I want. The calculategrade function has to return the char grade to main and then display. I can't figure out where I'm going wrong.
    Code:
    #include<iostream>
    using namespace std;
    
    void getScore(int &score);
    calculateGrade(int score);
    
    int main()
    {
    	float grade;
    	int courseScore;
    
    	cout<<"Line 1: Based on the course score, this program "
    		<<"computes the course grade."<<endl;
    
    	getScore(courseScore);
    	calculateGrade(courseScore);
    
    	cout<<"Line 7: Your grade for the course is: "<<grade<<endl;
    
    	return 0;
    }
    
    void getScore(int &score)
    {
    	cout<<"Line 4: Enter course score-> ";
    	cin>>score;
    	cout<<endl<<"Line 6: Course score is: "<<score<<endl;
    }
    
    calculateGrade(int score)
    {
    	float grade;
    
    	if(score >= 90)
    		grade = static_cast<char>('65');
    	else if(score >= 80)
    		grade = static_cast<char>('66');
    	else if(score >= 70)
    		grade = static_cast<char>('67');
    	else if(score >= 60)
    		grade = static_cast<char>('68');
    	else
    		grade = static_cast<char>('70');
    		
    	return(grade);
    }

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    i dont get what u or youre program is trying to do
    can u explain further please

    i can find some stuff uve done wrong but i cant help u if i dont know what u are trying to achieve

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Damn. My first double post.
    Last edited by Cshot; 10-03-2002 at 03:50 PM.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    That's cuz you didn't grab the return value from calculateGrade().

    >>calculateGrade(int score);
    float calculateGrade(int score);

    >>calculateGrade(int score){
    float calculateGrade(int score){

    >> calculateGrade(courseScore);
    grade = calculateGrade(courseScore);


    btw, is everyone here taking the same classes? Or are all professors just assigning the same homework assignments

    EDIT - I will always be one step of you Prelude! (pumps fist in air) Hehe jk, your way was better though. I didn't look at his calculateGrade function. Seen one too many today.
    Last edited by Cshot; 10-03-2002 at 04:21 PM.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    grade should be char, you don't need the static cast, and you really should be using character literals. The function needs to return a value in the tag, and assign what it returns to grade. Compare this with what you have:
    Code:
    #include<iostream>
    using namespace std;
    
    void getScore(int &score);
    char calculateGrade(int score);
    
    int main()
    {
      char grade;
      int courseScore;
      
      cout<<"Line 1: Based on the course score, this program "
        <<"computes the course grade."<<endl;
      
      getScore(courseScore);
      grade = calculateGrade(courseScore);
      
      cout<<"Line 7: Your grade for the course is: "<<grade<<endl;
      
      return 0;
    }
    
    void getScore(int &score)
    {
      cout<<"Line 4: Enter course score-> ";
      cin>>score;
      cout<<endl<<"Line 6: Course score is: "<<score<<endl;
    }
    
    char calculateGrade(int score)
    {
      char grade;
      
      if(score >= 90)
        grade = 'A';
      else if(score >= 80)
        grade = 'B';
      else if(score >= 70)
        grade = 'C';
      else if(score >= 60)
        grade = 'D';
      else
        grade = 'F';
    		
      return(grade);
    }
    Cshot: Stop that!

    -Prelude
    Last edited by Prelude; 10-03-2002 at 03:50 PM.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed