I am trying to write a program where the user can determine how large to make the array but i keep running into problems. Particularly my last line of code is giving me an error. I am using visual studio. I am very new to programming and would appreciate basic level help. I have not yet learned vectors so i was unable to find a solution on the forums or online. To give you an idea of my programming level i just started reading the c++ by tony gaddis and i am on chapter 10. I will appreciate any help!
//Chapter 9 Program 1 array allocation
//Written by
#include <iostream>
using namespace std;
int main()
{
const int sizeOfArray = 100; //Temporary. User will change this
int numbers[sizeOfArray]; //The array filled with integers
int *intPtr; //Pointer to integer
int userArraySize; //What the user wants the array size to be
cout << " Hi today we will be dynamically allocating an array. " << endl;
cout << " How large would you like the array to be? " << endl;
cin >> userArraySize;
int* numbers = new int[userArraySize];
You declare numbers variable twice. Get rid of first one because you do not need it.
Here is stripped down version and link to online compiler which succesfully compiles it:
1 2 3 4 5 6 7 8 9 10
#include <iostream>
int main()
{
std::cout << " Hi today we will be dynamically allocating an array.\n"" How large would you like the array to be?\n";
int userArraySize; //What the user wants the array size to be
std::cin >> userArraySize;
int* numbers = newint[userArraySize];
}
Thank you so much!! that did the trick. I did not realize i declared it twice. One more question though. How would i allocate user data into an array using pointer dereferencing? My teacher told us we were not allowed to use array notation but instead something like this:
*( pointerToMyAllocatedArray+ i ) = x
Am i suppose to int *intPtr = numbers to make my pointer point to the numbers array? and i will be the size of the array or count? I am sorry I am a novice!
By c++ standard arr[i] is equivalent to *(arr + i), so you can freely substitute one for other. So when you want to use array notation, replace it with pointer notation given above.
(And given that transformation works both ways and pointer ariphmetics is commutative, following works too: i[arr])
Thank you soooo much! You have been a tremendous help! I appreciate you keeping everything basic so i have an easier time understanding. I do have one more question though. I am able to compile my code but it only asks me to input a single number into the array instead of the number specified by the user. Did i do my cin portion at the end wrong?
//Chapter 9 Program 1 array allocation
//Written by
#include <iostream>
using namespace std;
int main()
{
int count;
int userArraySize; //What the user wants the array size to be
cout << " Hi today we will be dynamically allocating an array. " << endl;
cout << " How large would you like the array to be? " << endl;
cin >> userArraySize;
int* numbers = new int[userArraySize];
int *intPtr = numbers; //Pointer to integer
//Ask user to input numbers into array
for (count = 0; count < userArraySize; count++);
{
cout << " What numbers would you like to place into the array? " << endl;
cin >> *(numbers + count);
}
}
for (count = 0; count < userArraySize; count++); //notice stray semicolon
{
cout << " What numbers would you like to place into the array? " << endl;
cin >> *(numbers + count);
}
is threated as:
1 2 3 4 5 6 7
for (count = 0; count < userArraySize; count++)
; //An empty statement for loop body, essentually "do nothing userArraySize times"
{ //beginning of new block, not related to the loop
cout << " What numbers would you like to place into the array? " << endl;
cin >> *(numbers + count);
}
MiiNiPaa i seriously cannot thank you enough!! I finally completed my code and there is no way i could have done it without your help. Thank you soooooooooooooooooooooo much!
Here is my completed code just in case anybody needed to see it as a reference or something
//Chapter 9 Program 1 array allocation
//Written by
#include <iostream>
using namespace std;
int main()
{
int count;
int userArraySize; //What the user wants the array size to be
cout << " Hi today we will be dynamically allocating an array. " << endl;
cout << " How large would you like the array to be? " << endl;
cin >> userArraySize;
int* numbers = new int[userArraySize];
int *intPtr = numbers; //Pointer to integer
//Ask user to input numbers into array
for (count = 0; count < userArraySize; count++)
{
cout << " What numbers would you like to place into the array? " << endl;
cin >> *(numbers + count);
}
//Display the array
for (count = 0; count < userArraySize; count++)
{
cout << " number inputed " << *(numbers + count) << endl;
}
using std::cout and std::cin are more specific and clearer than usingnamespace std; but in this case where you using just #include <iostream> in a cpp file the first opinion are wordy overkill