Re: pass an array throuh a function
From: Leor Zolman (leor_at_bdsoft.com)
Date: 05/26/04
- Previous message: David White: "Re: pass an array throuh a function"
- In reply to: Kenny: "Re: pass an array throuh a function"
- Next in thread: Kenny: "Re: pass an array throuh a function"
- Reply: Kenny: "Re: pass an array throuh a function"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 25 May 2004 21:06:08 -0400
On Tue, 25 May 2004 19:31:44 -0400, "Kenny" <lerameur@yah00.c0m> wrote:
>ok:
>here is my program:
>Basically,
>
>The array would record the data from the user from one function.AND another
>that will display all the user input.
>I want to pass by reference the array so I can take the array,s information
>once the user is done and send it to the display function.
>I believe the class would help me do this through encapsulation.
>But I believe I have a small syntax error somewhere
#1. Please don't top post. To respond to a post, include the text you're
responding to /above/ your response, and please snip all extraneous text
(not relevant to your current response) completely.
#2. Please don't use non-standard headers, such as conio.h. Fortunately,
that's easy to eliminate; just use
std::cin.get();
instead.
#3. You've got more that a "small syntax error" going; you've got some
serious design ambiguity. I'll point out the places things are out of
whack. In order to make it readable, I'll use the result of loading your
code into Epsilon, which automatically indents it sanely. I've wrapped the
long lines manually. My comments are preceded by LZ:
#include <iostream>
using namespace std;
class Setting
{
public:
void SetCylinder(int , int, int, int a[] );
LZ: So what does a "Setting" represent? A "Cylinder"? I still have no
LZ: idea. Are you trying to set the hours, minutes and seconds? If
LZ: so, what would be the point of the array parameter? This is what
LZ: I was asking about before, and I still have no clue why you even
LZ: /want/ to pass in an array, whether by reference, value or any other
LZ: way.
void display();
LZ: A "Setting" has hours, minutes and seconds. Thus I'd expect the
LZ: display() function for "a setting" to display hours, minutes and
LZ: seconds. In fact, your display() function is going through a loop.
LZ: Why? What is it trying to display?
private:
int hours, minutes, seconds;
};
int count;
LZ: What is this 'count' for? You never use it, and there are other
LZ: local incarnations of count. What are you counting? Why?
int main()
{
Setting a[10];
int choice;
int count =0;
LZ: How about /this/ version of count? What are you counting?
for (int i=0; i<5; i++)
LZ: What does the '5' represent? You want to run the menu loop 5 times?
LZ: Why? Menu loops typically just run once, until the user enters 'quit'.
{
do {
cout << endl << endl;
cout << " Time Management System "<<endl;
cout << "========================================== "<<endl;
cout << " 1: Enter a time "<<endl;
cout << " 2: Display time "<<endl;
cout << " 3: Quit "<<endl;
cout << "========================================== "<<endl;
cout << " Your choice please: ";
cin >> choice;
switch (choice)
{
case 1:
a[i].SetCylinder(20,30,40, a[] );
LZ: Is SetCylinder (I sure wish I knew what a cylinder had to do with time
LZ: management) supposed to set the Setting or Cylinder or whatever to the
LZ: values you give (20, 30, 40), or ask for input from the user? How can
LZ: it do BOTH, for heavens' sake? And I still don't get the purpose of
LZ: that array...think about it: you're invoking a member function on a
LZ: single element of the array, a[i], but for some reason you're also
LZ: passing in the /entire/ array (logically, because you can't really
LZ: pass in arrays, as others have pointed out, but WHY do you even WANT
LZ: to???)
break;
case 2:
a[i].display();
LZ: Just when it seems something might make sense, we look at the
LZ: implementation of display(), and instead of displaying just the
LZ: data members of a "Setting", as one would expect, it does that loop
LZ: thing. I don't get it.
break;
case 3: cout<<"Thank you for having used this system"
", Bye Bye!!!"; break;
default: cout<<"Error: Invalid option, Please tryagain" ;
}
} while (choice != 3);
}// end function menu
cin.get();
return 0;
}
void Setting::SetCylinder(int r, int h, int s, int a[])
{
int value;
for (int i=0; i<count; i++)
{
cout<<" Enter the hours: " <<endl;
cin>> value;
a[i].hours = value;
cout<<" Enter minutes: " <<endl;
cin>> value;
a[i].minutes = value;
cout<<" Enter seconds: " <<endl;
cin>> value;
a[i].seconds = value;
count ++;
}
}
LZ: Let me just show a version of SetCylinder that makes sense to me, to be
LZ: consistent with your three data members:
void Setting::SetCylinder(int r, int h, int s)
{
int value;
cout<<" Enter the hours: " <<endl;
cin>> value;
hours = value;
cout<<" Enter minutes: " <<endl;
cin>> value;
minutes = value;
cout<<" Enter seconds: " <<endl;
cin>> value;
seconds = value;
count ++;
}
LZ: Of course, there ought to be sanity checking of the values, but I've
LZ: omitted all that for brevity, in order to focus on the core issue. You
LZ: are setting one object here, not a set of them. At least, that's what
LZ: I /think/ you want to be doing.
void Setting::display()
{
for (int i=0; i<count; i++)
{
cout<<" Here is the array numer: " << i <<endl:
cout<<"Hours: " <<a[i].hours <<endl <<"Minutes: " <<
a[i].minutes <<endl
<<"Seconds: " << a[i].seconds <<endl;
}
}
LZ: Again, this should just display a single item, becoming:
void Setting::display()
{
cout<<" Here is the next setting: " << endl;
cout<<"Hours: " << hours <<endl <<"Minutes: " <<
minutes <<endl
<<"Seconds: " << seconds <<endl;
}
If a "Cylinder" means a "setting", and what you really want is a collection
of settings, then what would work is a class, say Datebook, containing,
say, a vector of Settings as a data member, along with count (unless you
just let the vector tell you how many elements it has), and then it would
make sense for Datebook::display() to have a loop that iterates across the
elements of the vector, calling Setting::display() for each one:
class Setting { ... };
class Datebook
{
public:
...
private:
Setting a[10];
int count;
};
I hope some of this makes sense to you...
-leor
-- Leor Zolman --- BD Software --- www.bdsoft.com On-Site Training in C/C++, Java, Perl and Unix C++ users: download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
- Previous message: David White: "Re: pass an array throuh a function"
- In reply to: Kenny: "Re: pass an array throuh a function"
- Next in thread: Kenny: "Re: pass an array throuh a function"
- Reply: Kenny: "Re: pass an array throuh a function"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|