home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2977
Discussions: 188,378, Posts: 2,243,450, Members: 232,606
Old October 8th, 2002, 05:50 PM   Digg it!   #1 (permalink)
Best To Avoid Me
 
Martoch's Avatar
 
Join Date: Mar 2002
Location: Under Your Bed
Posts: 8,596
Load an array w/ a do/while loop

OK!!

I just got done w/ a C++ class, and the only way I was taught to load an array is with a FOR loop.

How can you load an array with an do/while loop.

EDIT: That was Lambo!!!
He thought he was still logged into this computer, but I was so he posted in my name.

However, feel free to answer his question as he would still like some input!

I PROMISE you we are two different people...I'm not crazy, Lambo is.

__________________
~ Camp Crystal Lake counselor positions opening daily ~


Last edited by Martoch : October 8th, 2002 at 05:52 PM.
Martoch is online now   Reply With Quote
Old October 8th, 2002, 06:48 PM     #2 (permalink)
Member
 
Join Date: Feb 2002
Posts: 161
Sample which loads an array with the values 1 through 10:

int myArray[10];
int currentIndex = 0;

do
{
myArray[currentIndex] = currentIndex + 1;
++currentIndex;
}while(currentIndex < 10);

Martee is offline   Reply With Quote
Old October 8th, 2002, 08:09 PM     #3 (permalink)
Member
 
Join Date: Sep 2002
Posts: 43
This is the real Lambo, or is it?

The reason I ask is that, he wanted us to create a club member file using structures.

How would you pass a struct array to a function, and within in that function accept user input from a keyboard and load that input to an array? for example

struct club{
int age;
char name[20];
doubl balance
};

main()
{
club members[30];

create (members)


return 0;
}

void create ( club * const member): would I take what you suggested and put that in this function?

Lambo is offline   Reply With Quote
Old October 9th, 2002, 04:19 PM     #4 (permalink)
ILC
Senior Member
 
ILC's Avatar
 
Join Date: Oct 2001
Location: Eastern Shore
Posts: 701
Why would you not want to use a for loop? Are you mandated to use a do/while in this program?

Anyways:

int i=0;

do
{
members[i].age=
members[i].name=//however you are inputting strings
members[i].balance=
i++;
}while(i<20);

Hope that helps some.

ILC

Last edited by ILC : October 9th, 2002 at 04:21 PM.
ILC is offline   Reply With Quote
Old October 9th, 2002, 05:45 PM     #5 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
Join Date: Oct 2001
Posts: 1,542
For the record,
Code:
for (initialisation; condition; increment) {
    body;
}
is effectively the same as
Code:
initialisation;
while (condition) {
    body;
    increment;
}
Note that a do-while loop always runs (evaluates the body statement/s) at least once, while a for loop and a while loop may not run at all (although of course both will evaluate the condition statement and the for loop will always evaluate the initialisation statement!)
strangerstill is offline   Reply With Quote
Old October 10th, 2002, 12:24 AM     #6 (permalink)
Member
 
Join Date: Sep 2002
Posts: 43
I wasn't mandated to use a for loop, but the for loop was loading garbage after user input from the keyboard had ended with a EOF. This is how I was trying to do it at first.

(struct type) club members[30];
create (members); //calling create function

void create (club * const member )
{
cout << "Enter the age, name, and balance.\n" << "Enter end-of-file to end input.\n? ";

for ( int i = 0; i < 30; i++){
while(cin >> (member[i]).age >> (member[i]).name >> (member[i}).balance) // Input data from the Keyboard.
{
cout <<"? ";
}

} After the for loop didn't work , I out of desperation from not knowing how to load an array any other way, started using a pointer and got nowhere. Thanks so much for showing me how to load an array with an do/while loop. I'm trying to learn this stuff. I really do appreciate it.
Lambo is offline   Reply With Quote
Old October 14th, 2002, 09:02 PM     #7 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
Join Date: Oct 2001
Posts: 1,542
OK, stripping unnecessary parentheses, and me-prettifying the layout, we have:
Code:
void create (club * const member ) {
	cout << "Enter the age, name, and balance.\n";
	cout << "Enter end-of-file to end input.\n";
	cout << "? ";

	for ( int i = 0; i < 30; i++) {
		while(cin >> member[i].age >> member[i].name >> member[i].balance) {
			cout << "? ";
		}
	}
}
Well, this sure won't work - execution stays within the while loop, so i is never incremented until cin gets an EOF and returns false, but then the for loops round and the while loop is run again!
The quick fix for the above is this:
Code:
void create (club * const member ) {
	cout << "Enter the age, name, and balance.\n";
	cout << "Enter end-of-file to end input.\n";
	cout << "? ";

	for ( int i = 0; i < 30 && cin >> member[i].age >> member[i].name >> member[i].balance; i++) {
		cout << "? ";
	}
}
strangerstill is offline   Reply With Quote
Reply


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

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Most Active Discussions
Is It Just Me? (2883)
The United States Debt (20)
Looks like Burris will get his Sena.. (7)
I think I just killed my computer w.. (24)
Upgrading RAM (5)
Folderchat Weekday thread (439)
Antec 300 bulk purchase? (11)
Worth the upgrade?? (14)
Titan quest and Immortal Throne, an.. (17)
Recent Discussions
dual monitors wont boot (0)
Folderchat Weekday thread (439)
New Build ( Finally ) (0)
MSN Hotmail Down??? (7)
Help with an Ati Radeon HD 4850.. (23)
Laptop waking up itself (0)
CPU wont boot (3)
Best digital camera for under 2.. (13)
Building first computer, will t.. (2)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 08:13 PM.
TechIMO Copyright 2008 All Enthusiast, Inc.



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 26 27 28