Connecting Tech Pros Worldwide Help | Site Map

Why can't I pass by value in this case??

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 06:48 PM
TaiwanNoWhere
Guest
 
Posts: n/a
Default Why can't I pass by value in this case??

#include<iostream>

using namespace std;

int main(void){

union Task{
int TaskNumber;
float Length;
};

// variables
static int TaskNumber= 0;
const int SIZE = 100;
int NumberOfTask = 0;
Task Schedule[SIZE];


cout<<"How many tasks are there?";
cin >>NumberOfTask;

// ask for input
cout<<"Please enter the length of tasks (mins):"<<endl;
for(int i=0; i<NumberOfTask; i++)
{
int Length = 0;

// Assign task number
TaskNumber++;
cout << TaskNumber << endl;
Schedule[i].TaskNumber= TaskNumber;
cout << Schedule[i].TaskNumber << endl;

// request for the length of each task
cout<<"Number "<< Schedule[i].TaskNumber <<" task takes :";
cin >>Length; // note: ensure length of time within 240
Schedule[i].Length= Length;
cout << "TaskNumber " << TaskNumber << endl;
cout << "Schedule[i].TaskNumber " << Schedule[i].TaskNumber << endl;

}

// display user's input
for(int i=0; i<NumberOfTask; i++)
{
cout <<"Number "<< Schedule[i].TaskNumber <<" task takes "
<< Schedule[i].Length <<" minutes."<< endl;
}

// arrange in order of length of task

return 0;
}


//---------------------------------------------------//
How can I solve this?
Please help, thanks :)

  #2  
Old July 19th, 2005, 06:48 PM
Kevin Goodsell
Guest
 
Posts: n/a
Default Re: Why can't I pass by value in this case??

TaiwanNoWhere wrote:

<snip code>
[color=blue]
>
>
> //---------------------------------------------------//
> How can I solve this?
> Please help, thanks :)[/color]

Uh, what's the question?

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

  #3  
Old July 19th, 2005, 06:55 PM
TaiwanNoWhere
Guest
 
Posts: n/a
Default Re: Why can't I pass by value in this case??

The problem is Schedule.TaskNumber will lose the value which I
assigned by ++TaskNumber
  #4  
Old July 19th, 2005, 06:55 PM
WW
Guest
 
Posts: n/a
Default Re: Why can't I pass by value in this case??

TaiwanNoWhere wrote:[color=blue]
> The problem is Schedule.TaskNumber will lose the value which I
> assigned by ++TaskNumber[/color]

Loose? When, where?

--
WW aka Attila


  #5  
Old July 19th, 2005, 06:55 PM
David B. Held
Guest
 
Posts: n/a
Default Re: Why can't I pass by value in this case??

"TaiwanNoWhere" <rudyliang@hotmail.com> wrote in message
news:d8b9947c.0310060645.215616a7@posting.google.c om...[color=blue]
> The problem is Schedule.TaskNumber will lose the value
> which I assigned by ++TaskNumber[/color]

That's because union members *share* storage (but not
dues). When you assign to Schedule[i].Length, you are
*overwriting* the storage used by the other members of
the union, and thus their values are no longer valid. It
looks to me like you want Task to be a struct, not a union.
Also, a static var declared inside main() is kinda funny,
since I believe main() is not re-entrant in C++. Then
again, you wrote "main(void)", so maybe it's supposed to
be a C program.

It really helps if you format your code nicely. Namely, use
proper indenting so loop structures and data structures
are obvious. Left-justified code is rarely read, hence
part of your difficulty in getting a useful response.

Dave



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003


  #6  
Old July 19th, 2005, 06:56 PM
Jonathan Mcdougall
Guest
 
Posts: n/a
Default Re: Why can't I pass by value in this case??

> #include<iostream>[color=blue]
>
> using namespace std;[/color]

Are you sure you need that ?
[color=blue]
> int main(void){[/color]

(void) is considered bad style in C++

int main()
{
[color=blue]
> union Task{
> int TaskNumber;
> float Length;
> };[/color]

Yurk. Why do you use a union? BTW, this is the source
of your problem. A union shares the memory between its
members so you just cannot use them at the same time.
[color=blue]
> // variables
> static int TaskNumber= 0;[/color]

What's the static for ? It is illegal to call main()
multiple times.
[color=blue]
> const int SIZE = 100;
> int NumberOfTask = 0;
> Task Schedule[SIZE];
>
>
> cout<<"How many tasks are there?";
> cin >>NumberOfTask;[/color]

What if the user enters 200 ? Did you consider
using std::vector ? Think : if I enter 3 tasks, there
is 97 elements wasted in 'Schedule'. If I enter 200,
there is a _big_ problem.

Look up std::vector in your book.
[color=blue]
> // ask for input
> cout<<"Please enter the length of tasks (mins):"<<endl;
> for(int i=0; i<NumberOfTask; i++)[/color]

Prefer ++i when you don't need the return value.
[color=blue]
> {
> int Length = 0;
>
> // Assign task number
> TaskNumber++;
> cout << TaskNumber << endl;
> Schedule[i].TaskNumber= TaskNumber;[/color]

TaskNumber is used here, so Length is invalid.
[color=blue]
> cout << Schedule[i].TaskNumber << endl;
>
> // request for the length of each task
> cout<<"Number "<< Schedule[i].TaskNumber <<" task takes :";
> cin >>Length; // note: ensure length of time within 240
> Schedule[i].Length= Length;[/color]

Length is used here so TaskNumber is invalid.
[color=blue]
> cout << "TaskNumber " << TaskNumber << endl;
> cout << "Schedule[i].TaskNumber " << Schedule[i].TaskNumber << endl;
>
> }
>
> // display user's input
> for(int i=0; i<NumberOfTask; i++)
> {
> cout <<"Number "<< Schedule[i].TaskNumber <<" task takes "
> << Schedule[i].Length <<" minutes."<< endl;[/color]

Length was used the last, so TaskNumber prints garbage.
[color=blue]
> }[/color]
[color=blue]
> How can I solve this?[/color]

Don't use a union, use a struct or a class.


Jonathan


  #7  
Old July 19th, 2005, 06:56 PM
David B. Held
Guest
 
Posts: n/a
Default Re: Why can't I pass by value in this case??

"Jonathan Mcdougall" <jonathanmcdougall@DELyahoo.ca> wrote in message
news:5pjgb.50920$282.698849@weber.videotron.net...[color=blue][color=green]
> > #include<iostream>
> >
> > using namespace std;[/color]
>
> Are you sure you need that ?[/color]

Does it matter if he needs it?
[color=blue][color=green]
> > int main(void){[/color]
>
> (void) is considered bad style in C++
>
> int main()
> {[/color]

The only thing that suprises me here is that you didn't go all
the way and say that K&R style braces are also bad style in
C++.
[color=blue]
> [...]
> What's the static for ? It is illegal to call main() multiple
> times.
> [...][/color]

It's illegal for the user to call main() one time.

Dave



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003


  #8  
Old July 19th, 2005, 06:56 PM
David B. Held
Guest
 
Posts: n/a
Default Re: Why can't I pass by value in this case??

"Jonathan Mcdougall" <jonathanmcdougall@DELyahoo.ca> wrote in message
news:5pjgb.50920$282.698849@weber.videotron.net...[color=blue][color=green]
> > #include<iostream>
> >
> > using namespace std;[/color]
>
> Are you sure you need that ?[/color]

Does it matter if he needs it?
[color=blue][color=green]
> > int main(void){[/color]
>
> (void) is considered bad style in C++
>
> int main()
> {[/color]

The only thing that suprises me here is that you didn't go all
the way and say that K&R style braces are also bad style in
C++.
[color=blue]
> [...]
> What's the static for ? It is illegal to call main() multiple
> times.
> [...][/color]

It's illegal for the user to call main() one time.

Dave



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003


  #9  
Old July 19th, 2005, 06:56 PM
Jonathan Mcdougall
Guest
 
Posts: n/a
Default Re: Why can't I pass by value in this case??

> > > #include<iostream>[color=blue][color=green][color=darkred]
> > >
> > > using namespace std;[/color]
> >
> > Are you sure you need that ?[/color]
>
> Does it matter if he needs it?[/color]

Of course. What is your point?
[color=blue][color=green][color=darkred]
> > > int main(void){[/color]
> >
> > (void) is considered bad style in C++
> >
> > int main()
> > {[/color]
>
> The only thing that suprises me here is that you didn't go all
> the way and say that K&R style braces are also bad style in
> C++.[/color]

Braces are a matter of taste. Putting (void) for an empty
parameter list is bad style in C++, except for C compatibility.
[color=blue][color=green]
> > [...]
> > What's the static for ? It is illegal to call main() multiple
> > times.
> > [...][/color]
>
> It's illegal for the user to call main() one time.[/color]

Yes, but this is not what I said. It is illegal to call main()
multiple time, since main() is called once by the operating
system.


Jonathan


  #10  
Old July 19th, 2005, 06:56 PM
Jack Klein
Guest
 
Posts: n/a
Default Re: Why can't I pass by value in this case??

On Mon, 6 Oct 2003 15:04:59 -0400, "Jonathan Mcdougall"
<jonathanmcdougall@DELyahoo.ca> wrote in comp.lang.c++:
[color=blue][color=green]
> > #include<iostream>
> >
> > using namespace std;[/color]
>
> Are you sure you need that ?
>[color=green]
> > int main(void){[/color]
>
> (void) is considered bad style in C++[/color]

By whom? "style" is a matter of opinion. Can you cite a link to the
C++ standard where this is either deprecated or marked obsolescent?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
  #11  
Old July 19th, 2005, 06:56 PM
Jonathan Mcdougall
Guest
 
Posts: n/a
Default Re: Why can't I pass by value in this case??

> > > #include<iostream>[color=blue][color=green][color=darkred]
> > >
> > > using namespace std;[/color]
> >
> > Are you sure you need that ?
> >[color=darkred]
> > > int main(void){[/color]
> >
> > (void) is considered bad style in C++[/color]
>
> By whom?[/color]

The C++ community in general.
[color=blue]
>"style" is a matter of opinion.[/color]

I would rather say that "style" is a mix of taste and
language-specific features and constraints.
[color=blue]
> Can you cite a link to the
> C++ standard where this is either deprecated or marked obsolescent?[/color]

No, that is why I asked if it was deprecated or not in another post
("void and this" by Vladimir Grul).


Jonathan


  #12  
Old July 19th, 2005, 07:11 PM
TaiwanNoWhere
Guest
 
Posts: n/a
Default Re: Why can't I pass by value in this case??

Thanks everyone's response.
After I copy and paste the codes here, the format is lost.:(
Therefore, sorry for my codes.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.