2 Replies - 308 Views - Last Post: 19 February 2013 - 10:52 PM Rate Topic: -----

#1 Speakeasys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-September 12

Add two dynamic user input arrays?

Posted 19 February 2013 - 10:06 PM

#include <iostream>
#include <new>
using namespace std;
void business (int num[], int num2[]);
int main ( )
{
  int i,j,k;
  int * p, *pt;
  cout << "How many numbers would you like add? ";
  cin >> i;
  cout << "What would you like to add?";
  cin >> j;
  cout << "+";
  cin >> k;
  business (i,j,k);

  delete[] p;
  return 0;
}

void business (int )
{
    int n;

    for (n=0; n<i; n++)
    {
      cout << "Enter number: ";
      cin >> p[n];
    }
    cout << "You have entered: ";
    for (n=0; n<i; n++)
      cout << p[n] << ", ";
      cout << p[n] + p[n];
}



So i want to make the function business to obtain the numbers and add them to each other. What am I doing wrong here?

Is This A Good Question/Topic? 0
  • +

Replies To: Add two dynamic user input arrays?

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Add two dynamic user input arrays?

Posted 19 February 2013 - 10:39 PM

Quote

So i want to make the function business to obtain the numbers and add them to each other. What am I doing wrong here?


Quite a few things. First you use pointers that you have have not allocated any memory for. Second you delete memory that you have never allocated. Third I suggest you study the function tutorials contained in my signature, your function prototype must match both the function call and the function implementation.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Add two dynamic user input arrays?

Posted 19 February 2013 - 10:52 PM

You need to pass j and k to the business(int,int) function. Because its a type void function it needs to print out the result after the two numbers are added. Declare say int result in the function followed by the statement result= j+j.Print result.
You don't need the pointers or num arrays in main()or the p[] array or loop in business() either. Nor do you need to pass i to business[].
Delete [] p is also redundant.
IGNORE THIS POST AS IT ONLY ADDRESSES ADDING TWO NUMBERS NOT THE DYNAMIC ALLOCATION OF MEMORY OR THE PASSING OF ARRAYS TO THE FUNCTION.

This post has been edited by buffalobill: 19 February 2013 - 11:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1