While your below code will technically work as presented, it will not work
this way if you extended it to, say, need to print out more than two of
these structs.
The reason it works is because "phone" is a global identifier, meaning that
the print() function can see it and use it. However, it you were to make
phone[] a local variable to main() (see below), it would no longer be seen
in print(). This is why there is a way to pass in arguments to the
function.
The "x" stuff in your original question is just that -- an argument for the
function itself. It isn't seen as an error as it is technically allowed to
not have a variable name for an argument -- incase you never need to use it.
Here's a modified version of your code that may help:
#include <string>
#include <iostream>
struct phonerec
{
string fname;
string lname;
}; // Don't need a global here -- we'll use a local in main() instead.
using namespace std;
// While the original was completely correct, giving a desciptive
// variable name for arguments is helpful for IDEs that use
// prototypes to remind you what a function expects (VC++
// comes to mind, but I know others do too)
void print(struct phonerec theRecord);
int main()
{
phonerec data[5]; // Heres our local data. Also, to make it more
interesting, we'll have 5.
int x;
x = 0;
// The collection stuff is nearly identical to yours, except I modified it
to collect 5 of them.
// The reason I used < is because I find it "cleaner" to do it this way,
instead of <=4,
// because the second can make the actual number seem misconstrued (I see
it
// atleast once a day from some of the first-year programmers I teach)
while(x<5)
{
cin >> phone[x].fname;
cin >> phone[x].lname;
x++;
}
// Here's where things go a bit different. Instead of calling print with
the entire list,
// print() will be modified to take in single entry and print it out.
So, it's now up to main()
// to loop through them.
x = 0; // always remember to reset your 'x'!
while (x < 5)
{
print(data[x]); // send the 'x'th element in the array to print().
}
return 0;
}
void print(struct phonerec theRecord) // Note: you don't have to name the
argument the same as the prototype, but descriptive names are nice here,
too.
{
cout << theRecord.fname<<" "<< theRecord.lname<<endl;
}
If you have any more questions don't hesitate to ask :)
- Alex
"kazack" <ka****@talon.net> wrote in message
news:wY********************@news1.epix.net...
Okay here is my code for a simple store first name last name of 2 people
passing it to a function to print both records to the screen. Please let
me know what is wrong with this is anything. The code works with no
problem but is it the proper way of doing it?
Also must a structure be defined outside of main?
The books do not cover that but that is where it is showing all defined
structures at. I did try moving the structure into main with the
following code and got like 6 different compile errors. So I just wanted to know
for sure if this is not the case how would you modify the code to work while
putting a structue inside of main. From everything I was taught so far
you want as little as possible outside of main so that the scope is not
global.
#include <string>
#include <iostream>
using namespace std;
void print(struct phonerec);
struct phonerec
{
string fname;
string lname;
}phone[2];
int main()
{
int x;
x = 0;
while(x<=1)
{
cin >> phone[x].fname;
cin >> phone[x].lname;
x++;
}
print(phone[2]);
return 0;
}
void print(struct phonerec)
{
int x = 0;
while (x<=1)
{
cout << phone[x].fname<<" "<<phone[x].lname<<endl;
x++;
}
}
Thank You for your help and understanding once again,
Shawn Mulligan
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (
http://www.grisoft.com).
Version: 6.0.536 / Virus Database: 331 - Release Date: 11/3/2003