1

not sure where I'm screwing up here. Just learned how to dynamically allocate arrays today, and now I'm trying to have the user allocate a number of integers and have the program find the average of the inputted numbers. But right when the user is going to read in the numbers in the newly allocated array, I get a segmentation fault. Am I going out of bounds?? Or am I just treating a pointer inappropriately?? Be as harsh or honest as you want with your answers, I really just want to learn what I did wrong.

The reason for so many loops was because not only do I have to have a break in the loop for 0, but I also need to repeat the iteration whenever NULL is returned from the function or when a negative number is entered.

Thank you so much in advance!!

#include <iostream>
using namespace std;

int* allocateArray(int);

int main()
{
   int size;
   int *ptr = NULL;
   double sum = 0;


   cout << "Enter Number of Integers. (Enter 0 to Exit)\n";
   cin >> size;
   while (size != 0)
   {
      do
      {
         do
         {
            while (size < 0)
            {
               cout << "Enter Number of Integers. (Enter 0 to Exit) \n";
               cin >> size;
            }
            ptr = allocateArray(size);

         }
         while (ptr = NULL);

         for (int i = 0; i < size; i++)
         {
            cout << "Enter Number for Integer " << i + 1 << endl;
            cin >> ptr[i];
            //THIS is where it stops. I get a segmentation fault no matter what number 
            //Or how many numbers I enter.
            sum += *(ptr + i);
         }
         cout << "The Average is " << (sum/size) << endl;
         delete [] ptr;
         ptr = NULL;
         sum = 0;


      }
      while (size != 0);
   }


   return 0;
}


//Function meant to practice allocating a dynamic array
int* allocateArray(int size)
{
   int* tmp = NULL;

   if (size < 1)
      return NULL;
   try
   {
      tmp = new int[size];
   } catch(bad_alloc)
   {
      tmp = NULL;
   }
   if (tmp == NULL)
   {
      cout << "Failed to allocate the array!\n";
   }
   return tmp;
}

2 Answers 2

1

Replace while (ptr = NULL); with

while (ptr == NULL);

It is common beginner's mistake to get confused between comparision and assignation operator. If you enable all warnings, you should probably get a warning for this.

This will assign NULL to ptr and will evaluate to false and break.

Sign up to request clarification or add additional context in comments.

1 Comment

I Love you. It worked. Thank you. I'm dumb for not seeing that.
0
 while (ptr = NULL);

 for (int i = 0; i < size; i++)
 {
   cout << "Enter Number for Integer " << i + 1 << endl;
   cin >> ptr[i];
   //THIS is where it stops. I get a segmentation fault no matter what number 
   //Or how many numbers I enter.
   sum += *(ptr + i);
 }

You've set "ptr" to NULL in a while loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.