Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 17th, 2006, 8:30 AM   #1
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 4 n00b is on a distinguished road
Send a message via MSN to n00b
static array...

hello everyone..
first of all lemme just post a few lines of code so that it's easier to explain the "problem".


int main()
{
	int n;
	int index[100];
	float average[100];
	char name[100][20];

	while((n<0) || (n>100))
	{
		cout << "How many students?" << endl;
		cin >> n;

		while(!cin)
		{
			cin.clear();
			cin.ignore(100,'\n');
			cout << "Input the NUMBER of students" << endl;
			cin >> n;
			cin.get();
		}
	}.......


so...my question is regarding the char name variable. let's say if i was using dynamic array..i would first ask for the number of students (cin >> n, and then create a pointer to a variable...

char (*name)[30] = new char[n][30];

but now...since i'm using static array, how do i do that? first defining the variable char name[100][30] (for eg 100 is the number of students), and THEN asking for the number of students doesn't make any sense does it? because...what if i answer that there are 200 students and the array is set to 100 ??


could be a stupid question but i've been using 99% dynamic allocation lately.
n00b is offline   Reply With Quote
Old Feb 17th, 2006, 8:59 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
... and THEN asking for the number of students doesn't make any sense does it?
Only if the number is less than (or equal to) the number you've allocated for. You might want it just to terminate the gathering of input. Personally (individual choice), I'd just go for "Press ENTER on a blank line to terminate entry" and stop input that way, with a safety exit if the number of entries exceeded what I'd allocated for.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC