1

I am trying to create an array using cin to define the size. While that seems to be working (based on what I currently have), none of the other stuff I want to do seems to be working.

For instance, I want to use a for loop to find the smallest int in the array since I will then need to compare it with all the other ints in the array, but no matter where I have the statement to return the smallest int, it does not do it.

What am I doing wrong?

#include <iostream>

using namespace std;

int main(){


  int userSize;
  cout << "Please define size of array: ";
  cin >> userSize;

  int *duckArray = new int[userSize];

  for (int i = 0; i < userSize; i++) {
    cout << "Please enter a number into the array: ";
    cin >> duckArray[i];
  }

  int smallest = duckArray[0];

  for (int i = 0; i < userSize; i++){
    if (duckArray[i] < smallest){
        smallest = duckArray[i];
        cout << smallest << endl;
    }
  }

  //cout << smallest << endl;

  return 0;
}
8
  • 2
    Return the smallest int to where? All you have is the main function. Commented Feb 10, 2016 at 1:16
  • 1
    Is it printing out the correct smallest value? Your code looks right. Commented Feb 10, 2016 at 1:18
  • FWIW: std::min_element will perform the search for you. Commented Feb 10, 2016 at 1:19
  • @Buddy No. It just immediately closes. The reason why I have the commented out code is because I tried it originally like that, but the same thing happened. Commented Feb 10, 2016 at 1:20
  • 1
    you should delete[] your array on the end or even better to use smart pointer Commented Feb 10, 2016 at 1:20

2 Answers 2

0

Your code is working if you change this:

for (int i = 0; i < userSize; i++){
    if (duckArray[i] < smallest){
        smallest = duckArray[i];
    }
}

cout << smallest << endl;

This will find and print the smallest number entered.

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

10 Comments

I tried that already, but when I saw that that did not work, I commented the cout out, and put it in the for loop. Unfortunately, the results are the same for me.
i just tried it and it worked (enter 3 for array size and then then numbers 5, 3 and 1 and it will print 1)
I guess Visual Studio is just not liking me, because it immediately closes after I fill in the array.
maybe this is the problem: your IDE is hiding the result output. Just run it from console and you should see the correct result.
or just add int x; cin >> x; at the end of the code (just before return 0). This will wait for another (dummy) input.
|
-3

Arrays in C++ must have their size declared to the compiler at runtime. Other people are going to explain how you can buffer memory to simulate a dynamically allocating arrays. You can also have your Array at a given size and as the user adds and removes, you can reject inputs over the current size.

I highly recommend you look into Vectors. Vectors are much like ArrayLists in Java. They are a form of higher level collections that resize themselves as you add more elements to them.

4 Comments

As I said, you can do a workaround with pointers. See this link as well for the same question asked years ago and the same answer as the one I provided. Using vectors here is a much better solution.
the thing with the pointers is not a "workaround", it's legal C++ code. But vectors are safer and easier to use. But in this case, this would not solve the problem.
Using a pointer to allocate memory dynamically for an array is not a workaround. In fact, that's what vector itself is doing under the hood.

Your Answer

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