Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Jul 2nd, 2007, 7:25 PM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Getting the wrong answer

I've got this program that uses the new operator with an array. I'm studying pointers and I read that you can use the new operator with an array and a pointer if you don't know how big the array is going to be. So I wrote this program that asks the user how many elements he would like in his array. Then he inputs that. Next, it asks him to enter the values for the elements. Then it inputs that. Finally, I want the program to simply output the sum of the elements. But I'm getting the wrong answer for the sum. I'm just kind of doing this by myself, my books don't show examples similar to this so here is my program:

int main()
{
	int max = 0;
	int sum = 0;
	int count = 0;
	
	cout << "Enter the number of elements you would like: ";
	cin >> max;

	int *pn = new int[max];

	do
	{
		cout << "Enter a value: ";
		cin >> *pn;
		count++;
	}while (count < max);

	for (int i = 0; i < max; i++)
		sum += *(pn + i);

	cout << "The sum is " << sum << endl;

	return 0;
}
357mag is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Code compiled without error, but gets the wrong answer; Calculating Volume Fall Back Son C 15 Oct 21st, 2006 6:51 PM
Wrong answer Edgar Assembly 6 Jul 12th, 2006 7:17 PM
[Python] Simple Comment Stripper UnKnown X Show Off Your Open Source Projects 0 Feb 10th, 2006 7:57 AM
First Python Programme: Fibonacci Finder UnKnown X Python 2 Dec 15th, 2005 6:19 PM
What is wrong with this code? c0ldshadow Visual Basic .NET 5 Dec 5th, 2005 5:37 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:32 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC