The villiage idiot is back! -Function Help Please???

This is a discussion on The villiage idiot is back! -Function Help Please??? within the C++ Programming forums, part of the General Programming Boards category; Hello All! I am in week three of my C++ class and it still isn't coming easy! I'm quite sure ...

  1. #1
    mzt
    mzt is offline
    Registered User
    Join Date
    Mar 2004
    Posts
    5

    Unhappy The villiage idiot is back! -Function Help Please???

    Hello All!

    I am in week three of my C++ class and it still isn't coming easy!
    I'm quite sure it never will!! This week I am writing a function to compute grades from numeric to alpha. I am getting an error and I have read everything I can get my hands on and still have no idea. I'm sure just one look from any of you will figure out what I have struggled with for days. (yes days - sick and wrong isn't it!)

    Below is the code - function and main .cpp and the header .h files.

    Thank you in advance for any assistance you can give me.


    mz


    This is the main.
    Code:
    #include <iostream.h>
    #include "Calcgrades.h"  
    
    void main()				//main program
    	{
    	 float A, B, C, D, F;
     	 char ngrade, lgrade;
    	 float count = 0.0;
    	 A = 0.0;
         B = 0.0;
         C = 0.0;
         D = 0.0;
         F = 0.0;
    
    
    	while (count < 10.0);  // put at 10 for testing
    	{
    	Calcgrades();
    
    }
    	cout <<"Grades (A, B, C, D, F) :  ";
    	cout <<A<< "," <<B<< "," <<C<< "," <<D<< "," <<F<< endl;
    
    }
    
    
    
    Code:
    
    
    
    This is the function file:
    
    
    
    Code:
    	char Calcgrades(char lgrade,char ngrade);
    
    
    {	if (ngrade >= 90)
    	{	lgrade = A;
    	}
    	else if (ngrade >= 80 and < 90)
    
    	{		lgrade = B;
    	}
    	else if (ngrade >= 70 and < 80)
    	{		lgrade = C;
    	}
    	else if (ngrade >= 60 and < 70)
    	{		lgrade = D;	
    	}
    	else if (ngrade < 50)
    	{		lgrade = F;	
    	}
    		cout >> "calcgrade lgrade :" >> lgrade >>; endl;  //for test
    
    			return lgrade;
    }
    This is the header file.
    char Calcgrades(); float count = 0.0;


    THANKS!!!!

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    what exactly is the error?

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904

    Re: The villiage idiot is back! -Function Help Please???

    Code:
    #include <iostream.h>
    #include "Calcgrades.h"  
    
    void main()				//main program
    {
    	float A, B, C, D, F;
     	char ngrade, lgrade;
    	float count = 0.0;
    	A = 0.0;
    	B = 0.0;
    	C = 0.0;
    	D = 0.0;
    	F = 0.0;
    
    	while (count < 10.0);  // put at 10 for testing
    	{
    		Calcgrades();
    	}
    	cout <<"Grades (A, B, C, D, F) :  ";
    	cout <<A<< "," <<B<< "," <<C<< "," <<D<< "," <<F<< endl;
    }

    function file:
    Code:
    #include "CalcGrades.h"   // Need to include this
    
    char Calcgrades(char lgrade,char ngrade)  // get rid of semicolon here
    {
    	if (ngrade >= 90)
    	{	lgrade = A;
    	}
    	else if (ngrade >= 80 and < 90)
    	{		lgrade = B;
    	}
    	else if (ngrade >= 70 and < 80)
    	{		lgrade = C;
    	}
    	else if (ngrade >= 60 and < 70)
    	{		lgrade = D;	
    	}
    	else if (ngrade < 50)
    	{		lgrade = F;	
    	}
    	cout >> "calcgrade lgrade :" >> lgrade >>; endl;  //for test
    	return lgrade;
    }
    header file

    Code:
     
    char Calcgrades();
    extern float count;   // make this extern or you will get errors from trying to declare it multiple times
    also, this program will go forever calculating grades because count isn't being incremented, you might want to do that somewhere

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    here Calcgrades doesn't have any parameters:

    Calcgrades();

    and here Calcgrades doesn't have any parameters:

    char Calcgrades();

    but here it does:

    char Calcgrades(char lgrade,char ngrade);

    Oops.

    Also, the semicolon at the end of the last line above is an error. The last line above is a function definition, not a function declaration. Function definitions don't have a semi-colon there, function declarations do.


    You return lgrades to main(), but you ignore the return value. Not going to help back in main() that way.

    The while loop is unending as you never change the value of count.

    I guess that's enough to start with.

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    also, in your function, you're trying to access the A-F variables, but they haven't been declared inside of that function or as externs in the header file, you would get an undefined variable error from that

  6. #6
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    One last thingy to add :

    use :

    Code:
    #include<iostream>
    instead of:

    Code:
    #include<iostream.h>
    The first is compliant to the CPP standart and the second is not...

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I guess this (possible) infinite loop wasn't intentional?
    Code:
    while (count < 10.0);  // put at 10 for testing
    {
    	Calcgrades();
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    mzt
    mzt is offline
    Registered User
    Join Date
    Mar 2004
    Posts
    5

    THANK YOU!

    THANK YOU: jverkoey, borko_b, Magos, elad


    I can't tell you how much I appreciate your help! After reading all your comments, I realize what a mess this project was!! AND how frustrated I was when I posted, since I forgot to tell everyone what error I was trying to get past!

    I have made all the corrections you all have graciously advised me on. The only error I am still getting (and the one I was most frustrated about is:

    error C2447: missing function header (old-style formal list?)

    I am getting this error on the function file? And I am still getting it after I have made all the changes suggested? I must be doing something wrong (again - imagine that)!


    If anyone can enlighten me on this error, I would greatly appreciate it. I looked it up in the MS library and it really doesn't explain it for "dummies". Maybe I should buy that book!?!?!


    THANKS again to everyone helping me.

    mz


  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I suspect that you are making a function call that doesn't fit the declaration/definition of the function or your function declaration doesn't agree with your function definition. Please post your current code.

  10. #10
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    Just look here
    Code:
    char Calcgrades(char lgrade,char ngrade);  // <- this shouldn't be here! :) :)
    
    {	
      if (ngrade >= 90)
    	{	lgrade = A;
    	}
    	else if (ngrade >= 80 and < 90)
    
    	{		lgrade = B;
    	}
    	else if (ngrade >= 70 and < 80)
    	{		lgrade = C;
    	}
    	else if (ngrade >= 60 and < 70)
    	{		lgrade = D;	
    	}
    	else if (ngrade < 50)
    	{		lgrade = F;	
    	}
    		cout >> "calcgrade lgrade :" >> lgrade >>; endl;  //for test
    
    			return lgrade;
    }
    You probably copied the function declaration from the header like this :

    Code:
    //Some header file :
    //copy begin
    char Calcgrades(char lgrade,char ngrade);
    //end
    Code:
    //Some cpp file :
    //paste begin
    char Calcgrades(char lgrade,char ngrade);
    {
      ...
    }
    //end

  11. #11
    mzt
    mzt is offline
    Registered User
    Join Date
    Mar 2004
    Posts
    5

    Unhappy Posting code again!

    Hey Experts!

    This is the new code after I (think) did all that you told me. The header file error is on the function file. Still the same error. I was hoping that the header file error would go away so I can debug and find the other errors I have in the code! (I'm sure there are others!)

    THANK YOU FOR YOUR HELP!! IT is amazing to me that you can just look at the code and fire back the errors like this.

    mz

    HEADER FILE

    Code:
    char Calcgrades(char lgrade,char ngrade, char A,char B,char C,char D,char F);
    extern float count;  
    char A,B,C,D,F;
    MAIN FILE

    [/CODE]

    #include <iostream.h>
    #include "Calcgrades.h"

    void main() //main program
    {
    float A, B, C, D, F;
    char ngrade, lgrade;
    float count = 0.0;
    A = 0.0;
    B = 0.0;
    C = 0.0;
    D = 0.0;
    F = 0.0;


    while (count < 10.0) // put at 10 for testing
    {
    char ngrade, lgrade;
    Calcgrades(ngrade,A, B, C, D, F,lgrade);
    cout << "lgrade after Calcgrades is: " <<lgrade<< endl;
    }

    cout <<"Grades (A, B, C, D, F) : ";
    cout <<A<< "," <<B<< "," <<C<< "," <<D<< "," <<F<< endl;

    }

    [/CODE]

    FUNCTION FILE

    [/CODE]

    #include "Calcgrades.h"


    {
    char lgrade, char ngrade, char A,char B,char C,char D,char F


    if (ngrade >= 90)
    { lgrade = A;
    }
    else if (ngrade >= 80 and < 90)
    { lgrade = B;
    }
    else if (ngrade >= 70 and < 80)
    { lgrade = C;
    }
    else if (ngrade >= 60 and < 70)
    { lgrade = D;
    }
    else if (ngrade < 50)
    { lgrade = F;
    }
    cout >> "calcgrade lgrade :" >> lgrade >>; endl; //for test
    count = count + 1.0;
    return lgrade;



    }


    [/CODE]

  12. #12
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    "and" is not an operator in C++. Try && instead and use it like this:
    Code:
    else if ( (ngrade >= 80) && (ngrade < 90) )
    { 
      lgrade = B;
    }
    Also:
    Code:
    #include "Calcgrades.h"
    
    
    {
    char lgrade, char ngrade, char A,char B,char C,char D,char F
    Is that supposed to be a function? Maybe you meant:
    Code:
    char Calcgrades(char lgrade,char ngrade, char A,char B,char C,char D,char F)
    {
    //...
    I don't know what they are teaching you in the class you are taking, but it isn't up to the standards.

    Code:
    #include <iostream> //unless you have an old compiler
    #include "Calcgrades.h" 
    using namespace std; //again, if you have an old compiler this wont work
    int main() //main is never void
    {
    At least change to int main(), but I'm guessing your compiler won't have the new headers.

    Also, it doesn't seem like you are bothering to format your code at all, for example:
    Code:
    while (count < 10.0) // put at 10 for testing
    {
    char ngrade, lgrade;
    Calcgrades(ngrade,A, B, C, D, F,lgrade);
    cout << "lgrade after Calcgrades is: " <<lgrade<< endl;
    }
    I would indent the code inside the braces, and in you formatted your if statements oddly, but I suppose that is ok unless you have more than one line of code inside the braces

    You probably have other problems, but that should get you started
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The following code is uncompiled but I think it will work. Compare to your code and try to figure out where and why they are different. Ask questions to learn why they are diferent if you can't figure it out yourself.
    Code:
    //Calcgrades.h//
    char Calcgrades(float ngrade); 
    
    --------------------------------------------------------------------------------
    //MAIN FILE//
    
    #include <iostream.h>
    #include "Calcgrades.h" 
    
    int main() 
    {
      char lgrade;
      float ngrade = 45.1;
      int count = 0;
    
      while (count < 5) 
      {
        lgrade = Calcgrades(ngrade);
        cout << "ngrade of " << ngrade << " is lgrade of " << lgrade << endl;
        count++;
        ngrade = ngrade + 10;
      }
    }
     
    
    //Calgrades.cpp//
    
    #include "Calcgrades.h"
    
    char Calcgrades(float ngrade)
    {
      char letterGrade;
      if (ngrade >= 90)
          letterGrade = 'A';
      else if (ngrade >= 80 && < 90)
          letterGrade = 'B';
       else if (ngrade >= 70 && < 80)
          letterGrade = 'C';
       else if (ngrade >= 60 && < 70)
          letterGrade = 'D'; 
       else if (ngrade < 60)
          letterGrade = 'F'; 
    
      return letterGrade;
    }
    Last edited by elad; 03-19-2004 at 03:38 PM.

  14. #14
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Originally posted by JaWiB
    "and" is not an operator in C++.
    Actually, "and" is a valid keyword and can be used instead of "&&". However, I wouldn't recommend it since most programmers are more familiar with "&&" and some compilers do not support it (like MSVC++ 6.0).

  15. #15
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Wow, I've never heard that before. Learn something new every day I guess
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. using push back function of a vector class template
    By anti-hw in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2002, 12:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21