1,105,310 Community Members

Cannot compile: average 10 numbers using array

Member Avatar
mikejz88
Newbie Poster
3 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I can't seem to get this to compile for nothing; I'm new to this c++ any help out there?

#include <iostream>

int main()
{
	//declare variables
	double average = 0;
	double sum = 0;
	double array[10];
	int k;

	// User Input and Total of number input
	for (k =0; k < 10; k++)
	{
		cout << "Enter Number: " << (k + 1) << end1;
		cin >> array[k];
		sum = sum + array[k];
	}

	average = sum/10;

	for (k = 0; k < 10; k++)
	{
		cout << "The average of the numbers entered are: " << average << end1;
	}

	return ( 0 )
}
Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
0
 

What are the error messages?

(it looks like you're missing the std:: qualification on cout, endl etc. The best way to deal with that in your case is to put

using std::cout;
using std::endl;
using std::cin;

at the top and leaving the rest of the code alone. Note that you could use a blanket using namespace std; at the top, but that's a bad habit to get into, as it pollutes your namespace, rendering all of the hundreds of names housed in std:: unusable for anything else, or worse, causing conflict with your functions and variables of the same name.

Member Avatar
mikejz88
Newbie Poster
3 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

It's giving me an error code saying it's unable to start my program. The system cannot find the file specified.

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

#include <iostream>

using std::cout;
using std::endl;
using std::cin;

int main()
{
	//declare variables
	double average = 0;
	double sum = 0;
	double array[10];
	int k;

	// User Input and Total of number input
	for (k =0; k < 10; k++)
	{
		cout << "Enter Number: " << (k + 1) << endl;
		cin >> array[k];
		sum = sum + array[k];
	}

	average = sum/10;

	for (k = 0; k < 10; k++)
	{
		cout << "The average of the numbers entered are: " << average << endl;
	}

	return ( 0 )
}
Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
0
 

Get rid of lines 4-7. You can't have 2 mains in your program. It can't find the .exe file to start it because the code is not compiling successfully. Monitor the messages at the bottom of the Visual C++ screen to see the errors in your compilation. Double click on them to bring you to the line that has the error.

This is in no way your fault, Microsoft is trying to generalize main to be able to handle different encodings, and it's confusing as all get out when you're first getting started. :)

Member Avatar
mikejz88
Newbie Poster
3 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks! I took out the line 4-7 and forgot an [ ; ] at the end of the code. Now I'm getting weird numbers for my average..

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

using std::cout;
using std::endl;
using std::cin;

int main()
{
	//declare variables
	double average = 0;
	double sum = 0;
	double array[10];
	int k;

	// User Input and Total of number input
	for (k =0; k < 10; k++)
	{
		cout << "Enter Number: " << (k + 1) << endl;
		cin >> array[k];
		sum = sum + array[k];
	}

	average = sum/10;

	for (k = 0; k < 10; k++)
	{
		cout << "The average of the numbers entered are: " << average << endl;
	}

	return ( 0 );
Question Answered as of 2 Years Ago by jonsca
Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
0
 

Not the chief concern, but take off the loop in 26, there's no need to output the same average 10 times.

Also, in C++ (and C99) you can declare the loop variable within the for loop

for (int k = 0;k<10;k++)
{
   //k is available here
}
//k does not exist out here

You're missing your closing brace, but I think that might have been a copy/paste error.

How are you running your program? Try running it at a prompt. I was able to get the correct result.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: