Im trying to add up 100 grades and get the averge but it wont compile because it "cannot convert from 'int' to 'int [100].
Can anyone help me??

#include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{
	int total, j;
	double average;
	int grade[100]= (56,97,73,59,68,79,86,67,79,66,
					 95,64,98,56,79,83,75,89,73,91,
					 52,72,63,81,62,85,37,78,100,89,
					 56,89,75,76,99,66,84,98,68,59,
					 98,77,96,88,67,88,95,79,56,79,
					 56,97,43,59,68,79,85,67,79,66,
					 95,64,98,65,79,83,75,89,73,91,
					 52,72,63,49,62,85,37,78,100,89,
					 84,89,75,76,99,66,84,98,68,59,
					 98,77,96,88,67,88,95,79,56,79);
	char pause;

	for(int j=0; j<100; j++)
	{
		total=total + grade[j];
	}
		average=total/100;

	cout<<"Your average is "<<average<<endl;
	cin>>pause;
	return 0;
}

Dani AI

Generated

Nice catch by on the braces and by / on initialization. A couple of extra, practical clarifications that weren't spelled out yet:

The parentheses vs. braces error: writing int grade[100] = (56, 97, ...); does not create an initializer list. The parentheses + commas form a single expression that uses the comma operator and evaluates to one int (the last value). The compiler therefore sees an int on the right-hand side and complains it cannot convert that int to an int[100]. Curly braces form the proper initializer list: int grade[100] = { ... };. You can also zero-initialize the whole array with int grade[100] = {};.

Watch the average calculation: average = total / 100; performs integer division because both operands are int. Convert one operand to double so you keep the fractional part:

average = static_cast<double>(total) / 100;
 // or
average = total / 100.0;

Small further improvements and troubleshooting tips:

  • Initialize total to 0 at its declaration and prefer a named constant for the count (e.g., const int N = 100;) so the loop and division stay consistent.
  • For clearer, safer code use algorithms: #include <numeric> and then
    double average = std::accumulate(grade, grade + 100, 0) / static_cast<double>(100);
  • Prefer for (int j = 0; j < N; ++j) (pre-increment is a tiny stylistic win) and avoid unused outer declarations.
  • If still getting the same compile error, search for any stray parentheses around the initializer or accidental commas; the compiler message points to the RHS type mismatch.

These points complement the fixes by and others and will give a correct, robust average calculation.

Recommended Answers

All 6 Replies

Good first post! Good title, Code with tags, mildly useful description. All in all, a great first effort.

Unfortunately, you didn't post the entire error message. Also didn't mention what line the error is on. I don't see any lines that would have that error, either. Please add a few more details and we're in business.

!!!Initialize your variables!!!

You have three errors in your code. first you when you declare the vairbable "total" you have to intialize "total" before using it. like

int total = 0;

then when you create an array. you use curly braces like this
int grade[1] = {1,2};
third when you run for loop you declared again the variable "j" in the for loop declaration section

// you wrote
for(int j=0; j<100; j++)	

// my suggetion 
for(j=0; j<100; j++)	
	{		
		total += grade[j];

	}	
{

Hope it helped.

#include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{
	int total = 0;
	double average = 0.0;
	int grade[100]= {56,97,73,59,68,79,86,67,79,66,
					 95,64,98,56,79,83,75,89,73,91,
					 52,72,63,81,62,85,37,78,100,89,
					 56,89,75,76,99,66,84,98,68,59,
					 98,77,96,88,67,88,95,79,56,79,
					 56,97,43,59,68,79,85,67,79,66,
					 95,64,98,65,79,83,75,89,73,91,
					 52,72,63,49,62,85,37,78,100,89,
					 84,89,75,76,99,66,84,98,68,59,
					 98,77,96,88,67,88,95,79,56,79};
	char pause;

	for(int j=0; j<100; j++)
	{
		total=total + grade[j];
	}
		average=total/100;

	cout<<"Your average is "<<average<<endl;
	cin>>pause;
	return 0;
}

I think this will work (can't test it right now)
I removed the int j on line 8,.
Initialized the int total on line 8.
Used curly braces on on line 10 and 19.
Well, that was it.
You might want to init the char pause on line 20, but it isn't necessary.
Clean code BTW

commented: Do not write people's code for them. This includes fixing it. -2
commented: Disagree with Walt, the few points you changed were properly explained +12

Ok thanx you guys, simple mistakes lol, i feel stupid. But thank you all for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.