home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Load an array w/ a do/while loop

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1492
Discussions: 200,990, Posts: 2,379,860, Members: 246,356
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,863
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 offline   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
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (3063)
Charges against non-tippers dropped.. (20)
Delete an OS (17)
Nvidia GTX 260 problem (9)
Laptop with wireless problem. (12)
windows vista security holes (19)
Wireless Televisions. (12)
CPU fan stops spinning randomly (11)
Regular Build (11)
Point and Shoot Camera Suggestions. (8)
windows 7 problem (7)
[F@H SPAM 11/16/09] ! 1/2 months to.. (39)
Internet Lost (5)
Multiple Restarts Required at Boot (5)
Recent Discussions
How to converter MTS Video to HD mpeg.. (0)
Can a page file be "too big".. (0)
Point and Shoot Camera Suggestions. (8)
Looking for new motherboard (0)
Size after cutting 700Mb file is 2.5 .. (0)
Delete an OS (17)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (39)
windows vista security holes (19)
updating BIOS via winflash, claims fi.. (1)
New Server Configuration Suggestions (0)
Desktop Calendar Application (0)
cheap gaming laptop? (12)
Unallocated Space (2)
help me pls laptop just stopped worki.. (1)
C# + LINQ Help (7)
Nvidia GTX 260 problem (9)
Dynex DX E-402 (3)
EVGA 9800 gtx help with finding a goo.. (12)
Multiple Restarts Required at Boot (5)
cell phone won't work (0)
Is the PSU I received dead? (15)
Can't open Word (12)
Steam ID's, Gamertags etc... (4)
Games, Cables, PCI cards, and more fo.. (6)
Dept. of HS: NSA 'Helped' Develop Vis.. (17)


All times are GMT -4. The time now is 05:39 AM.
TechIMO Copyright 2009 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