October 8th, 2002, 05:50 PM
|
#1 (permalink)
| | Best To Avoid Me
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.
|
| |
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); |
| |
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? |
| |
October 9th, 2002, 04:19 PM
|
#4 (permalink)
| | Senior Member
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.
|
| |
October 9th, 2002, 05:45 PM
|
#5 (permalink)
| | Ultimate Member
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!) |
| |
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. |
| |
October 14th, 2002, 09:02 PM
|
#7 (permalink)
| | Ultimate Member
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 << "? ";
}
} |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |