Keep getting 4 errors in my code and dont understand

hey. in my code im keep getting errors and i don't understand what they mean and how I can correct it. Im fairly new to C++.

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
34
35
 #include <stdio.h>
#include <stdlib.h>
#define ARRAYSIZE 5

float calculateMean() //Calculate the average of 5 integers
{
	float result = 0;
	
   	for(int i = 0; i < ARRAYSIZE; i++)
	{
 		result + (float)myIntArray[i];
 	}

	return result / ARRAYSIZE;
}

int main()
{
	char inputBuffer[BUFFERLEN];
 	int myIntArray[5];
 	float result;

 	for(int i = 1; i <= ARRAYSIZE i++)
	{ 
 		printf("Supply number %d", i);
 		scanf("%d", myInt);
 		myIntArray[i] = myInt;
 	}

 	result = calculateMean(result);
 	printf("The average of the five numbers is: %.2f", result);

 	return 0;
}
are you learning C or C++? This is C, or nearly.

the function, calculate mean, is talking about myintarray, but there is no such variable. That variable is local to main, and if you want it in the function, you need to pass it in as a parameter.

there is nothing called bufferlen, perhaps you meant arraysize?

line 23 missing a ;
the format is
for(initialize; condition; action)
you have
for(initialize; condition action)

myint (27) does not exist.

30 you have a parameter to the function, which does not take one!

There were some other bugs as well. I think this one works and is close to what you wanted
and moves it toward c++:

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
34
35
#include <cstdio>
#include <cstdlib>
const int ARRAYSIZE{5}; //#defined items have no clean type, can be an issue in some code

float calculateMean(int *myIntArray) //Calculate the average of 5 integers
{
	float result{}; //sets to zero.  {42.5} sets to 42.5.  this is preferred in c++ to initialize. 
	
   	for(int i = 0; i < ARRAYSIZE; i++)
	{
 		result += myIntArray[i]; //+= here, you added and discarded the result. 
 	}

	return result /(float)ARRAYSIZE; //cast to ensure floating point math. 
}

int main()
{  //removed unused variable. 	
 	int myIntArray[5];
 	//float result;

 	for(int i = 0; i < ARRAYSIZE; i++) //c++ and C index from 0 not 1
	{ 
 		cout << "Supply number " << i << endl; //printf("Supply number %d", i);
 		//scanf("%d", myInt);
		cin >> myIntArray[i]; //removed useless variable and extra step
 	}

 	//result = calculateMean(myIntArray);
 	cout << "The average of the five numbers is: " <<  calculateMean(myIntArray) << endl;

 	//return 0; //not required
}



a note on errors:
the compiler tells you what is wrong, and where. It can take a while to learn to read the messages, but it tries hard.

In function 'float calculateMean()':
11:20: error: 'myIntArray' was not declared in this scope
that is what it said, and what it means is you used a variable that does not exist at that location.

23:33: error: expected ';' before 'i'
Its hard to say that any plainer. Line 23 is missing a semi

and so on. read the message, look at the line it points to, try to fix it.
note that after the first error, subsequent errors CAN be nonsense, so if you can't make any sense of them, fix the top most error and recompile to see what is next and you may get a better message.
Last edited on
ohk got it. Thanks
Using a C++ run-time variable sized container such as a std::vector let's the user enter as many or few numbers as they want.
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 <vector>

double CalcAv(const std::vector<int>& vec)
{
   double sum { };

   for (size_t i { }; i < vec.size(); ++i)
   {
      sum += vec[i];
   }

   return sum / vec.size();
}

int main()
{
   std::vector<int> vec;

   for (int input;
        (std::cout << "Enter a number (0 to stop): ") && std::cin >> input && input != 0;)
   {
      vec.push_back(input);
   }

   std::cout << "The average is: " << CalcAv(vec) << '\n';
}
Enter a number (0 to stop): 1
Enter a number (0 to stop): 1
Enter a number (0 to stop): 2
Enter a number (0 to stop): 3
Enter a number (0 to stop): 3
Enter a number (0 to stop): 3
Enter a number (0 to stop): 4
Enter a number (0 to stop): a
The average is: 2.42857

Having an input loop like the one used takes care of a user entering non-numeric values gracefully. The loop terminates if 0 or alphabetic character is entered.
If you have something like a vector, then you can sum the contents easily using std::accumulate (#include <numeric>) :

1
2
3
4
double CalcAv(const std::vector<int>& vec)
{
    return std::accumulate(vec.begin(), vec.end(), 0.0) / vec.size();
}


http://www.cplusplus.com/reference/numeric/accumulate/
Topic archived. No new replies allowed.