Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > skipping an element in an array

Reply
Thread Tools

skipping an element in an array

 
 
Ken
Guest
Posts: n/a
 
      05-23-2004
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[i]: " << a[i] <<endl;
cout<<" t2: " << t2 <<endl;
cout<<" before i: " << i <<endl;
i++;
a[i] = t2;
j = i;
cout<<" j: " << j <<endl;
cout<<" a[i]: " << a[i] <<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[i] = t1;
cout<<" before j: " << j <<endl;
cout<<" t1: " << t1 <<endl;
cout<<" i: " << i <<endl;
cout<<" a[i]: " << a[i] <<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[i]: " << a[i] << 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;
}


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-23-2004

"Ken" <(E-Mail Removed)0m> wrote in message
news:wj8sc.17$(E-Mail Removed)...
> 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[i]: " << a[i] <<endl;
> cout<<" t2: " << t2 <<endl;
> cout<<" before i: " << i <<endl;
> i++;
> a[i] = t2;
> j = i;
> cout<<" j: " << j <<endl;
> cout<<" a[i]: " << a[i] <<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[i] = t1;
> cout<<" before j: " << j <<endl;
> cout<<" t1: " << t1 <<endl;
> cout<<" i: " << i <<endl;
> cout<<" a[i]: " << a[i] <<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


 
Reply With Quote
 
 
 
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      05-23-2004
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/
 
Reply With Quote
 
Ken
Guest
Posts: n/a
 
      05-23-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Ken" <(E-Mail Removed)0m> wrote in message
> news:wj8sc.17$(E-Mail Removed)...
> > 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[i]: " << a[i] <<endl;
> > cout<<" t2: " << t2 <<endl;
> > cout<<" before i: " << i <<endl;
> > i++;
> > a[i] = t2;
> > j = i;
> > cout<<" j: " << j <<endl;
> > cout<<" a[i]: " << a[i] <<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[i] = t1;
> > cout<<" before j: " << j <<endl;
> > cout<<" t1: " << t1 <<endl;
> > cout<<" i: " << i <<endl;
> > cout<<" a[i]: " << a[i] <<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
>
>



 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-24-2004

"Ken" <(E-Mail Removed)0m> wrote in message
news:izasc.19$(E-Mail Removed)...
> 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


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-24-2004
> >
> > 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


 
Reply With Quote
 
Ken
Guest
Posts: n/a
 
      05-24-2004
crap, I am mixing those two, I will try that


ken


"John Harrison" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> > >
> > > 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
>
>



 
Reply With Quote
 
Ken
Guest
Posts: n/a
 
      05-24-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> > >
> > > 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
>
>



 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-24-2004

"Ken" <(E-Mail Removed)0m> wrote in message
newsrosc.43$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      05-24-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Each Array Method Skipping first Array position Chris R. Ruby 3 01-28-2011 04:00 PM
Once again pointer to first element vs pointer to array cast topointer to element Szabolcs Borsanyi C Programming 6 05-23-2008 11:06 AM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
How to delete array element and add to previous element Al Cholic Ruby 18 07-28-2007 04:56 PM
Skipping content in Array within loop and go to the next (of NEXT function) Tad McClellan Perl Misc 3 05-13-2004 10:14 PM



Advertisments