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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TaiwanNoWhere

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

    #include<iostre am>

    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 >>NumberOfTas k;

    // 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 :)
  • Kevin Goodsell

    #2
    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.

    Comment

    • TaiwanNoWhere

      #3
      Re: Why can't I pass by value in this case??

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

      Comment

      • WW

        #4
        Re: Why can't I pass by value in this case??

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

        Loose? When, where?

        --
        WW aka Attila


        Comment

        • David B. Held

          #5
          Re: Why can't I pass by value in this case??

          "TaiwanNoWh ere" <rudyliang@hotm ail.com> wrote in message
          news:d8b9947c.0 310060645.21561 6a7@posting.goo gle.com...[color=blue]
          > The problem is Schedule.TaskNu mber 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


          Comment

          • Jonathan Mcdougall

            #6
            Re: Why can't I pass by value in this case??

            > #include<iostre am>[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 >>NumberOfTas k;[/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


            Comment

            • David B. Held

              #7
              Re: Why can't I pass by value in this case??

              "Jonathan Mcdougall" <jonathanmcdoug all@DELyahoo.ca > wrote in message
              news:5pjgb.5092 0$282.698849@we ber.videotron.n et...[color=blue][color=green]
              > > #include<iostre am>
              > >
              > > 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


              Comment

              • David B. Held

                #8
                Re: Why can't I pass by value in this case??

                "Jonathan Mcdougall" <jonathanmcdoug all@DELyahoo.ca > wrote in message
                news:5pjgb.5092 0$282.698849@we ber.videotron.n et...[color=blue][color=green]
                > > #include<iostre am>
                > >
                > > 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


                Comment

                • Jonathan Mcdougall

                  #9
                  Re: Why can't I pass by value in this case??

                  > > > #include<iostre am>[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


                  Comment

                  • Jack Klein

                    #10
                    Re: Why can't I pass by value in this case??

                    On Mon, 6 Oct 2003 15:04:59 -0400, "Jonathan Mcdougall"
                    <jonathanmcdoug all@DELyahoo.ca > wrote in comp.lang.c++:
                    [color=blue][color=green]
                    > > #include<iostre am>
                    > >
                    > > 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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

                    Comment

                    • Jonathan Mcdougall

                      #11
                      Re: Why can't I pass by value in this case??

                      > > > #include<iostre am>[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


                      Comment

                      • TaiwanNoWhere

                        #12
                        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.

                        Comment

                        Working...