Using Pointers

Apr 2, 2013 at 4:25pm
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;
}
Apr 2, 2013 at 4:29pm
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 Apr 2, 2013 at 4:29pm
Apr 2, 2013 at 4:33pm
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
Apr 2, 2013 at 4:34pm
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;
}


Apr 2, 2013 at 4:39pm
It seems to work just fine: http://ideone.com/3ZNkwZ

What problem are you having?
Apr 2, 2013 at 4:53pm
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
Apr 2, 2013 at 4:54pm
Post the error messages.
Apr 2, 2013 at 5:00pm
---------------------------
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
---------------------------
Apr 2, 2013 at 5:09pm
That's got nothing to do with your program. That has to do with VS not finding your exe. You botched some settings somewhere.
Apr 2, 2013 at 5:15pm
What operating system are you using?

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

Last edited on Apr 2, 2013 at 5:16pm
Apr 2, 2013 at 5:21pm
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
Apr 2, 2013 at 5:29pm
I am using Microsoft Windows on my personal laptop so I don't think I should have any restrictions
Apr 2, 2013 at 5:41pm
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!
Apr 2, 2013 at 5:42pm
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.