skipping an element in an array

Discussion in 'C++' started by Ken, May 23, 2004.

  1. Ken

    Ken Guest

    I have a small program , wi tha menu (only option 1,2 and 6 are working
    for now)
    Something weird is happening, my array works to input all the data the user
    puts in, but whebn I choose option 2, display, it always gives me 4202717
    for the element number 1 in the array, While 0,2,3 .. are all ok.
    It seems to be skipping number 1 in the array.
    anyone ?
    problem is in createTime() function

    I am using visual c++
    ken


    #include <iostream>
    #include <conio.h>
    using namespace std;

    class timeChange
    {
    public:
    void createTime();
    void display1();

    private:
    int t1, t2, i, j, a[] ;
    };

    inline void timeChange::createTime()
    {

    if (t1 >0)
    {
    cout<<" Enter a number 1: " <<endl;
    cin>> t2;
    i=0;
    i = i + j;
    do {
    cout<<" before j: " << j <<endl;
    cout<<" a: " << a <<endl;
    cout<<" t2: " << t2 <<endl;
    cout<<" before i: " << i <<endl;
    i++;
    a = t2;
    j = i;
    cout<<" j: " << j <<endl;
    cout<<" a: " << a <<endl;
    cout<<" t2: " << t2 <<endl;
    cout<<" i: " << i <<endl;
    }
    while (t1 < 0);

    }

    else
    {
    cout<<" Enter a number 2: " <<endl;
    cin>> t1;
    i =0;
    do {
    a = t1;
    cout<<" before j: " << j <<endl;
    cout<<" t1: " << t1 <<endl;
    cout<<" i: " << i <<endl;
    cout<<" a: " << a <<endl;
    cout<<" before i++: " << i <<endl;
    i++;
    j = i;

    cout<<" j: " << j <<endl;
    cout<<" t1: " << t1 <<endl;
    cout<<" i: " << i <<endl;
    }
    while (t1 < 0);


    } // end else

    } //end void function
    inline void timeChange::display1()
    {

    cout<<" Here it is: " << t1 <<endl;
    for (int i=0; i<= j; i++)

    {
    cout<<"a: " << a << endl;
    cout<<"i: " << i << endl;
    }

    }

    /*inline void timeChange::diffTime()
    inline void timeChange::substracTime()
    inline void timeChange::addTime()
    inline void timeChange::validateTime()
    */


    void menu() //showing the menu for the 4 options
    { timeChange time;
    int choice;

    do {
    cout << endl << endl;
    cout << " Time Management System "<<endl;
    cout << "============================================== "<<endl;
    cout << " 1: Create a new time object "<<endl;
    cout << " 2: Display all time object "<<endl;
    cout << " 3: Calculate the difference between two times "<<endl;
    cout << " 4: Substrat a number of seconds from a time "<<endl;
    cout << " 5: Add a number of seconds to a time "<<endl;
    cout << " 6: Quit "<<endl;
    cout << "============================================== "<<endl;

    cout << " Your choice please: ";
    cin >> choice;

    switch (choice)
    {
    case 1:
    time.createTime();
    //Calling function to convert seconds into hours
    break;
    case 2:
    time.display1(); //Calling function to convert tome to seconds
    break;
    /* case 3:
    time.diffTime();
    break;
    case 4:
    time.substracTime(); //Calling function to subtract a time from seconds
    break;
    case 5:
    time.addTime(); //Calling function to subtract a time from seconds
    break;
    */ case 6: cout<<"Thank you for having used this system, Bye Bye!!!";
    break;

    default: cout<<"Error: Invalid option, Please try again" ;
    }
    } while (choice != 6);

    }// end function menu


    int main()
    {
    timeChange time;
    menu();


    getch();
    return 0;
    }
    Ken, May 23, 2004
    #1
    1. Advertising

  2. "Ken" <0m> wrote in message
    news:wj8sc.17$...
    > I have a small program , wi tha menu (only option 1,2 and 6 are working
    > for now)
    > Something weird is happening, my array works to input all the data the

    user
    > puts in, but whebn I choose option 2, display, it always gives me 4202717
    > for the element number 1 in the array, While 0,2,3 .. are all ok.
    > It seems to be skipping number 1 in the array.
    > anyone ?
    > problem is in createTime() function
    >
    > I am using visual c++
    > ken
    >
    >
    > #include <iostream>
    > #include <conio.h>
    > using namespace std;
    >
    > class timeChange
    > {
    > public:
    > void createTime();
    > void display1();
    >
    > private:
    > int t1, t2, i, j, a[] ;


    a[] is not legal C++. I've no idea what visual c++ makes of it, but it isn't
    right. If you want to declare an array in C++ you must say how big you want
    it to be.

    > };
    >
    > inline void timeChange::createTime()
    > {
    >
    > if (t1 >0)


    t1 is unintialised at this point

    > {
    > cout<<" Enter a number 1: " <<endl;
    > cin>> t2;
    > i=0;
    > i = i + j;


    j is uninitialised at this point

    > do {
    > cout<<" before j: " << j <<endl;
    > cout<<" a: " << a <<endl;
    > cout<<" t2: " << t2 <<endl;
    > cout<<" before i: " << i <<endl;
    > i++;
    > a = t2;
    > j = i;
    > cout<<" j: " << j <<endl;
    > cout<<" a: " << a <<endl;
    > cout<<" t2: " << t2 <<endl;
    > cout<<" i: " << i <<endl;
    > }
    > while (t1 < 0);
    >
    > }
    >
    > else
    > {
    > cout<<" Enter a number 2: " <<endl;
    > cin>> t1;
    > i =0;
    > do {
    > a = t1;
    > cout<<" before j: " << j <<endl;
    > cout<<" t1: " << t1 <<endl;
    > cout<<" i: " << i <<endl;
    > cout<<" a: " << a <<endl;
    > cout<<" before i++: " << i <<endl;
    > i++;
    > j = i;
    >
    > cout<<" j: " << j <<endl;
    > cout<<" t1: " << t1 <<endl;
    > cout<<" i: " << i <<endl;
    > }
    > while (t1 < 0);
    >
    >
    > } // end else
    >
    > } //end void function


    You shouldn't use variables before you have given them values.

    john
    John Harrison, May 23, 2004
    #2
    1. Advertising

  3. Ken schrieb:
    > problem is in createTime() function
    >
    > [...]
    >
    > class timeChange
    > {
    > public:
    > void createTime();
    > void display1();
    >
    > private:
    > int t1, t2, i, j, a[] ;
    > };
    >
    > inline void timeChange::createTime()
    > {
    >
    > if (t1 >0)


    First, you should initialize the variables of the class before using them.
    Write a constructor and set t1,t2,i,j to zero or whatever you like.
    If you don't do so, the variables contain "random" numbers.

    Second, "int a[]" is nothing. It is a declaration of an int-array with zero
    elements. It does not grow automaticaly when you access an index that is out
    of bound.

    Maybe you should read a good book about C++ programming or search the web
    for an online tutorial.

    --
    Thomas

    http://www.draig.de/LinkBar/
    Thomas J. Gritzan, May 23, 2004
    #3
  4. Ken

    Ken Guest

    t1 is unintialised at this point

    > {
    > cout<<" Enter a number 1: " <<endl;
    > cin>> t2;
    > i=0;
    > i = i + j;


    j is uninitialised at this point


    THOSE HAS BEEN initialized in the private part of the class.
    Second, if I give them a value at the beginning of the function, how can
    my incrementation work ???


    ken

    "John Harrison" <> wrote in message
    news:...
    >
    > "Ken" <0m> wrote in message
    > news:wj8sc.17$...
    > > I have a small program , wi tha menu (only option 1,2 and 6 are

    working
    > > for now)
    > > Something weird is happening, my array works to input all the data the

    > user
    > > puts in, but whebn I choose option 2, display, it always gives me

    4202717
    > > for the element number 1 in the array, While 0,2,3 .. are all ok.
    > > It seems to be skipping number 1 in the array.
    > > anyone ?
    > > problem is in createTime() function
    > >
    > > I am using visual c++
    > > ken
    > >
    > >
    > > #include <iostream>
    > > #include <conio.h>
    > > using namespace std;
    > >
    > > class timeChange
    > > {
    > > public:
    > > void createTime();
    > > void display1();
    > >
    > > private:
    > > int t1, t2, i, j, a[] ;

    >
    > a[] is not legal C++. I've no idea what visual c++ makes of it, but it

    isn't
    > right. If you want to declare an array in C++ you must say how big you

    want
    > it to be.
    >
    > > };
    > >
    > > inline void timeChange::createTime()
    > > {
    > >
    > > if (t1 >0)

    >
    > t1 is unintialised at this point
    >
    > > {
    > > cout<<" Enter a number 1: " <<endl;
    > > cin>> t2;
    > > i=0;
    > > i = i + j;

    >
    > j is uninitialised at this point
    >
    > > do {
    > > cout<<" before j: " << j <<endl;
    > > cout<<" a: " << a <<endl;
    > > cout<<" t2: " << t2 <<endl;
    > > cout<<" before i: " << i <<endl;
    > > i++;
    > > a = t2;
    > > j = i;
    > > cout<<" j: " << j <<endl;
    > > cout<<" a: " << a <<endl;
    > > cout<<" t2: " << t2 <<endl;
    > > cout<<" i: " << i <<endl;
    > > }
    > > while (t1 < 0);
    > >
    > > }
    > >
    > > else
    > > {
    > > cout<<" Enter a number 2: " <<endl;
    > > cin>> t1;
    > > i =0;
    > > do {
    > > a = t1;
    > > cout<<" before j: " << j <<endl;
    > > cout<<" t1: " << t1 <<endl;
    > > cout<<" i: " << i <<endl;
    > > cout<<" a: " << a <<endl;
    > > cout<<" before i++: " << i <<endl;
    > > i++;
    > > j = i;
    > >
    > > cout<<" j: " << j <<endl;
    > > cout<<" t1: " << t1 <<endl;
    > > cout<<" i: " << i <<endl;
    > > }
    > > while (t1 < 0);
    > >
    > >
    > > } // end else
    > >
    > > } //end void function

    >
    > You shouldn't use variables before you have given them values.
    >
    > john
    >
    >
    Ken, May 24, 2004
    #4
  5. "Ken" <0m> wrote in message
    news:izasc.19$...
    > t1 is unintialised at this point
    >
    > > {
    > > cout<<" Enter a number 1: " <<endl;
    > > cin>> t2;
    > > i=0;
    > > i = i + j;

    >
    > j is uninitialised at this point
    >
    >
    > THOSE HAS BEEN initialized in the private part of the class.


    No they haven't.

    > Second, if I give them a value at the beginning of the function, how can
    > my incrementation work ???
    >


    You write a constructor, and give them a value there. I think you need to
    buy a book on C++ and do some studying.

    john
    John Harrison, May 24, 2004
    #5
  6. > >
    > > THOSE HAS BEEN initialized in the private part of the class.

    >
    > No they haven't.
    >


    Maybe you are confusing declaration with initialisation. They have been
    declared, they have not been initialised.

    john
    John Harrison, May 24, 2004
    #6
  7. Ken

    Ken Guest

    crap, I am mixing those two, I will try that


    ken


    "John Harrison" <> wrote in message
    news:...
    > > >
    > > > THOSE HAS BEEN initialized in the private part of the class.

    > >
    > > No they haven't.
    > >

    >
    > Maybe you are confusing declaration with initialisation. They have been
    > declared, they have not been initialised.
    >
    > john
    >
    >
    Ken, May 24, 2004
    #7
  8. Ken

    Ken Guest

    I did bought a book recently on the subject.
    But all the books and tutorial I have seen so far always deals with data
    already inserted in the programs wich do not ask anything from the user.
    The data I want to treat would be in a >>cin , so I do not see how I can
    initialized those values in a constructor.




    "John Harrison" <> wrote in message
    news:...
    > > >
    > > > THOSE HAS BEEN initialized in the private part of the class.

    > >
    > > No they haven't.
    > >

    >
    > Maybe you are confusing declaration with initialisation. They have been
    > declared, they have not been initialised.
    >
    > john
    >
    >
    Ken, May 24, 2004
    #8
  9. "Ken" <0m> wrote in message
    news:xrosc.43$...
    > I did bought a book recently on the subject.
    > But all the books and tutorial I have seen so far always deals with data
    > already inserted in the programs wich do not ask anything from the user.
    > The data I want to treat would be in a >>cin , so I do not see how I

    can
    > initialized those values in a constructor.
    >


    It doesn't change the fact that you are using t1 and j before you have
    assigned them values.

    Maybe you don't want to give them values in a constructor but you have to
    given them values *somewhere* before you start using them.

    john
    John Harrison, May 24, 2004
    #9
  10. Ken wrote:
    >
    > I did bought a book recently on the subject.
    > But all the books and tutorial I have seen so far always deals with data
    > already inserted in the programs wich do not ask anything from the user.
    > The data I want to treat would be in a >>cin , so I do not see how I can
    > initialized those values in a constructor.
    >


    Then ask yourself.
    In (code posted by yourself)

    #include <iostream>
    #include <conio.h>
    using namespace std;

    class timeChange
    {
    public:
    void createTime();
    void display1();

    private:
    int t1, t2, i, j, a[] ;
    };

    inline void timeChange::createTime()
    {

    if (t1 >0)
    {
    cout<<" Enter a number 1: " <<endl;
    cin>> t2;
    i=0;
    i = i + j;

    What is the value of t1 right after the function starts executing?
    Nobody knows, it hasn't been given a value right now. Yet you
    use that variable in the comparison in the very first statement
    in this function.

    Even if some of the values are enterd by the user, it is *still*
    a good idea to use the help of a constructor to give them
    some defined values.

    class timeChange
    {
    public:
    timeChange() { t1 = 0;
    t2 = 0;
    i = 0;
    j = 0;
    }

    ....

    so that whenever a timeChange object comes into existence, it is
    *guaranteed* that those member variables have some defined values.
    Now when function createTime starts execution, we *know* that t1
    has a value of 0!

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, May 24, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HANM
    Replies:
    2
    Views:
    716
    Joseph Kesselman
    Jan 29, 2008
  2. Szabolcs Borsanyi
    Replies:
    6
    Views:
    664
    Ben Bacarisse
    May 23, 2008
  3. Chris R.
    Replies:
    3
    Views:
    137
    Adam Prescott
    Jan 28, 2011
  4. Tad McClellan
    Replies:
    3
    Views:
    142
    Edward Wijaya
    May 13, 2004
  5. ccc31807

    skipping blank array items

    ccc31807, Sep 6, 2013, in forum: Perl Misc
    Replies:
    14
    Views:
    236
    Willem
    Sep 8, 2013
Loading...

Share This Page