User Defined Functions

I'm running into errors with my code and I don't know how to fix it. The problem that I'm doing is below:

For example, you have been creating the function main since the beginning, and it is a function without parameters. Using this information, write a function called getLabGrades that prompts the user for each assignment grade (there are 5). Remember that each lab is worth 120 points. Use the following code in main to call it:
int main() {
double labs = getLabGrades();
cout << "Your lab grade is: " << labs << endl;
return 0;
}


Sample output:

Please enter best grade for lab 1: 100
Please enter best grade for lab 2: 95
Please enter best grade for lab 3: 90
Please enter best grade for lab 4: 85
Please enter best grade for lab 5: 80
Lab grade: 540
Your lab grade is: 540


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  #include <iostream>
#include <string>
#include <time.h>
using namespace std;

int getLabGrades(){
	const int NUMLABS = 5;
	double grade[NUMLABS];
	for ( int x = 0; x < NUMLABS; x++){
		grade[x] = 0;
		cout << "Please enter best grade for lab " << NUMLABS << ": ";
		cin >> grade[x];
		{
			grade[x] = 0;
			cout << "Please enter best grade for lab " << NUMLABS << ": ";
			cin >> grade[x];
		}
	}

	int main(){		
			double labs = getLabGrades();
			cout << "Your lab grade is: " << labs << endl;
			return 0;
	
	
}
closed account (SECMoG1T)
you could do this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
double getLabGrades()
{
    double total=0,currentgrade=0;
    int counter=0;
    const int numlabs=4;

    while(counter<numlabs)
     {
        cout << "Please enter best grade for lab " << counter+1 << ": ";
        std::cin>>currentgrade;

        if(currentgrade>120||currentgarde<0)
         {
             while(currentgrade<0||currentgrade>120)
               {
                  std::cout<<"Invalid score, enter correct grade for lab "<<counter+1<<" : ";
                 std::cin>>currentgrade;
                }
          }
        total+=currentgrade;
        ++counter;
     }

   return total;
 }
Last edited on
@TC: Could you post the errors you are having?
@andy1992 that doesn't work.

@Zhuge the error: 1>c:\users\courthey buckmon\documents\visual studio 2010\projects\assignment 6-1\assignment 6-1\problem.cpp(20): error C2601: 'main' : local function definitions are illegal
1> c:\users\courthey buckmon\documents\visual studio 2010\projects\assignment 6-1\assignment 6-1\problem.cpp(6): this line contains a '{' which has not yet been matched
1>c:\users\courthey buckmon\documents\visual studio 2010\projects\assignment 6-1\assignment 6-1\problem.cpp(27): fatal error C1075: end of file found before the left brace '{' at 'c:\users\courthey buckmon\documents\visual studio 2010\projects\assignment 6-1\assignment 6-1\problem.cpp(6)' was matched
Could you please show your current code?
double getLabGrades(){
const int NUMLABS = 5;
double grade[NUMLABS];
for ( int x = 0; x < NUMLABS; x++){
grade[x] = 0;
cout << "Please enter best grade for lab " << NUMLABS << ": ";
cin >> grade[x];
{
grade[x] = 0;
cout << "Please enter best grade for lab " << NUMLABS << ": ";
cin >> grade[x];
}
}

int main(){
double labs = getLabGrades();
cout << "Your lab grade is: " << labs << endl;
return 0;


}
So basically, you ignore everything andy told you? Seriously?...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
double getLabGrades()
{
	double total = 0, currentgrade = 0;
	int counter = 0;
	const int numlabs = 5;

	while (counter < numlabs)
	{
		cout << "Please enter best grade for lab " << counter + 1 << ": ";
		std::cin >> currentgrade;

		if (currentgrade > 120 || currentgrade < 0)
		{
			while (currentgrade < 0 || currentgrade>120)
			{
				std::cout << "Invalid score, enter correct grade for lab " << counter + 1 << " : ";
				std::cin >> currentgrade;
			}
		}
		total += currentgrade;
		++counter;
	}

	return total;
}

int main()
{
	double labs = getLabGrades();
	cout << "Your lab grade is: " << labs << endl;
	system("pause");
	return 0;
}


Topic archived. No new replies allowed.