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

T

TaiwanNoWhere

#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.TaskNumber= TaskNumber;
cout << Schedule.TaskNumber << endl;

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

}

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

// arrange in order of length of task

return 0;
}


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

Kevin Goodsell

TaiwanNoWhere wrote:

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

Uh, what's the question?

-Kevin
 
T

TaiwanNoWhere

The problem is Schedule.TaskNumber will lose the value which I
assigned by ++TaskNumber
 
D

David B. Held

TaiwanNoWhere said:
The problem is Schedule.TaskNumber will lose the value
which I assigned by ++TaskNumber

That's because union members *share* storage (but not
dues). When you assign to Schedule.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
 
J

Jonathan Mcdougall

#include said:
using namespace std;

Are you sure you need that ?
int main(void){

(void) is considered bad style in C++

int main()
{
union Task{
int TaskNumber;
float Length;
};

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.
// variables
static int TaskNumber= 0;

What's the static for ? It is illegal to call main()
multiple times.
const int SIZE = 100;
int NumberOfTask = 0;
Task Schedule[SIZE];


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

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.
// ask for input
cout<<"Please enter the length of tasks (mins):"<<endl;
for(int i=0; i<NumberOfTask; i++)

Prefer ++i when you don't need the return value.
{
int Length = 0;

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


TaskNumber is used here, so Length is invalid.
cout << Schedule.TaskNumber << endl;

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


Length is used here so TaskNumber is invalid.
cout << "TaskNumber " << TaskNumber << endl;
cout << "Schedule.TaskNumber " << Schedule.TaskNumber << endl;

}

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


Length was used the last, so TaskNumber prints garbage.
How can I solve this?

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


Jonathan
 
D

David B. Held

Jonathan Mcdougall said:
Are you sure you need that ?

Does it matter if he needs it?
(void) is considered bad style in C++

int main()
{

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++.
[...]
What's the static for ? It is illegal to call main() multiple
times.
[...]

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

Dave
 
D

David B. Held

Jonathan Mcdougall said:
Are you sure you need that ?

Does it matter if he needs it?
(void) is considered bad style in C++

int main()
{

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++.
[...]
What's the static for ? It is illegal to call main() multiple
times.
[...]

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

Dave
 
J

Jonathan Mcdougall

#include said:
Does it matter if he needs it?

Of course. What is your point?
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++.

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

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

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
 
J

Jonathan Mcdougall

#include said:

The C++ community in general.
"style" is a matter of opinion.

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

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


Jonathan
 
T

TaiwanNoWhere

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top