473,414 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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 :)
Jul 19 '05 #1
11 1646
TaiwanNoWhere wrote:

<snip code>


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


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.

Jul 19 '05 #2
The problem is Schedule.TaskNumber will lose the value which I
assigned by ++TaskNumber
Jul 19 '05 #3
WW
TaiwanNoWhere wrote:
The problem is Schedule.TaskNumber will lose the value which I
assigned by ++TaskNumber


Loose? When, where?

--
WW aka Attila
Jul 19 '05 #4
"TaiwanNoWhere" <ru*******@hotmail.com> wrote in message
news:d8**************************@posting.google.c om...
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[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
Jul 19 '05 #5
> #include<iostream>

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[i].TaskNumber= TaskNumber;
TaskNumber is used here, so Length is invalid.
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;
Length is used here so TaskNumber is invalid.
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;
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
Jul 19 '05 #6
"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message
news:5p********************@weber.videotron.net...
#include<iostream>

using namespace std;
Are you sure you need that ?


Does it matter if he needs it?
int main(void){


(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

---
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
Jul 19 '05 #7
"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message
news:5p********************@weber.videotron.net...
#include<iostream>

using namespace std;
Are you sure you need that ?


Does it matter if he needs it?
int main(void){


(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

---
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
Jul 19 '05 #8
> > > #include<iostream>

using namespace std;


Are you sure you need that ?


Does it matter if he needs it?


Of course. What is your point?
int main(void){


(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++.


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
Jul 19 '05 #9
On Mon, 6 Oct 2003 15:04:59 -0400, "Jonathan Mcdougall"
<jo***************@DELyahoo.ca> wrote in comp.lang.c++:
#include<iostream>

using namespace std;


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


(void) is considered bad style in C++


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
Jul 19 '05 #10
> > > #include<iostream>

using namespace std;
Are you sure you need that ?
int main(void){


(void) is considered bad style in C++


By whom?


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
Jul 19 '05 #11
Thanks everyone's response.
After I copy and paste the codes here, the format is lost.:(
Therefore, sorry for my codes.
Jul 19 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: SB | last post by:
Ok, very simple problem. I'm trying to update a value by calling a function using pass by reference, but it does not update the value. In short, the value I'm trying to update is balance, which is...
18
by: usr.root | last post by:
Is there any differece of this two function: int f1(int first); int f2(int *second);
14
by: Robin Tucker | last post by:
Although I've been working on this project for 8 months now, I'm still not sure of the difference between ByVal and ByRef. As most objects in VB are reference types, passing ByVal I've discovered...
5
by: Just Me | last post by:
Given a button name Btn_5 and Index=5 I want to do something like dim zz as string = Btn_??Index??.Text or given an array of buttons, do:
3
by: D.P. Roberts | last post by:
I have 3 vbscripts and a vb form with radio buttons corresponding to each script. The form's only purpose is to provide a nice GUI for the user to decide which of the 3 scripts to run. Now, because...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
5
by: Mahendra Kumar Kutare | last post by:
I am trying to implement a webserver with boss-worker model thread pool implementation - I have a header declaration threadpool.h as - typedef struct threadpool_work { void (*routine) ();...
14
by: Siegfried Heintze | last post by:
Why does VB.NET V2 force me to pass by value for my set function? When I try to change it to const byref it gives me a syntax error. It seems very inefficient to be passing strings around by value...
4
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.