help with functions using variables as arguments

This is a discussion on help with functions using variables as arguments within the C++ Programming forums, part of the General Programming Boards category; Hi, I am really new so if this is in the wrong place, I apologize in advance. I am having ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    30

    Question help with functions using variables as arguments

    Hi,

    I am really new so if this is in the wrong place, I apologize in advance. I am having a problem with passing a value that is the return of one function as the argument of another. I thought I had the problem fixed, but I keep getting some errors. Could someone tell me where I have gone wrong?
    Here is my code:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    double getInput();
    double calAvg(double); 
    char getGrade(double );
    void doAgain();
    
    const int numStudents = 5;
    const int numScores = 4;
    
    int main ()
    {
    	getInput();
    	
        doAgain();
    }
    double getInput()
    
    {
    		char name [5] [25];
    		double score [4];
    		int count = 0;
    		double average[5];
    	
    	
    		for (count = 0; count < numStudents; count++)
    		{
    			cout << "Please enter your student's name and press enter:\n";
    			cin.getline(name [5],25);
    			cout << "Please enter his or her average scores and press enter\n";
    			for (count = 0; count < numScores; count++)
    			{
    				cin >> score[4];
    				if (score [4] < 0 || score[4] > 100)
    				{
    					cout << "Scores cannot be less than 0 or more than 100\n";
    					cout << "Please try again.";
    					break;
    			calAvg(score[4]);
    			getGrade(average[5]);
    				}
    			}
    		}
    }
    double calAvg()
    {
    	
    	double score [4];
    	double total;
    	double average[5];
    	
    	for (int loop = 0; loop < numStudents; loop++);
    	{
    		total = 0;
    		total += (score[0] + score[1] + score [2] + score[3]);
    		average[5]  = total / numScores; 
    		cout << "This student's average is " << average[5];
    		system ("cls");
    		return (average[5]);
    	}	
    }
    
    char getGrade()
    
    {
    
    	for (int loop = 0; loop < numStudents; loop++);
    		{
    			if average[5] < 100 && average[5] > 90;
    			{
    				cout << "The student's grade is A";
    			}
    			if average[5] < 89 && average[5] > 80;
    			{
    				cout << "The student's grade is B";
    			}
                if average[5] < 79 && average[5] > 70;
    			{
    				cout << "The student's grade is C";
    			}
                if average[5] < 69 && average[5] > 60;
    			{
    				cout << "The student's grade is D";
    			}
                if average[5] < 69 && average[5] > 0;
    			{
    				cout << "The student's grade is F";
    			}
    		}	
    }
    void doAgain ()
    	{
    		char end;
    
    		cout << "Do You Want To Exit The Program? Please Press Y or y to end:";
    		cin >> end;
    		switch (end)
    			{ 
    				case 'Y':
    				case 'y': exit (1);
    				break;
    				default: main();
    					
    		}
    	return;
    	}
    Here are the errors I received:
    Code:
    Compiling...
    
    gradebook.cpp
    
    c:\documents and settings\____\my documents\visual studio 2005\projects\____\____\gradebook.cpp(80) : error C2061: syntax error : identifier 'average'
    
    c:\documents and settings\____\my documents\visual studio 2005\projects\____\____\gradebook.cpp(84) : error C2061: syntax error : identifier 'average'
    
    c:\documents and settings\____\my documents\visual studio 2005\projects\____\____\gradebook.cpp(88) : error C2061: syntax error : identifier 'average'
    
    c:\documents and settings\____\my documents\visual studio 2005\projects\____\____\gradebook.cpp(92) : error C2061: syntax error : identifier 'average'
    
    c:\documents and settings\____\my documents\visual studio 2005\projects\____\____\gradebook.cpp(96) : error C2061: syntax error : identifier 'average'
    
    Build log was saved at "file://c:\Documents and Settings\____\My Documents\Visual Studio 2005\Projects\____\____\Debug\BuildLog.htm"
    Please help.
    Thanks in advance

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    15
    Until a savvy C++ person comes along, here's my theory:

    Why doesn't the main() have a return statement?

    How come getInput() and getGrade() does not return any value?

    Code:
    for (int loop = 0; loop < numStudents; loop++);
    {
    
    ... return (average[5]);
    }
    I doubt the way you used the return statement in calAvg() is legal...not to mention when you defined that function, you forgot to add in its parameter to recieve the double array from the getInput(). Your getGrade() also have this issue. Finally, take out that semicolon next to the for loop condition statement.

    Why does doAgain() have a return statement if the datatype of the function is void?

    There's more that I know, but I believe these would get you started. I might be wrong, but worth the try. And you seriously need to format your code correctly.
    Last edited by Bird Killer; 03-29-2006 at 09:34 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    You need to read up on how to use arrays, like how to index them, and how to pass them as parameters.

    Here's a start, I got bored
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    double getInput();
    double calAvg(double score[]);
    char getGrade(double average );
    void doAgain();
    
    const int numStudents = 5;
    const int numScores = 4;
    
    int main ()
    {
      getInput();
      doAgain();
      return 0;
    }
    
    double getInput()
    {
        char name [numStudents][25];
        double score [numScores];
        int count = 0, i ;
        double average;
    
    
        for (count = 0; count < numStudents; count++)
        {
          cout << "Please enter your student's name and press enter:\n";
          cin.getline(name [count],25);
          cout << "Please enter his or her average scores and press enter\n";
          for (i = 0; i < numScores; i++) //!! use a different counter
          {
            cin >> score[i];
            if (score [i] < 0 || score[i] > 100)
            {
              cout << "Scores cannot be less than 0 or more than 100\n";
              cout << "Please try again.";
              break;
              }
          }
          average calAvg(score);
          getGrade(average);
        }
    }
    
    double calAvg(double score[])
    {
      double total = 0.0;
    
      for (int loop = 0; loop < numStudents; loop++) //!! removed ;
      {
        total += score[loop];
      }
      total = total / numStudents;
      cout << "This student's average is " << total;
      return (total);
    }
    
    char getGrade( double average )
    {
      for (int loop = 0; loop < numStudents; loop++);
        {
          if ( average < 100 && average > 90 )  //!! pay attention to syntax
          {
            cout << "The student's grade is A";
          }
          if average < 89 && average > 80;  //!! meh, you fix the rest
          {
            cout << "The student's grade is B";
          }
                if average < 79 && average > 70;
          {
            cout << "The student's grade is C";
          }
                if average < 69 && average] > 60;
          {
            cout << "The student's grade is D";
          }
                if average < 69 && average > 0;
          {
            cout << "The student's grade is F";
          }
        }
    }
    
    char doAgain ()
    {
        char end;
    
        cout << "Do You Want To Exit The Program? Please Press Y or y to end:";
        cin >> end;
        switch (end)
          {
            case 'Y':
            case 'y': exit (1);
            break;
            // default: main(); //!! main cannot be called recursively
    
        }
      return end;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions Taking Dif. Numbers of Arguments at Dif. Times?
    By bengreenwood in forum C++ Programming
    Replies: 6
    Last Post: 03-23-2009, 05:12 PM
  2. Calling non-static functions on static variables
    By pandu in forum C++ Programming
    Replies: 14
    Last Post: 06-19-2008, 04:07 AM
  3. Functions in class and class variables.
    By Karakus in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2006, 03:29 AM
  4. Replies: 6
    Last Post: 05-06-2003, 04:08 PM
  5. functions and pointer arguments
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2002, 05:04 PM

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