Thread: Help needed :-(

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    Haddock, GA, United States
    Posts
    13

    Help needed :-(

    Can you help me understand why I keep getting this error message when I compile this code? The error message is: determine_grade redefinition different basic types.

    Code:
    #include <stdio.h>
    				   
    int main(void)
    {
    	// declaring variables
    	float test_1,
    		test_2,
    		test_3,
    		test_4,
    		test_5;
    	char letter_1,
    		letter_2,
    		letter_3,
    		letter_4,
    		letter_5;
    	float test_average;
    
    	// printing a safe-entry message for the user
    	printf("WARNING! Only values between 0-100 are valid.\n");
    	
    	// asking the user for the test scores
    	printf("Test 1: ");
    	scanf("%f", &test_1);
    	printf("Test 2: ");
    	scanf("%f", &test_2);
    	printf("Test 3: ");
    	scanf("%f", &test_3);
    	printf("Test 4: ");
    	scanf("%f", &test_4);
    	printf("Test 5: ");
    	scanf("%f", &test_5);
    
    	// passing the arguments to the calc_average function
    	test_average = calc_average(test_1, test_2, test_3, test_4, test_5);
    
    	// passing the arguments to the determine_grade function
    	letter_1 = determine_grade(test_1);
    	letter_2 = determine_grade(test_2);
    	letter_3 = determine_grade(test_3);
    	letter_4 = determine_grade(test_4);
    	letter_5 = determine_grade(test_5);
    
    	// printing the test scores, equivalent letters, and
    	// the test average and its letter on the screen
    	printf("\n%.2f\t%c\n", test_1, determine_grade(letter_1));
    	printf("%.2f\t%c\n", test_2, determine_grade(letter_2));
    	printf("%.2f\t%c\n", test_3, determine_grade(letter_3));
    	printf("%.2f\t%c\n", test_4, determine_grade(letter_4));
    	printf("%.2f\t%c\n", test_5, determine_grade(letter_5));
    	printf("Test Average: %.2f\t%c\n", test_average, determine_grade(test_average));
    
    }
    
    // declaring the calc_average function
    float calc_average(float, float, float,
    				   float, float);
    float calc_average(float score_1, float score_2, float score_3,
    				   float score_4, float score_5)
    {
    	float average;
    
    	average = (score_1 + score_2 + score_3 + score_4 + score_5) / 5.0;
    
    	return average;
    }
    
    // declaring the determine_grade function
    char determine_grade(float);
    char determine_grade(float test_score)
    {
    	if (test_score >= 90 && test_score <= 100)
    		return 'A';
    	else if (test_score >= 80 && test_score <= 89)
    		return 'B';
    	else if (test_score >= 70 && test_score <= 79)
    		return 'C';
    	else if (test_score >= 60 && test_score <= 69)
    		return 'D';
    	else if (test_score >= 0 && test_score < 60)
    		return 'F';
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    char determine_grade(float);
    Move that forward declaration up above the main() function. You are attempting to call determine_grade() in the main() function, but it isn't defined yet!

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    char determine_grade(float);
    char determine_grade(float test_score)
    That first line needs to appear before the main() function in the code.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    50
    To be safe, shouldn't all function declarations go above main? Meaning this too should go up top for safety's sake:

    Code:
    // declaring the calc_average function
    float calc_average(float, float, float,
    				   float, float);

  5. #5
    Registered User
    Join Date
    Apr 2008
    Location
    Haddock, GA, United States
    Posts
    13
    Okay, I did. I tried to move both of the statements declaration for both of the functions, but now the compiler says: determine_grade not all control paths return a value. :-(

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    50
    Quote Originally Posted by adrian.mowrey View Post
    Okay, I did. I tried to move both of the statements declaration for both of the functions, but now the compiler says: determine_grade not all control paths return a value. :-(
    What if test_score is -15 ?

  7. #7
    Registered User
    Join Date
    Apr 2008
    Location
    Haddock, GA, United States
    Posts
    13
    I haven't gotten to the error handling part. I printed a message to let the user know that values between 0-100 are valid. But I don't understand why the compiler says that not all control paths return a value.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by adrian.mowrey View Post
    Okay, I did. I tried to move both of the statements declaration for both of the functions, but now the compiler says: determine_grade not all control paths return a value. :-(
    Don't be discouraged that you got another error. Getting another error doesn't necessarily mean what you was wrong. It merely showed you that there is another error in the code that it didn't find before.
    That said, after taking into account the post above, check this out, as well:
    SourceForge.net: Do not remove parameter names - cpwiki

    The error says that that function may not return a value!
    If test_score < 0 or test_score > 100, the function will not return anything!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    50
    Quote Originally Posted by adrian.mowrey View Post
    I don't understand why the compiler says that not all control paths return a value.
    Nothing gets returned by determine_grade if test_score is below zero.

    Two options the way I see it:
    Change the last else if just just an else.
    or...
    Append a final else, and have a special character for an error(perhaps 'E' or '!')



    edit: Elysia explained it maybe a bit better than me

  10. #10
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Return '?'. That's easy for the user. Else s/he will only see some random crap.

  11. #11
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Simply remove the check for 'test_score >= 0' and do something like:


    Code:
       elseif ( test_score < 60 )
           	return 'F';
       else 
            return 'Z'; / * A+ grade (with extra credit) */
    Last edited by slingerland3g; 07-27-2009 at 01:38 PM.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code tags ends with a /, not \!
    This doesn't fix the > 100 bug, however.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Location
    Haddock, GA, United States
    Posts
    13
    Okay, this worked.

    Code:
    /*
    Chapter: 3
    Exercise: 7
    Author: Adrian Gheorghe Mowrey
    */
    
    #include <stdio.h>
    
    float calc_average(float, float, float,
    				   float, float);
    
    char determine_grade(float);
    
    int main(void)
    {
    	// declaring variables
    	float test_1,
    		test_2,
    		test_3,
    		test_4,
    		test_5;
    	char letter_1,
    		letter_2,
    		letter_3,
    		letter_4,
    		letter_5;
    	float test_average;
    
    	// printing a safe-entry message for the user
    	printf("WARNING! Only values between 0-100 are valid.\n");
    	
    	// asking the user for the test scores
    	printf("Test 1: ");
    	scanf("%f", &test_1);
    	printf("Test 2: ");
    	scanf("%f", &test_2);
    	printf("Test 3: ");
    	scanf("%f", &test_3);
    	printf("Test 4: ");
    	scanf("%f", &test_4);
    	printf("Test 5: ");
    	scanf("%f", &test_5);
    
    	// passing the arguments to the calc_average function
    	test_average = calc_average(test_1, test_2, test_3, test_4, test_5);
    
    	// passing the arguments to the determine_grade function
    	letter_1 = determine_grade(test_1);
    	letter_2 = determine_grade(test_2);
    	letter_3 = determine_grade(test_3);
    	letter_4 = determine_grade(test_4);
    	letter_5 = determine_grade(test_5);
    
    	// printing the test scores, equivalent letters, and
    	// the test average and its letter on the screen
    	printf("\n%.2f\t%c\n", test_1, letter_1);
    	printf("%.2f\t%c\n", test_2, letter_2);
    	printf("%.2f\t%c\n", test_3, letter_3);
    	printf("%.2f\t%c\n", test_4, letter_4);
    	printf("%.2f\t%c\n", test_5, letter_5);
    	printf("Test Average: %.2f\t%c\n", test_average, determine_grade(test_average));
    
    }
    
    // declaring the calc_average function
    float calc_average(float score_1, float score_2, float score_3,
    				   float score_4, float score_5)
    {
    	float average;
    
    	average = (score_1 + score_2 + score_3 + score_4 + score_5) / 5.0;
    
    	return average;
    }
    
    // declaring the determine_grade function
    char determine_grade(float test_score)
    {
    	if (test_score >= 90 && test_score <= 100)
    		return 'A';
    	else if (test_score >= 80 && test_score <= 89)
    		return 'B';
    	else if (test_score >= 70 && test_score <= 79)
    		return 'C';
    	else if (test_score >= 60 && test_score <= 69)
    		return 'D';
    	else (test_score >= 0 && test_score  < 60);
    		return 'F';
    }
    Thank you "crowe," and of course everybody else for the replies. ;-) I didn't need anything to handle the errors, I just needed to demonstrate the purpose of functions and how to return values back, etc. Thank you again! :-D

  14. #14
    Registered User
    Join Date
    Jul 2009
    Posts
    50
    Quote Originally Posted by adrian.mowrey View Post
    Okay, this worked.

    Code:
    	else (test_score >= 0 && test_score  < 60);
    This is wrong. else is the final result. As in, if none of the other conditions evaluate to true, then you do this. So you shouldn't have an expression there.

    Code:
    	else
    		return 'F';
    Still, this isn't really the best solution. You'd probably want to do it more like:

    Code:
    ...
    	else if (test_score >= 0 && test_score < 60)
    		return 'F';
    	else
    		return '?';
    That would handle values both above 100 and below 0.


    One more thing to consider: Multiple returns are generally considered a no-no.

  15. #15
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    Code:
    // declaring the determine_grade function
    char determine_grade(float test_score)
    {
    	if (test_score >= 80 && test_score <= 89)
    		return 'B';
    	else if (test_score >= 70 && test_score <= 79)
    		return 'C';
    	else if (test_score >= 60 && test_score <= 69)
    		return 'D';
    	else if (test_score >= 0 && test_score  < 60)
    		return 'F';
    	else
    		return 'A';
    }
    Elegant in its simplicity.
    wipe on -
    A slap on the hand is better than a slap on the face. A tragic lesson learned far too late in life.
    - wipe off

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  4. C++ Knowledge Needed For Making Graphic Games?
    By Krak in forum C++ Programming
    Replies: 14
    Last Post: 07-11-2003, 09:11 PM