C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-24-2006, 02:38 PM   #1
Registered User
 
Join Date: Aug 2006
Posts: 4
User input for array size.

Hello.


I haven't done any programming in since I took CS1100 last year. Now I am taking CS1110 and I don't want to get owned, so I am trying to practice some exercises from the book before my professor assigns anything.


So I am currently stuck with this part of a program--I want to be able to creat an array, the size of which is based upon the input of the user.


This is what I have right now. It doesn't work for obvious reasons but I can't figure anything out to work around it. (I don't know why the indentation is all weird)


Code:
		

cout << "Please enter the number of salespeople./n";
                int n;
	cin >> n;
	const int * const sPtr = &n;
	int salaryarray[ *sPtr ];

I have never used pointers until today, and I am very rusty with arrays. I understand that arrays have to have a constant value as their size. But there has to be a way to make that size be specified by the user. Right?
coolmoniker is offline   Reply With Quote
Old 08-24-2006, 02:41 PM   #2
Registered User
 
Join Date: May 2006
Posts: 894
Yes there is and it is called dynamic memory allocation.
Code:
std::cin >> n;
int* salaryarray = new int[n];
The last line creates a pointer to hold the address of memory allocated by the 'new' operator and then you need to specify the type (so that the compiler knows the size of the type) and in the brackets the number of elements of that type to allocate. When allocating with new[], deallocate using delete[] and the same thing if you allocate with new, you'll want to use delete.
Desolation is offline   Reply With Quote
Old 08-24-2006, 02:43 PM   #3
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
Yes, use dynamic memory allocation.

Code:
int size, *array;

cout << "How many? ";
cin >> size;

array = new int[size];

for(int x = 0; x < size; x ++) {
    cout << "Enter number " << x << ": ";
    cin >> array[x];
}

for(int x = 0; x < size; x ++) {
    cout << array[x] << endl;
}

delete [] array;  // Don't forget to free the memory
[edit] Two minutes too late . . . [/edit]
__________________
dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell


Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net

My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
dwks is offline   Reply With Quote
Old 08-24-2006, 03:12 PM   #4
Registered User
 
Join Date: Aug 2005
Posts: 1,265
I think C99 standard allows this
Code:
	cin >> n;
	int salaryarray[ n ];
or, since this is a c++ program, you might use a vector
Code:
	cin >> n;
	vector<int> salaryarray(n);
Ancient Dragon is offline   Reply With Quote
Old 08-24-2006, 03:17 PM   #5
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
Yes, the C99 standard allows variable-sized arrays.

[edit]
Code:
cout << "Please enter the number of salespeople./n";
BTW, I think you meant "\n". [/edit]
__________________
dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell


Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net

My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.

Last edited by dwks; 08-24-2006 at 03:39 PM.
dwks is offline   Reply With Quote
Old 08-24-2006, 03:19 PM   #6
Registered User
 
Join Date: Jan 2005
Posts: 7,252
In C++ you should really use the standard vector class for these types of things, although if your professor teaches C style arrays you should learn them as well.
Daved is offline   Reply With Quote
Old 08-24-2006, 04:23 PM   #7
Registered User
 
Join Date: May 2006
Posts: 894
For such a small need I think using a vector would be like shooting a fly with a canon.
Desolation is offline   Reply With Quote
Old 08-24-2006, 04:26 PM   #8
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
Not really. A vector doesn't store any pointers for each element (the memory is just a contiguous chunk, like you would get with a dynamically allocated array). And I can't imagine it would impact execution speed very much.
__________________
dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell


Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net

My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
dwks is offline   Reply With Quote
Old 08-24-2006, 04:29 PM   #9
Registered User
 
Join Date: Jan 2005
Posts: 7,252
>> For such a small need I think using a vector would be like shooting a fly with a canon.
It's your opinion, but why are you even using C++ then?
Daved is offline   Reply With Quote
Old 08-24-2006, 04:41 PM   #10
(?<!re)tired
 
Mario F.'s Avatar
 
Join Date: May 2006
Location: Portugal
Posts: 5,656
>> For such a small need I think using a vector would be like shooting a fly with a canon.

Or using an array would be like trimming your nails with a butchers knife. And I do echo Daved's question.
__________________
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.


Mario F. is offline   Reply With Quote
Old 08-24-2006, 05:09 PM   #11
System Novice
 
siavoshkc's Avatar
 
Join Date: Jan 2006
Location: Tehran
Posts: 1,075
Quote:
For such a small need I think using a vector would be like shooting a fly with a canon.
Why you think so?
__________________
Microsoft Visual Studio 2008 Professional (On Microsoft Windows XP SP2)

Learn the language before using it. (C++ Books and C Books)
Read the FAQ before making a problem.
Then make a Google and Forum search.

My code painter new version Version 0.97 DOWNLOAD NOW! (Let the pop up, pop!)

SiavoshKC
siavoshkc is offline   Reply With Quote
Old 08-24-2006, 05:12 PM   #12
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
Because a vector is slower and uses more memory than an array. But its advantages far outweigh its disadvantages. (We're using C++, right?)
__________________
dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell


Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net

My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
dwks is offline   Reply With Quote
Old 08-24-2006, 05:15 PM   #13
Cat
Registered User
 
Join Date: May 2003
Posts: 1,199
Quote:
Originally Posted by Desolation
For such a small need I think using a vector would be like shooting a fly with a canon.
I tend to disagree. Using pointers in general requires careful planning to avoid memory leaks. Even a simple piece of code like this:

Code:
int * a = new int[10];
int * b = new int[10];
delete[] a;
delete[] b;
has one possible scenario where it leaks memory.
__________________
You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.
Cat is offline   Reply With Quote
Old 08-24-2006, 05:16 PM   #14
Registered User
 
Join Date: Jan 2005
Posts: 7,252
>> Because a vector is slower and uses more memory than an array.
That's not a sufficient reason (even if it were true), unless you would say using C++ instead of C, or C instead of assembly is like shooting a fly with a cannon.
Daved is offline   Reply With Quote
Old 08-24-2006, 05:16 PM   #15
System Novice
 
siavoshkc's Avatar
 
Join Date: Jan 2006
Location: Tehran
Posts: 1,075
Vector is slower than new/delete? Why?
__________________
Microsoft Visual Studio 2008 Professional (On Microsoft Windows XP SP2)

Learn the language before using it. (C++ Books and C Books)
Read the FAQ before making a problem.
Then make a Google and Forum search.

My code painter new version Version 0.97 DOWNLOAD NOW! (Let the pop up, pop!)

SiavoshKC
siavoshkc is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
displaying user input values of an array by calling functions... PLEASE HELP ME!!! hiddenprophecy C Programming 12 04-12-2009 05:49 PM
timed user input sainiabhishek C Programming 4 04-01-2009 11:59 AM
Truncating user input CS_Student8337 C Programming 10 03-19-2009 12:34 AM
MFC: need varying number of user input files - best interface? BrianK Windows Programming 4 04-21-2004 04:18 PM
problem: reading user input into an array alpha561 C Programming 13 05-24-2002 07:23 PM


All times are GMT -6. The time now is 02:42 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22