Using Pointers

I'm having trouble creating a program that returns the user's integers. It returns the first integer and then gives me values for the rest of integers. It should print something like this:

Enter number of integers: 5
Enter integers: -3 -4 6 7 23
You entered: -3 -4 6 7 23

This is my code so far:

#include <iostream>
using namespace std;

int main(){

int *dynArray; // pointer to the dynamically allocated array
int size; // array size

cout << "Enter number of integers: ";
cin >> size;
dynArray = new int [size];

cout << "Enter integers: ";
//for(int i = 0; i < size; i++)
cin >> *dynArray;

cout << "You Entered: ";
for(int i = 0; i < size; i++)
{
cout << dynArray[i] << " ";
cin >> dynArray[i];
}

return 0;
}
You already had a thread: http://www.cplusplus.com/forum/beginner/97625/

Anyway, you have that for-loop commented out, why?

Also, please put your code [code]between code tags[/code]
Last edited on
I commented out the first for loop because after I entered the integers, the program ended. Having that for loop commented out allowed it to print my third line with at least one integer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

int main(){

int *dynArray; // pointer to the dynamically allocated array
int size; // array size

cout << "Enter number of integers: ";
cin >> size;
dynArray = new int [size];

cout << "Enter integers: ";
for(int i = 0; i < size; i++)
    cin >> dynArray[i];

cout << "You Entered: ";
for(int i = 0; i < size; i++)
{
cout << dynArray[i] << " ";
}

return 0;
}


It seems to work just fine: http://ideone.com/3ZNkwZ

What problem are you having?
Hmm.... that's really weird. When I enter that same exact code into Microsoft Visual Studio, it tells me there are build errors and won't even let me run the program
Post the error messages.
---------------------------
Microsoft Visual C++ 2010 Express
---------------------------
Unable to start program 'C:\Users\Owner\Documents\Visual Studio 2010\Projects\Practice_pg150\Debug\Practice_pg150.exe'.



The system cannot find the file specified.


---------------------------
OK
---------------------------
That's got nothing to do with your program. That has to do with VS not finding your exe. You botched some settings somewhere.
What operating system are you using?

Do you have permission to write to the directory where the project is located?

Last edited on
I created a new project and pasted the code in so now it is allowing me to run it! The only problem I'm having now is....after I enter integers, my program just exits and does not print back the numbers I entered
I am using Microsoft Windows on my personal laptop so I don't think I should have any restrictions
I FIGURED IT OUT GUYS!!!! I needed a pause at the end to keep my program from exiting. I just entered:

int pause;
cin >> pause;

Thank you so much for all your help!
That's just how your IDE acts. There should be an option of Run Without Debugging that will keep the window open for you. Alternatively, you can run it from a command line. I recommend learning how to do this at some point.
Topic archived. No new replies allowed.